谁能帮我看下这段程序,为什么最后输出的直线斜率是正的,两个点所画出的直线应该是负的才对啊#include #include // 四舍五入int Round(float x){\x05return (int)(x < 0 x - 0.5 :x + 0.5);}// 使用 DDA 算法画任

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 05:22:39

谁能帮我看下这段程序,为什么最后输出的直线斜率是正的,两个点所画出的直线应该是负的才对啊#include #include // 四舍五入int Round(float x){\x05return (int)(x < 0 x - 0.5 :x + 0.5);}// 使用 DDA 算法画任
谁能帮我看下这段程序,为什么最后输出的直线斜率是正的,两个点所画出的直线应该是负的才对啊
#include
#include
// 四舍五入
int Round(float x)
{
\x05return (int)(x < 0 x - 0.5 :x + 0.5);
}
// 使用 DDA 算法画任意斜率的直线(包括起始点,不包括终止点)
void Line_DDA(int x1,int y1,int x2,int y2,int color)
{
\x05float x,y;\x05\x05// 当前坐标点
\x05float cx,cy;\x05// x、y 方向上的增量
\x05
\x05int steps = abs(x2 - x1) > abs(y2 - y1) abs(x2 - x1) :abs(y2 - y1);
\x05x = (float)x1;
\x05y = (float)y1;
\x05cx = (float)(x2 - x1) / steps;
\x05cy = (float)(y2 - y1) / steps;
\x05for(int i = 0; i < steps; i++)
\x05{
\x05\x05putpixel(Round(x),Round(y),color);\x05// 在坐标 (x,y) 处画一个 color 颜色的点
\x05\x05x += cx;
\x05\x05y += cy;
\x05}
}
// 主函数
void main()
{
\x05initgraph(640,480);
\x05
\x05// 测试画线
\x05Line_DDA(100,1,1,478,GREEN);
\x05Line_DDA(1,478,638,1,GREEN);
\x05
\x05// 按任意键退出
\x05getch();
\x05closegraph();
}

谁能帮我看下这段程序,为什么最后输出的直线斜率是正的,两个点所画出的直线应该是负的才对啊#include #include // 四舍五入int Round(float x){\x05return (int)(x < 0 x - 0.5 :x + 0.5);}// 使用 DDA 算法画任
程序没有错误.关于正负问题,主要是因为屏幕坐标系,默认的屏幕坐标系,y 轴向下为正,和数学的坐标系正好相反,所以才会出现你说的问题.