如何画一条斜线

Posted

技术标签:

【中文标题】如何画一条斜线【英文标题】:How do I draw an angled Line 【发布时间】:2021-06-18 14:12:21 【问题描述】:

我试着在一个窗口中用 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);
    

【问题讨论】:

“没有库”是否排除了 Windows GDI(+)? (docs.microsoft.com/en-us/windows/win32/gdi/windows-gdi) 是的,没关系 【参考方案1】:

您对下一行for(int a=0;a&lt;=ratioX;a++)中的比率部分的余数有疑问

虽然ratioX 是一个浮点数,但循环将一直工作到数字的整数部分。 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;
        
    

【讨论】:

所以我把代码改成这样:float ratioX= difX/difY; const float remainderConst=ratioX-(int)ratioX; float remainder=remainderConst; for(int b=0;b&lt;=difY;b++) for(int a=0;a&lt;(int)ratioX;a++) *pixel++=color; remainder += remainderConst; if(remainder&gt;=(float)1) *pixel++=color; remainder--; pixel -= width; 但是这条线大部分时间都是短的,缺少大约 10 到 20 个像素

以上是关于如何画一条斜线的主要内容,如果未能解决你的问题,请参考以下文章

matlab中如何画一条直线

如何在 Sprite-kit 中画一条线

如何使用 fabric.js 在画布上画一条线

HTML5 Canvas - 如何在图像背景上画一条线?

如何在swift中以最简单的方式画一条线

如何在 HighCharts 上画一条垂直线?