繁体   English   中英

如何画一条斜线

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM