怎么画斜线
How do I draw an angled Line
我试着用 c 在 window 中画一些线条和形状(没有一个库可以用一两条线为我画)来学习一点。我已经可以绘制水平和垂直线以及矩形。现在,我想画斜线。
这是完整的代码:
#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
是一个浮点数,但循环将一直工作到数字的整数部分。 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;
}
}
}
我试着用 c 在 window 中画一些线条和形状(没有一个库可以用一两条线为我画)来学习一点。我已经可以绘制水平和垂直线以及矩形。现在,我想画斜线。 这是完整的代码:
#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
是一个浮点数,但循环将一直工作到数字的整数部分。 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;
}
}
}