📄 newmorning.c
字号:
int x0,y0,x1,y1;
initgr();
printf("\ninput the first point:\n");
scanf("%d,%d",&x0,&y0);
printf("\nin put the second point:\n");
scanf("%d,%d",&x1,&y1);
cleardevice();
DDAline(x0,y0,x1,y1);
getch();
jingzhimenu();
}
/*************************************/
/********************************************/
/*画圆*/
/****************************************/
/*******************************************/
void circlepoints(int x,int y,int a,int b,int color)
{
putpixel(x+a,y+b,color);
putpixel(y+a,x+b,color);
putpixel(-y+a,x+b,color);
putpixel(-x+a,y+b,color);
putpixel(y+a,-x+b,color);
putpixel(x+a,-y+b,color);
putpixel(-x+a,-y+b,color);
putpixel(-y+a,-x+b,color);
}
void midpointcircle(int a,int b,int radius,int color)
{
int x,y,deltaE,deltaSE;
float d;
x=0;
y=radius;
d=5-4*radius;
circlepoints(x,y,a,b,color);
deltaE=12;
deltaSE=20-8*radius;
while(y>x)
{
if(d<=0)
{
d+=deltaE;
deltaSE+=8;
}
else
{
d+=deltaSE;
deltaSE+=16;
y--;
}
deltaE+=8;
x++;
circlepoints(x,y,a,b,color);
}
}
/*中点算法*/
midyuan()
{
int radius,color,a,b;
initgr(); /* BGI初始化 */
/*****此部分添加你自己的代码,例如
line(25, 25, 220, 220);
circle(100, 100, 50);
等等*****/
printf("Please input the zhongxin radius and color:");
scanf("%d,%d,%d,%d",&a,&b,&radius,&color);
midpointcircle(a,b,radius,color);
getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
/* 恢复TEXT屏幕模式 */
jingzhimenu();
}
void circlepoints1(int a,int b,int x,int y,int color)
{
putpixel(x+a,y+b,color);
putpixel(y+a,x+b,color);
putpixel(-y+a,x+b,color);
putpixel(-x+a,y+b,color);
putpixel(y+a,-x+b,color);
putpixel(x+a,-y+b,color);
putpixel(-x+a,-y+b,color);
putpixel(-y+a,-x+b,color);
}
void PositiveNegativeArc(int a,int b,int radius,int color)
{
int x,y,d;
x=0;
y=radius;
putpixel(x+a,y+b,color);
x=1;
putpixel(x+a,y+b,color);
d=1;
while(y>=0)
{
if(d>=0)
{
d=d-2*y+1;
y--;
}
else
{
d=d+2*x+1;
x++;
}
circlepoints1(a,b,x,y,color);
}
}
/* 正负算法*/
zhengfuyuan()
{ int radius,color,a,b;
initgr();
printf("Please input zhongxin radius and color:\n");
scanf("%d,%d,%d,%d",&a,&b,&radius,&color);
cleardevice();
PositiveNegativeArc(a,b,radius,color);
getch();
/* 暂停一下,看看前面绘图代码的运行结果 */
jingzhimenu();
}
/******************************************************************/
/******************************************************************/
/*椭圆*/
/******************************************************************/
/*****************************************************************/
void ellipsepoints(int a,int b,int x,int y,int color)
{
putpixel(x+a,y+b,color);
putpixel(-x+a,y+b,color);
putpixel(-x+a,-y+b,color);
putpixel(x+a,-y+b,color);
}
void ellipsepoints1(int x,int y,int color)
{
putpixel(x+200,y+200,color);
putpixel(-x+200,y+200,color);
putpixel(-x+200,-y+200,color);
putpixel(x+200,-y+200,color);
}
void tuoyuan1(int a,int b,int color)
{
long xP,yP,squarea,squareb;
int x,y;
int d,m,n=0;
squarea=a*a;
squareb=b*b;
xP=(long)(0.5+(float)squarea/sqrt((float)(squarea+squareb))); /*计算分界点是为了减少运算次数*/
yP=(long)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));
x=a;
for(;x>=xP;x--) /*画出椭圆下半部分的圆弧*/
{
m=(int)(0.5+(float)b*sqrt((float)(a*a-(x-1)*(x-1)))/a);
d=(int)(0.5+(float)b*sqrt((float)(a*a-(x-1)*(x-1)))/a-(float)b*sqrt((float)(a*a-x*x))/a);
for(y=n;y<=m;y++) /*描出每个横坐标对应的所有像素*/
ellipsepoints1((int)(x-1),(int)(y),color);
n+=d;
}
n=0;
for(y=b;y>=yP;y--) /*画出椭圆上半部分的圆弧*/
{
m=(int)(0.5+(float)a*sqrt((float)(b*b-(y-1)*(y-1)))/b);
d=(int)(0.5+(float)a*sqrt((float)(b*b-(y-1)*(y-1)))/b-(float)a*sqrt((float)(b*b-y*y))/b);
for(x=n;x<=m;x++) /*描出每个纵坐标对应的所有像素*/
ellipsepoints1((int)(x),(int)(y-1),color);
n+=d;
}
}
weibutuoyuan()
{
int a,b;
initgr(); /* BGI初始化 */
/*****此部分添加你自己的代码,例如
line(25, 25, 220, 220);
circle(100, 100, 50);
等等*****/
/*printf("Please enter a:\n");
scanf("%d",&a);
printf("Please enter b:\n");
scanf("%d",&b);
tuoyuan(a,b,RED);*/
tuoyuan1(120,50,5);
getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
/* 恢复TEXT屏幕模式 */
jingzhimenu();
}
void midpointellipse(int a1,int a2,int a,int b,int color)
{
/*int x,y,d,xP,yP,squarea,squareb;*/
long x,y,d,xP,yP,squarea,squareb;
squarea=a*a;
squareb=b*b;
/*xP=(int)(0.5+(float)squarea/sqrt((float)(squarea+squareb)));
yP=(int)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));*/
xP=(long)(0.5+(float)squarea/sqrt((float)(squarea+squareb)));
yP=(long)(0.5+(float)squareb/sqrt((float)(squarea+squareb)));
x=0;y=b;
d=4*(squareb-squarea*b)+squarea;
ellipsepoints(a1,a2,x,y,color);
while(x<=xP)
{
if(d<=0)
d+=4*squareb*(2*x+3);
else
{
d+=4*squareb*(2*x+3)-8*squarea*(y-1);
y--;
}
x++;
ellipsepoints(a1,a2,x,y,color);
}
x=a;y=0;
d=4*(squarea-a*squareb)+squareb;
ellipsepoints(a1,a2,x,y,color);
while(y<yP)
{
if(d<=0)
d+=4*squarea*(2*y+3);
else
{
d+=4*squarea*(2*y+3)-8*squareb*(x-1);
x--;
}
y++;
ellipsepoints(a1,a2,x,y,color);
}
}
int midtuoyuan()
{int a1,a2,a,b;
initgr();
printf("Please enter a1,a2,a,b:\n");
scanf("%d,%d,%d,%d",&a1,&a2,&a,&b);
midpointellipse(a1,a2,a,b,BLUE);
midpointellipse(a1,a2,a-50,b-20,YELLOW);
getch();
jingzhimenu();}
/***************************************************/
/*****************************************************/
/*抛物线*/
/*****************************************************/
/********************************************************/
void duicheng(x,y,color)
{
putpixel(x+150,y+150,color);
putpixel(x+150,-y+150,color);
}
void paowuxian(int y1,int color)
{
int x,y,d;
x=0,y=0;
putpixel(x,y,color);
x=1;
putpixel(x,y,color);
d=1;
while(y<=y1)
{
if(d>=0)
{
d=d-2*y-1;
y++;
}
else
{
d=d+1;
x++;
}
duicheng(x,y,color);
}
}
PaoWuxian()
{
initgr();
paowuxian(50,RED);
paowuxian(10,BLUE);
getch();
jingzhimenu();
}
/********************************************************/
/**********************************************************/
/*裁剪*/
/***********************************************************/
/***********************************************************/
/*求交点,裁剪中的*/
void Intersect(Vertex *s,Vertex *p,Edge clipBoundary,Vertex *I)
{
if(clipBoundary[0].y==clipBoundary[1].y)/*水平的裁剪边*/
{
I->y=clipBoundary[0].y;
I->x=s->x+(clipBoundary[0].y-s->y)*(p->x-s->x)/(p->y-s->y);
}
else/*竖直裁剪边*/
{
I->x=clipBoundary[0].x;
I->y=s->y+(clipBoundary[0].x-s->x)*(p->y-s->y)/(p->x-s->x);
}
}
int Inside(Vertex *testVertex,Edge clipBoundary)
{
if(clipBoundary[1].x>clipBoundary[0].x)
{ if(testVertex->y<=clipBoundary[0].y) return true;
}
else if(clipBoundary[1].x<clipBoundary[0].x)
{ if(testVertex->y>=clipBoundary[0].y) return true;
}
else if(clipBoundary[1].y>clipBoundary[0].y)
{ if(testVertex->x>=clipBoundary[0].x) return true;
}
else if(clipBoundary[1].y<clipBoundary[0].y)
{ if(testVertex->x<=clipBoundary[0].x) return true;
}
return false;
} /*程序Inside()结束*/
void Output(Vertex *newVertex,int *outLength,VertexArray outVertexArray)
/*将new Vetex加入到结果多边形顶点表outVertexArray中*/
{
outVertexArray[*outLength].x=newVertex->x;
outVertexArray[*outLength].y=newVertex->y;
(*outLength)++;
}/*output结束*/
/* 裁剪算法*/
void SutherlandHodgmanPolygonclip(int inLength,VertexArray inVertexArray,int *outLength,VertexArray outVertexArray,Edge clipBoundary)
{
Vertex *s,*p,I;
int j;
*outLength=0;
s=&(inVertexArray[inLength-1]);
for(j=0;j<inLength;j++)
{
p=&(inVertexArray[j]);
if(Inside(p,clipBoundary))
{ if(Inside(s,clipBoundary))
Output(p,outLength,outVertexArray);
else
{ Intersect(s,p,clipBoundary,&I);
Output(&I,outLength,outVertexArray);
Output(p,outLength,outVertexArray);
}
}
else if(Inside(s,clipBoundary))
{ Intersect(s,p,clipBoundary,&I);
Output(&I,outLength,outVertexArray);
}
s=p;
}
}
/* 裁剪开始*/
caijian()
{int i,j,d,inlength,*outLength;
VertexArray a;
VertexArray inVertexArray,outVertexArray;
Edge b1,b2,b3,b4,c;
initgr();
printf("input ai cai jian duo bian xing ding dian shu\n");
scanf("%d",&inlength);
printf("input dai cai jian duo bian xing dingdian:\n");
for(i=0;i<inlength;i++)
scanf("%d,%d",&inVertexArray[i].x,&inVertexArray[i].y);
printf("please input caijian duo bian xing\n");
for(i=0;i<=1;i++)
scanf("%d,%d",&c[i].x,&c[i].y);
cleardevice();
duobianxing(inlength,inVertexArray);
setcolor(1);
rectangle(c[0].x,c[0].y,c[1].x,c[1].y);
getch();
cleardevice();
b1[0].x=c[0].x;b1[0].y=c[0].y;
b1[1].x=c[0].x;b1[1].y=c[1].y;
b2[0].x=c[0].x;b2[0].y=c[1].y;
b2[1].x=c[1].x;b2[1].y=c[1].y;
b3[0].x=c[1].x;b3[0].y=c[1].y;
b3[1].x=c[1].x;b3[1].y=c[0].y;
b4[0].x=c[1].x;b4[0].y=c[0].y;
b4[1].x=c[0].x;b4[1].y=c[0].y;
SutherlandHodgmanPolygonclip(inlength,inVertexArray,outLength,outVertexArray,b1);
inlength=*outLength;
for(i=0;i<inlength;i++)
inVertexArray[i]=outVertexArray[i];
SutherlandHodgmanPolygonclip(inlength,inVertexArray,outLength,outVertexArray,b2);
inlength=*outLength;
for(i=0;i<inlength;i++)
inVertexArray[i]=outVertexArray[i];
SutherlandHodgmanPolygonclip(inlength,inVertexArray,outLength,outVertexArray,b3);
inlength=*outLength;
for(i=0;i<inlength;i++)
inVertexArray[i]=outVertexArray[i];
SutherlandHodgmanPolygonclip(inlength,inVertexArray,outLength,outVertexArray,b4);
initgr();
duobianxing(*outLength,outVertexArray);
getch();
jingzhimenu();
}
/*******************************************************************/
/*********************************************************************/
/*字符输出呵呵 */
/********************************************************************/
/***********************************************************************/
void heng1(int hx,int hy)
{
setcolor(YELLOW);
line(hx+6,hy+0,hx+30,hy+0);
line(hx+6,hy+1,hx+30,hy+1);
setcolor(WHITE);
line(hx+5,hy+2,hx+31,hy+2);
setcolor(YELLOW);
line(hx+6,hy+3,hx+30,hy+3);
line(hx+6,hy+4,hx+30,hy+4);
}
void shu1(int sx,int sy)
{
setcolor(YELLOW);
line(sx+0,sy+5,sx+0,sy+30);
line(sx+1,sy+5,sx+1,sy+30);
setcolor(WHITE);
line(sx+2,sy+4,sx+2,sy+31);
setcolor(YELLOW);
line(sx+3,sy+5,sx+3,sy+30);
line(sx+4,sy+5,sx+4,sy+30);
}
void draw_01(int x0,int y0)
{
heng1(x0,y0);
heng1(x0,y0+60);
shu1 (x0,y0);
shu1 (x0+32,y0);
shu1 (x0+32,y0+30);
shu1 (x0,y0+30);
}
void draw_11(int x0,int y0)
{
shu1 (x0+32,y0);
shu1 (x0+32,y0+30);
}
void draw_2(int x0,int y0)
{
heng1(x0,y0);
heng1(x0,y0+30);
heng1(x0,y0+60);
shu1 (x0+32,y0);
shu1 (x0,y0+30);
}
void draw_31(int x0,int y0)
{
heng1(x0,y0);
heng1(x0,y0+30);
heng1(x0,y0+60);
shu1 (x0+32,y0);
shu1 (x0+32,y0+30);
}
void draw_4(int x0,int y0)
{
heng1(x0,y0+30);
shu1 (x0,y0);
shu1 (x0+32,y0);
shu1 (x0+32,y0+30);
}
void draw_5(int x0,int y0)
{
heng1(x0,y0);
heng1(x0,y0+30);
heng1(x0,y0+60);
shu1 (x0,y0);
shu1 (x0+32,y0+30);
}
void draw_point(px,py)
{
register int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(!((i==1)&&(j==1)))
putpixel(px+j,py+i,YELLOW);
putpixel(px+1,py+1,WHITE);
}
char tu64H[]={
/* 以下是 '图' 的 64点阵黑体 字模,512 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,
0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,
0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,
0x01,0xF0,0x00,0x00,0x00,0x00,0x0F,0x00,
0x01,0xF0,0x00,0x00,0x00,0x00,0x0F,0x00,
0x01,0xF0,0x00,0x20,0x00,0x00,0x0F,0x00,
0x01,0xF0,0x00,0x3C,0x00,0x00,0x0F,0x00,
0x01,0xF0,0x00,0x3C,0x00,0x00,0x0F,0x00,
0x01,0xF0,0x00,0x7C,0x00,0x00,0x0F,0x00,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -