int x,y,x0, y0, x1, y1,a,b; int d0, d1, d2; float k; DCPoint->SetROP2(R2_COPYPEN);//绘图方法为直接画 //直线端点由鼠标确定后存放在group[0]、group[1] x0 = group[0].x; y0 = group[0].y; x1 = group[1].x; y1 = group[1].y; a = y0 - y1; b = x1 - x0; d0 = 2 * a + b; d1 = a; d2 = a + b; if (x0 == x1 && y0 == y1)return; if (x0 == x1)//垂直线,k不存在 { if (y0 > y1) { x = y0; y0 = y1; y1 = x; } for (x = y0; x <= y1; x++) { DCPoint->SetPixel(x0, x, RGB(255, 0, 0)); } return; } if (y0 == y1)//水平线,k=0 { if (x0 > x1) { x = x0; x0 = x1; x1 = x; } for (x = x0; x <= x1; x++) { DCPoint->SetPixel(x, y0, RGB(255, 0, 0)); } return; } if ((!x0 == x1) && (!y0 == y1)) { k = (-a) / b; x = x0; y = y0; for (; x <= x1;) { DCPoint->SetPixel(x, y, RGB(255, 0, 0)); if (d0 <= 0) { x++, y++, d0 += d2; } else { x++, d0 += d1; } }}画不出直线是什么原因
时间: 2024-04-04 20:07:53 浏览: 12
有可能是以下几个原因:
1. 给定的两个点坐标不合法,比如两个点的横纵坐标相等,或者两个点的坐标差值太大,超出了绘制范围。
2. 绘图设备 DCPoint 没有被正确初始化,导致无法绘制像素点。
3. 绘图设备的画笔属性没有被正确设置,导致无法绘制像素点。
4. 在计算斜率 k 的过程中出现了除 0 的情况,导致程序崩溃。
你可以检查以上几个原因,看看哪一个是导致无法绘制直线的原因。同时,你也可以将代码中的绘制方式改为其他方式,比如使用 GDI+ 绘制直线,或者使用 OpenGL 绘制直线。这些方式都可以实现直线的绘制。
相关问题
用void MidPointEllipse(int x0,int y0,int x1,int y1,void (*SetPixel)(int x,int y))写中点画椭圆的代码
void MidPointEllipse(int x0, int y0, int x1, int y1, void (*SetPixel)(int x, int y)) {
int a = abs(x1 - x0);
int b = abs(y1 - y0);
int b2 = b * b;
int a2 = a * a;
int d = a2 + b2 * (-a + 1) / 4 + b2 / 4;
int x = 0;
int y = b;
while (a2 * (y - 0.5) > b2 * (x + 1)) {
if (d < 0) {
d += a2 * (2 * y + 3);
} else {
d += a2 * (2 * y + 3) + b2 * (-2 * x + 2);
x++;
}
y--;
SetPixel(x0 + x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 - x, y0 - y);
}
d = a2 * (y * y - y) + b2 * (x * x + x - a2 * b2);
while (y >= 0) {
SetPixel(x0 + x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 - x, y0 - y);
if (d < 0) {
d += a2 * (2 * y - 1) + b2 * (2 * x + 2);
x++;
} else {
d += b2 * (2 * x + 2);
}
y--;
}
}
用void MidPointCircle(int x0,int y0,int x1,int y1,void (*SetPixel)(int x,int y))写中点画圆的代码
void MidPointCircle(int x0, int y0, int x1, int y1, void(*SetPixel)(int x, int y)) {
int r = sqrt(pow((x1 - x0), 2) + pow((y1 - y0), 2)); // 计算圆的半径
int x = 0, y = r;
int d = 1 - r; // 初始决策参数
SetPixel(x0 + x, y0 + y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 - y);
SetPixel(x0 + y, y0 + x);
SetPixel(x0 - y, y0 + x);
SetPixel(x0 + y, y0 - x);
SetPixel(x0 - y, y0 - x);
while (x < y) {
if (d < 0) {
d += (x << 1) + 3;
}
else {
d += ((x - y) << 1) + 5;
y--;
}
x++;
SetPixel(x0 + x, y0 + y);
SetPixel(x0 - x, y0 + y);
SetPixel(x0 + x, y0 - y);
SetPixel(x0 - x, y0 - y);
SetPixel(x0 + y, y0 + x);
SetPixel(x0 - y, y0 + x);
SetPixel(x0 + y, y0 - x);
SetPixel(x0 - y, y0 - x);
}
}