计算机图形学-2.1用中点画线扫描转换算法,绘制任意斜率 的直线。可以通过调用此函数绘制图案(图案中包含各 种斜率)
程序员文章站
2022-07-14 10:09:27
...
(1)算法设计原理
计算k值不同情况下d的初值和d’与d的计算式
总结如下
(2)程序关键代码
//直线垂直于y轴,斜率不存在
if(x2==x1)
{
y1=y1<y2?y1:y2;
y2=y1<y2?y2:y1;
while(y1<=y2)
putpixel(x1,y1++,color);
}
//计算斜率
k=(float)(y2-y1)/(float)(x2-x1);
//根据斜率选择相应的函数
( if(k>=0&&k<=1)
MidPointLine1(x1,y1,x2,y2,color);
else if(k>1)
MidPointLine2(x1,y1,x2,y2,color);
else if(k>=-1&&k<=0)
MidPointLine3(x1,y1,x2,y2,color);
else
MidPointLine4(x1,y1,x2,y2,color);
)
//0<k<1
void MidPointLine1(int x0,int y0,int x1,int y1,int color)
{
x=x0<x1?x0:x1;
x1=x1>x0?x1:x0;
y=x0<x1?y0:y1;
y1=x1>x0?y1:y0; //使(x,y),(x1,y1)的关系满足x<x1
a=y-y1;
b=x1-x;
d=2*a+b;
delta1=2*(a+b);
delta2=2*a;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{
x++;y++;
d+=delta1;
}
else
{
x++;
d+=delta2;
}
putpixel(x,y,color);
}
}
//k>1
void MidPointLine2(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=y0<y1?x0:x1;
x1=y1>y0?x1:x0;
y=y0<y1?y0:y1;
y1=y1>y0?y1:y0; //使(x,y),(x1,y1)的关系满足y<y1
a=y-y1;
b=x1-x;
d=a+2*b;
delta1=2*b;
delta2=2*(a+b);
putpixel(x,y,color);
while(y<y1)
{
if(d<0)
{
y++;
d+=delta1;
}
else
{
y++;x++;
d+=delta2;
}
putpixel(x,y,color);
}
}
//-1<k<0
void MidPointLine3(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=x0<x1?x0:x1;
x1=x1>x0?x1:x0;
y=x0<x1?y0:y1;
y1=x1>x0?y1:y0; //使(x,y),(x1,y1)的关系满足x<x1
a=y-y1;
b=x1-x;
d=2*a-b;
delta1=2*a;
delta2=2*a-2*b;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{
x++;
d+=delta1;
}
else
{
x++;y--;
d+=delta2;
}
putpixel(x,y,color);
}
}
//k<-1
void MidPointLine4(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=y0>y1?x0:x1;
x1=y0>y1?x1:x0;
y=y0>y1?y0:y1;
y1=y0>y1?y1:y0; //使(x,y),(x1,y1)的关系满足y>y1
a=y-y1;
b=x1-x;
d=a-2*b;
delta1=2*(a-b);
delta2=-2*b;
putpixel(x,y,color);
while(y>y1)
{
if(d<0)
{
x++;y--;
d+=delta1;
}
else
{
y--;
d+=delta2;
}
putpixel(x,y,color);
}
}
(3)运行结果截屏(对数据输入的说明)
完整代码:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
//0<k<1
void MidPointLine1(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=x0<x1?x0:x1;
x1=x1>x0?x1:x0;
y=x0<x1?y0:y1;
y1=x1>x0?y1:y0;
a=y-y1;
b=x1-x;
d=2*a+b;
delta1=2*(a+b);
delta2=2*a;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{
x++;y++;
d+=delta1;
}
else
{
x++;
d+=delta2;
}
putpixel(x,y,color);
}
}
//k>1
void MidPointLine2(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=y0<y1?x0:x1;
x1=y1>y0?x1:x0;
y=y0<y1?y0:y1;
y1=y1>y0?y1:y0;
a=y-y1;
b=x1-x;
d=a+2*b;
delta1=2*b;
delta2=2*(a+b);
putpixel(x,y,color);
while(y<y1)
{
if(d<0)
{
y++;
d+=delta1;
}
else
{
y++;x++;
d+=delta2;
}
putpixel(x,y,color);
}
}
//-1<k<0
void MidPointLine3(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=x0<x1?x0:x1;
x1=x1>x0?x1:x0;
y=x0<x1?y0:y1;
y1=x1>x0?y1:y0;
a=y-y1;
b=x1-x;
d=2*a-b;
delta1=2*a;
delta2=2*a-2*b;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{
x++;
d+=delta1;
}
else
{
x++;y--;
d+=delta2;
}
putpixel(x,y,color);
}
}
//k<-1
void MidPointLine4(int x0,int y0,int x1,int y1,int color)
{
int a,b,delta1,delta2,d,x,y;
x=y0>y1?x0:x1;
x1=y0>y1?x1:x0;
y=y0>y1?y0:y1;
y1=y0>y1?y1:y0;
a=y-y1;
b=x1-x;
d=a-2*b;
delta1=2*(a-b);
delta2=-2*b;
putpixel(x,y,color);
while(y>y1)
{
if(d<0)
{
x++;y--;
d+=delta1;
}
else
{
y--;
d+=delta2;
}
putpixel(x,y,color);
}
}
void MidPointLine(int x1,int y1,int x2,int y2,int color)
{
float k;
k=(float)(y2-y1)/(float)(x2-x1);
if(x2==x1)
{
y1=y1<y2?y1:y2;
y2=y1<y2?y2:y1;
while(y1<=y2)
putpixel(x1,y1++,color);
}
else if(k>=0&&k<=1)
MidPointLine1(x1,y1,x2,y2,color);
else if(k>1)
MidPointLine2(x1,y1,x2,y2,color);
else if(k>=-1&&k<=0)
MidPointLine3(x1,y1,x2,y2,color);
else
MidPointLine4(x1,y1,x2,y2,color);
}
void main()
{
initgraph(640, 480);
MidPointLine(400,100,0,0,0xAA0000); //斜率为0~1,蓝色
MidPointLine(100,400,0,0,0x00AA00); //斜率大于1,绿色
MidPointLine(0,100,400,0,0x55FFFF); //斜率为-1~0,黄色
MidPointLine(0,400,100,0,0xFF55FF); //斜率小于-1,亮紫
MidPointLine(0,240,640,240,0xFFFFFF); //垂直于y轴,白色
MidPointLine(320,0,320,480,0xFFFF55); //垂直于x轴,亮青
_getch();
closegraph();
}