试验一、中点画直线算法
#include <>
#include <>
void MidpointLine1(int x0,int y0,int x1, int y1,int color)
{ int a, b, d1, d2, d, x, y;
a=y0-y1; b=x1-x0; d=2*a+b;
d1=2*a ; d2=2* (a+b);
x=x0; y=y0;
putpixel(x, y, color);
while (x<x1)
{ if (d<0) {x++; y++; d+=d2; }
else {x++; d+=d1;}
putpixel (x, y, color); } }
void MidpointLine2(int x0,int y0,int x1, int y1,int color)
{ int a, b, d1, d2, d, x, y;
a=y0-y1; b=x1-x0; d=2*a-b;
d1=2*a ; d2=2* (a-b);
x=x0; y=y0;
putpixel(x, y, color);
while (x<x1){
if (d<0) {x++; d+=d1; }
else {x++;y--; d+=d2; }
putpixel (x, y, color); } }
void MidpointLine3(int x0,int y0,int x1, int y1,int color)
{ int a, b, d1, d2, d, x, y;
a=y0-y1; b=x1-x0; d=a+2*b;
d1=2*b ; d2=2* (a+b);
x=x0; y=y0;
putpixel(x, y, color);
while (x<x1){
if (d<0) {y++; d+=d1; }
else {x++; y++; d+=d2; }
putpixel (x, y, color); } }
void MidpointLine4(int x0,int y0,int x1, int y1,int color)
{ int a, b, d1, d2, d, x, y;
a=y0-y1; b=x1-x0; d=a-2*b;
d1=-2*b ; d2=2* (a-b);
x=x0; y=y0;
putpixel(x, y, color);
while (x<x1){
if (d<0) {x++;y--; d+=d2; }
else {y--; d+=d1; }
putpixel (x, y, color); } }
//当输入两端点x坐标相同时,斜率为无穷大,调用以下函数画线
LineV(int x,int y0,int y1,int color)
{ int y,t;
if(y1<y0){t=y0;y0=y1;y1=t;}
for(y=y0;y<=y1;y++)
putpixel(x,y,color); }
main()
{ int x0,y0,x1,y1,t; float k;
int gdriver = DETECT , gmode ;
scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
initgraph ( &gdriver , &gmode ,“c:\\tc\\bgi") ;
if(x0==x1) LineV(x0,y0,y1,WHITE);
else{ if(x1<x0)
{t=x0;x0=x1;x1=t;
t=y0;y0=y1;y1=t; }
k=(float)(y1-y0)/(x1-x0);
if (k>=0&&k<1)MidpointLine1(x0,y0,x1,y1,WHITE);
else if(k<0&&k>=-1)MidpointLine2(x0,y0,x1,y1,WHITE);
else if(k>1)MidpointLine3(x0,y0,x1,y1,WHITE);
else MidpointLine4(x0,y0,x1,y1,WHITE); }
getch(); closegraph ( ) ; }
二、中点画圆
#include <>
#include <>
void Midpoint_circle(r,color)
int r,c
计算机图形学实验答案样稿 来自淘豆网m.daumloan.com转载请标明出处.