[英]How do I draw an angled Line
我試着在 window 和 c 中畫一些線條和形狀(沒有一個庫,它用一兩條線為我做這件事)來學習一點。 我已經可以繪制水平和垂直線和矩形。 現在,我想畫有角度的線。 這是整個代碼:
#include <windows.h>
#include <stdint.h>
typedef uint32_t u32;
int running = 1;
int width = 0;
int height = 0;
int rgb=0;
int pointA[]={100,100};
int pointB[]={200,100};
void* memory;
BITMAPINFO bitmap_info;
//Farben
u32 red = 0xFF0000;
u32 green = 0x00FF00;
u32 blue = 0x0000FF;
u32 black = 0x000000;
u32 white = 0xFFFFFF;
int calculateCord(int x, int y){
int memoryNumber=(height-y+1)*width-(width-x+1);
return memoryNumber;
}
void setBackground(u32 color)
{
u32 *pixel = (u32 *)memory;
for (int memoryNumber=0;memoryNumber<width*height;++memoryNumber)
{
*pixel++=color;
}
}
void drawPixel(u32 color,int x,int y)
{
u32 *pixel = (u32 *)memory;
pixel += calculateCord(x,y);
*pixel=color;
}
void drawLineHor(u32 color,int x,int y,int lineWidth,int direction)
{
u32 *pixel=(u32 *)memory;
pixel+=calculateCord(x,y);
for(int a=0;a<lineWidth;a++)
{
if(direction ==0){
*pixel++=color;
}else{
*pixel--=color;
}
}
}
void drawLineVert(u32 color,int x,int y,int lineHeight,int direction)
{
u32 *pixel=(u32 *)memory;
pixel+=calculateCord(x,y);
*pixel=color;
for(int b=0;b<lineHeight;b++)
{
*pixel=color;
if(direction==0){
pixel-= width;
}else {
pixel += width;
}
}
}
void drawLineDiagonal(u32 color,int x,int y,int distance,int direction)
{
u32 *pixel=(u32 *)memory;
pixel+=calculateCord(x,y);
*pixel=color;
for(int a=0;a<distance;a++){
if (direction==0){
*pixel++=color;
pixel -= width;
}else{
*pixel--=color;
pixel += width;
}
}
}
void drawLineAngle(u32 color,int x,int y,int targetX,int targetY)
{
u32 *pixel=(u32 *)memory;
pixel += calculateCord(x,y);
*pixel=color;
int difX=targetX-x+1;
int difY=targetY-y+1;
float ratioX= difX/difY;
if(ratioX>=1){
for (int b=0;b<difY;b++)
{
for(int a=0;a<=ratioX;a++)
{
*pixel++=color;
}
*pixel=color;
pixel -= width;
}
}
}
void drawRect(u32 color,int x,int y,int rectWidth,int rectHeight)
{
u32 *pixel=(u32 *)memory;
pixel+=calculateCord(x,y);
*pixel=color;
for (int a=0;a<rectWidth;a++)
{
*pixel++=color;
}
for(int b=0;b<rectHeight;b++)
{
*pixel=color;
pixel -= width;
}
for (int a=0;a<rectWidth;a++)
{
*pixel--=color;
}
for(int b=0;b<rectHeight;b++)
{
*pixel=color;
pixel += width;
}
}
void fillRect(u32 color,int x,int y,int rectWidth,int rectHeight)
{
u32 *pixel=(u32 *)memory;
pixel += calculateCord(x,y);
*pixel=color;
for(int b=0;b<rectHeight;b++)
{
for(int a=0;a<rectWidth;a++)
{
*pixel++=color;
}
pixel -= width+rectWidth;
*pixel=color;
}
}
LRESULT CALLBACK
WindowProc(HWND window,
UINT message,
WPARAM w_param,
LPARAM l_param)
{
LRESULT result;
switch(message)
{
case WM_CLOSE:
{
running = 0;
printf("Ende");
} break;
default:
{
result = DefWindowProc(window,
message,
w_param,
l_param);
} break;
}
return result;
}
int WINAPI
wWinMain(HINSTANCE instance,
HINSTANCE prev_instance,
PWSTR cmd_line,
int cmd_show)
{
WNDCLASS window_class = {0};
wchar_t class_name[] = L"GameWindowClass";
window_class.lpfnWndProc = WindowProc;
window_class.hInstance = instance;
window_class.lpszClassName = class_name;
RegisterClass(&window_class);
HWND window = CreateWindowEx(0,
class_name,
L"Fenster",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
instance,
0);
RECT rect;
GetClientRect(window, &rect);
width = rect.right - rect.left;
height = rect.bottom - rect.top;
memory = VirtualAlloc(0,
width * height * 4,
MEM_RESERVE|MEM_COMMIT,
PAGE_READWRITE);
setBackground(black);
drawLineAngled(red,100,100,140,150);
drawPixel(blue,140,150);
drawPixel(green,100,100);
bitmap_info.bmiHeader.biSize = sizeof(bitmap_info.bmiHeader);
bitmap_info.bmiHeader.biWidth = width;
bitmap_info.bmiHeader.biHeight = height;
bitmap_info.bmiHeader.biPlanes = 1;
bitmap_info.bmiHeader.biBitCount = 32;
bitmap_info.bmiHeader.biCompression = BI_RGB;
HDC hdc = GetDC(window);
while(running)
{
MSG message;
while(PeekMessage(&message, window, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
StretchDIBits(hdc,
0,
0,
width,
height,
0,
0,
width,
height,
memory,
&bitmap_info,
DIB_RGB_COLORS,
SRCCOPY);
}
return 0;
}
這只是斜線的一部分:
void drawLineAngled(u32 color,int x,int y,int targetX,int targetY)
{
u32 *pixel=(u32 *)memory;
pixel += calculateCord(x,y);
*pixel=color;
int difX=targetX-x+1;
int difY=targetY-y+1;
float ratioX= difX/difY;
if(ratioX>=1){
for (int b=0;b<difY;b++)
{
for(int a=0;a<=ratioX;a++)
{
*pixel++=color;
}
*pixel=color;
pixel -= width;
}
}
當我執行它時,它會畫一條有角度的線,但不是重點。 這條線在目標點右側結束一點像素。 我知道代碼不是最漂亮的,還有一些需要改進的地方,但我想先集中精力。(對不起我的英語)
(編輯)現在我得到了這個工作正常的代碼
void drawAngledLine(u32 color,int originX,int originY,int targetX,int targetY)
{
int swap=0;
if(originX > targetX || originY > targetY){
int swapX=originX;
originX=targetX;
targetX=swapX;
int swapY=originY;
originY=targetY;
targetY=swapY;
swap=1;
}
const float m= (float)(targetY-originY)/ (float)(targetX-originX);
const float b= originY-m*(float)originX;
if(m<=1){
for(int x=originX;x<targetX;x++)
{
float y=m*(float)x+b;
y+=0.5;
drawPixel(color,x,(int)y);
}
}else{
const float w=(float)(targetX-originX)/(float)(targetY-originY);
const float v=originX-w*(float)originY;
for(int y=originY;y<targetY;y++)
{
float x=w*(float)y+v;
x+=0.5;
drawPixel(color,(int)x,y);
}
}
if(swap=0){
drawPixel(green,originX,originY);
drawPixel(blue,targetX,targetY);
}else{
drawPixel(green,targetX,targetY);
drawPixel(blue,targetX,targetY);
}
}
您對下一行中比率定義的余數有疑問for(int a=0;a<=ratioX;a++)
盡管ratioX
是一個浮點數,但循環將一直工作到數字的 integer 部分。 從ratioX - (int)ratioX
的差異中剩下的任何東西。
解決方案與我們解決閏年問題相同。 我們累積一年中額外的 6 小時,每 4 年增加一天。 以同樣的方式,每次累加器通過 1(並從累加器中減去 1)時,您需要累加這個差值並添加一個額外的像素。
這會給您留下最后一個像素的問題。 有時,即使有所有補償,您的線也會短一個像素。 因此,您可以明確設置targetX,targetY
上的最后一個像素。
以下是基於上述代碼的示例:
void drawLineAngled(u32 color,int x,int y,int targetX,int targetY)
{
u32 *pixel=(u32 *)memory;
pixel += calculateCord(x,y);
*pixel=color;
int difX=targetX-x+1;
int difY=targetY-y+1;
float ratioX= difX/difY;
const float remainderConst=ratioX-(int)ratioX;
float remainder=0;
if(ratioX>=1){
for (int b=0;b<difY;b++)
{
for(int a=0;a<=ratioX;a++)
{
*pixel++=color;
}
// Add the pixel fraction that we had skipped in the loop
remainder+=remainderConst;
// If the skipped fractions accumulate to 1 or more, add a pixel
if (remainder >= 1) {
*pixel++=color;
reminder--;
}
*pixel=color;
pixel -= width;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.