C语言写椭圆
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
putpixel(-x+300,-y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
while(b*b*(x+1)<a*a*(y-0.5))
{
if(d1<=0)
{
d1+=b*b*(2*x+3);
x++;
}
else
{
d1+=b*b*(2*x+3)+a*a*((-2)*y+2);
x++;
y--;
}
putpixel(x+300,y+200,color);
putpixel(-x+300,-y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
printf("* input 'E' exit system! *\n");
printf("************************************************************\n");
}
/*主函数块*/
void main()
{
int gdriver=DETECT,gmode;
y=(int)r;
d=(int)(3-2*r);
while(x<y)
{
cirpot(x0,y0,x,y,color);
if(d<0)
d+=4*x+6;
else
{
d+=4*(x-y)+10;
y--;
}
x++;
}
if(x==y)
cirpot(x0,y0,x,y,color);
}
int cirpot(int x0,int y0,int x,int y,int color)
else
length=abs(y2-y1);
increx=(x2-x1)/length;
increy=(y2-y1)/length;
x=x1;
y=y1;
for(i=1;i<=length;i++)
{ xx=x+0.5;
yy=y+0.5;
putpixel((int)xx,(int)yy,color);
case '3': line(110,120,200,240);break;
case '4': MidBresenhamllipse(34,55,6);break;
case '5': cs_Ellipse(120,240,20,120,21);break;
case '6': ellipse(110,250,0,360,100,40);break;
{
int i,x=x0,y=y0,r=r1,color=color1;
cs_circle(x,y,r,1);
}
void fun()
{
printf("************************************************************\n");
printf("* input '1' ues midpointline painting line ! *\n");
circle(100,100,67);
cs_Circle(100,100,23,2);break;
}
printf("Input any key to continue!!!!\n");
getch();
cleardevice();
fun();
scanf("%c",&c);
}
while(c!='E');
{
putpixel((x0+x),(y0+y),color);
putpixel((x0+y),(y0+x),color);
putpixel((x0+y),(y0-x),color);
putpixel((x0+x),(y0-y),color);
putpixel((x0-x),(y0-y),color);
{
int x,y,n,i;
float t=0,t1;
t1=dt*0.0174533;
n=360/dt;
moveto(x0+r,y0);
for(i=1;i<n;i++)
{
t=t+t1;
y=y0+r*sin(t);
lineto(x,y);
}
lineto(x0+r,y0);
}
void cs_Circle(int x0,int y0,int r1,int color1)
/*setbkcolor(9);*/
char c;
initgraph(&gdriver,&gmode,"c:\tc");
fun();
scanf("%c",&c);
do
{
switch(c)
{
case '1': midpointline(120,220,200,160,20);break;
case '2': DDA_line(110,120,200,340,10);break;
}
d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>0)
{
if(d2<=0)
{
d2+=b*b*(2*x+2)+a*a*(-2*y+3);
x++;
y--;
}
else
{
d2+=a*a*(-2*y+3);
y--;
}
putpixel(x+300,y+200,color);
printf("* input '2' ues DDA_line painting line! *\n");
printf("* input '3' ues system function painting line ! *\n");
printf("* input '4' ues midBresenham painting elliptic ! *\n");
计算机图形学
用C语言画图(直线,画圆,椭圆的生成算法)
09级地信班兰军12009244560
#include "graphics.h"
#include "conio.h"
#include "dos.h"
#include "math.h"
#include "stdio.h"
/*直线中点算法*/
void midpointline(int x0,int y0,int x1,int y1,int color )
putpixel((x0-y),(y0-x),color);
putpixel((x0-y),(y0+x),color);
putpixel((x0-x),(y0+y),color);
return(0);
}
/*参数法画圆*/
void cs_circle(int x0,int y0,int r,int dt)
putpixel(x,y,color);
}
}
/*直线DDA算法*/
DDA_line(int x1,int y1,int x2,int y2,int color)
{
int i;
float increx,increy,x,y,length,xx,yy;
if(abs(x2-x1)>abs(y2-y1))
length=abs(x2-x1);
case '7': BresenhemCircle(100,100,50,4);break;
case '8': circle(100,100,67);break;
case '9': cs_Circle(100,100,23,2);break;
case 'a': midpointline(120,220,200,160,20);
{
int i,x=x0,y=y0,a=a1,b=b1,color=color1;
cs_ellipse(x,y,a,b,1);
}
/*中点Bresenham算法画圆*/
void BresenhemCircle(int x0,int y0,double r,int color)
{
int x,y,d;
x=0;
printf("* input '5' ues parametric painting elliptic ! *\n");
printf("* input '6' ues system function painting elliptic ! *\n");
printf("* input '7' ues midBresenham painting circle ! *\n");
printf("* input '8' ues midBresenham painting circle ! *\n");
printf("* input '9' ues parametric painting circle ! *\n");
printf("* input 'a' print all graphics!!! *\n");
{
int x,y;
int a,b,c,d,d1,d2;
a=(y0-y1);
b=(x1-x0);
d=a+a+b;
d1=a+a;
d2=a+b+a+b;
x=x0;y=y0;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{x++;y++;d+=d2;}
else
{x++;d+=d1;}
DDA_line(110,120,200,340,10);
line(110,120,200,240);
MidBresenhamllipse(34,55,6);
cs_Ellipse(120,240,20,120,21);
ellipse(110,250,0,360,100,40);
BresenhemCircle(100,100,50,4);
int x,y,n,i;
float t=0,t1;
t1=dt*0.0174533;
n=360/dt;
moveto(x0+a,y0);
for(i=1;i<n;i++)
{
t=t+t1;
x=x0+a*cos(t);
y=y0+b*sin(t);
lineto(x,y);
}Fra Baidu bibliotek
lineto(x0+a,y0);
}
void cs_Ellipse(int x0,int y0,int a1,int b1,int color1)
printf("exit system!\n");
closegraph();
}
putpixel(-x+300,-y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
}
}
/*参数法画椭圆*/
void cs_ellipse(int x0,int y0,int a,int b,int dt)
{
x=x+increx;
y=y+increy;
}
}
/*椭圆中点算法*/
void MidBresenhamllipse(double a,double b,int color)
{
double x,y, d1,d2;
x=0;y=b;
d1=b*b+a*a*(-b+0.25);
putpixel(x+300,y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
while(b*b*(x+1)<a*a*(y-0.5))
{
if(d1<=0)
{
d1+=b*b*(2*x+3);
x++;
}
else
{
d1+=b*b*(2*x+3)+a*a*((-2)*y+2);
x++;
y--;
}
putpixel(x+300,y+200,color);
putpixel(-x+300,-y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
printf("* input 'E' exit system! *\n");
printf("************************************************************\n");
}
/*主函数块*/
void main()
{
int gdriver=DETECT,gmode;
y=(int)r;
d=(int)(3-2*r);
while(x<y)
{
cirpot(x0,y0,x,y,color);
if(d<0)
d+=4*x+6;
else
{
d+=4*(x-y)+10;
y--;
}
x++;
}
if(x==y)
cirpot(x0,y0,x,y,color);
}
int cirpot(int x0,int y0,int x,int y,int color)
else
length=abs(y2-y1);
increx=(x2-x1)/length;
increy=(y2-y1)/length;
x=x1;
y=y1;
for(i=1;i<=length;i++)
{ xx=x+0.5;
yy=y+0.5;
putpixel((int)xx,(int)yy,color);
case '3': line(110,120,200,240);break;
case '4': MidBresenhamllipse(34,55,6);break;
case '5': cs_Ellipse(120,240,20,120,21);break;
case '6': ellipse(110,250,0,360,100,40);break;
{
int i,x=x0,y=y0,r=r1,color=color1;
cs_circle(x,y,r,1);
}
void fun()
{
printf("************************************************************\n");
printf("* input '1' ues midpointline painting line ! *\n");
circle(100,100,67);
cs_Circle(100,100,23,2);break;
}
printf("Input any key to continue!!!!\n");
getch();
cleardevice();
fun();
scanf("%c",&c);
}
while(c!='E');
{
putpixel((x0+x),(y0+y),color);
putpixel((x0+y),(y0+x),color);
putpixel((x0+y),(y0-x),color);
putpixel((x0+x),(y0-y),color);
putpixel((x0-x),(y0-y),color);
{
int x,y,n,i;
float t=0,t1;
t1=dt*0.0174533;
n=360/dt;
moveto(x0+r,y0);
for(i=1;i<n;i++)
{
t=t+t1;
y=y0+r*sin(t);
lineto(x,y);
}
lineto(x0+r,y0);
}
void cs_Circle(int x0,int y0,int r1,int color1)
/*setbkcolor(9);*/
char c;
initgraph(&gdriver,&gmode,"c:\tc");
fun();
scanf("%c",&c);
do
{
switch(c)
{
case '1': midpointline(120,220,200,160,20);break;
case '2': DDA_line(110,120,200,340,10);break;
}
d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>0)
{
if(d2<=0)
{
d2+=b*b*(2*x+2)+a*a*(-2*y+3);
x++;
y--;
}
else
{
d2+=a*a*(-2*y+3);
y--;
}
putpixel(x+300,y+200,color);
printf("* input '2' ues DDA_line painting line! *\n");
printf("* input '3' ues system function painting line ! *\n");
printf("* input '4' ues midBresenham painting elliptic ! *\n");
计算机图形学
用C语言画图(直线,画圆,椭圆的生成算法)
09级地信班兰军12009244560
#include "graphics.h"
#include "conio.h"
#include "dos.h"
#include "math.h"
#include "stdio.h"
/*直线中点算法*/
void midpointline(int x0,int y0,int x1,int y1,int color )
putpixel((x0-y),(y0-x),color);
putpixel((x0-y),(y0+x),color);
putpixel((x0-x),(y0+y),color);
return(0);
}
/*参数法画圆*/
void cs_circle(int x0,int y0,int r,int dt)
putpixel(x,y,color);
}
}
/*直线DDA算法*/
DDA_line(int x1,int y1,int x2,int y2,int color)
{
int i;
float increx,increy,x,y,length,xx,yy;
if(abs(x2-x1)>abs(y2-y1))
length=abs(x2-x1);
case '7': BresenhemCircle(100,100,50,4);break;
case '8': circle(100,100,67);break;
case '9': cs_Circle(100,100,23,2);break;
case 'a': midpointline(120,220,200,160,20);
{
int i,x=x0,y=y0,a=a1,b=b1,color=color1;
cs_ellipse(x,y,a,b,1);
}
/*中点Bresenham算法画圆*/
void BresenhemCircle(int x0,int y0,double r,int color)
{
int x,y,d;
x=0;
printf("* input '5' ues parametric painting elliptic ! *\n");
printf("* input '6' ues system function painting elliptic ! *\n");
printf("* input '7' ues midBresenham painting circle ! *\n");
printf("* input '8' ues midBresenham painting circle ! *\n");
printf("* input '9' ues parametric painting circle ! *\n");
printf("* input 'a' print all graphics!!! *\n");
{
int x,y;
int a,b,c,d,d1,d2;
a=(y0-y1);
b=(x1-x0);
d=a+a+b;
d1=a+a;
d2=a+b+a+b;
x=x0;y=y0;
putpixel(x,y,color);
while(x<x1)
{
if(d<0)
{x++;y++;d+=d2;}
else
{x++;d+=d1;}
DDA_line(110,120,200,340,10);
line(110,120,200,240);
MidBresenhamllipse(34,55,6);
cs_Ellipse(120,240,20,120,21);
ellipse(110,250,0,360,100,40);
BresenhemCircle(100,100,50,4);
int x,y,n,i;
float t=0,t1;
t1=dt*0.0174533;
n=360/dt;
moveto(x0+a,y0);
for(i=1;i<n;i++)
{
t=t+t1;
x=x0+a*cos(t);
y=y0+b*sin(t);
lineto(x,y);
}Fra Baidu bibliotek
lineto(x0+a,y0);
}
void cs_Ellipse(int x0,int y0,int a1,int b1,int color1)
printf("exit system!\n");
closegraph();
}
putpixel(-x+300,-y+200,color);
putpixel(-x+300,y+200,color);
putpixel(x+300,-y+200,color);
}
}
/*参数法画椭圆*/
void cs_ellipse(int x0,int y0,int a,int b,int dt)
{
x=x+increx;
y=y+increy;
}
}
/*椭圆中点算法*/
void MidBresenhamllipse(double a,double b,int color)
{
double x,y, d1,d2;
x=0;y=b;
d1=b*b+a*a*(-b+0.25);
putpixel(x+300,y+200,color);