计算机图形作业 费了很大脑子写出来的
1、时钟(鄙人最得意的一个作品)
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define pi 3.1415926
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;
#define d(a,b,c,d) X(a,b,c);Y(a,b,c);Bline(300,240,x,y,d)
void plot_circle_points(int xc, int yc, int x, int y, int c)
{
putpixel(xc+x, yc+y, c);
putpixel(xc-x, yc+y, c);
putpixel(xc+x, yc-y, c);
putpixel(xc-x, yc-y, c);
putpixel(xc+y, yc+x, c);
putpixel(xc-y, yc+x, c);
putpixel(xc+y, yc-x, c);
putpixel(xc-y, yc-x, c);
}
void Bcircle (int xc, int yc, int radius, int c)
{
int x, y, p;
x=0;
y=radius;
p=3-2*radius;
while (x<y){
plot_circle_points(xc, yc, x, y, c);
if (p<0) p=p+4*x+6;
else{
p=p+4*(x-y)+10;
y-=1;
}
x+=1;
}
if (x == y)
plot_circle_points(xc, yc, x, y, c);
}
void Bline (int x1, int y1, int x2, int y2, int c)
include意思{
int x, y, tmp;
int dx, dy, d;
float k;
if( 0 == (x1 - x2))
{
x = x1;
if(y1 > y2)
{ tmp = y1;y1 = y2; y2 =tmp;}
for(y = y1; y < y2; y ++)
{
putpixel(x, y, abs(getpixel(x,y)-c));
}
return;
}
k = (float)(y2 -y1)/(float)(x2 - x1);
if(k > 1.0)
{
tmp = x1; x1 = y1; y1 = tmp;
tmp = x2; x2 = y2; y2 = tmp;
}
else if(k > 0)
{}
else if(k < -1.0)
{
x1 = -x1;
x2 = -x2;
tmp = x1; x1 = y1; y1 = tmp;
tmp = x2; x2 = y2; y2 = tmp;
}
else if( k < 0)
{
y1 = - y1;
y2 = - y2;
}
else
{
y = y1;
if(x1 > x2)
{ tmp = x1; x1 = x2; x2 = tmp;}
for(x = x1; x <= x2; x ++)
{
putpixel(x, y, abs(getpixel(x,y)-c));
}
return;
}
if(x1 > x2)
{
tmp = x1; x1 = x2; x2 = tmp;
tmp = y1; y1 = y2; y2 = tmp;
}
dx = x2 - x1;
dy = y2 - y1;
d = 2 * dy - dx;
x = x1; y = y1;
for(; x < x2; x ++)
{
if(k > 1)
{putpixel(y, x, abs(getpixel(y, x) - c));}
else if(k > 0)
{putpixel(x, y, abs(getpixel(x,y)-c));}
else if( k < -1)
{putpixel(-y, x, abs(getpixel(-y,x)-c));}
else if(k < 0)
{putpixel(x, -y, abs(getpixel(x,-y)-c));}
else {}
if(d >= 0)
{
y++;
d+=2*dy-2*dx;
}
else
{
d+=2*dy;
}
}
}
void init()
{
int i,l,x1,x2,y1,y2;
setbkcolor(1);
Bcircle(300,240,200,15);
Bcircle(300,240,205,15);
Bcircle(300,240,5,15);
for(i=0;i<60;i++)
{
if(i%5==0) l=15;
else l=5;
x1=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/180)+240;
Bline(x1,y1,x2,y2,15);
}
}
void main()
{
int x,y;
int gd,gm;
unsigned char h,m,s;
struct time t[1];
detectgraph(&gd, &gm);
initgraph(&gd,&gm," ");
init();
gettime(t);
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec;
d(150,h,30,7);
d(170,m,6,14);
d(190,s,6,4);
while(!kbhit())
{
while(t[0].ti_sec==s)
gettime(t);
sound(400);
delay(70);
sound(200);
delay(30);
nosound();
d(190,s,6,4);
s=t[0].ti_sec;
d(190,s,6,4);
if (t[0].ti_min!=m)
{
d(170,m,6,14);
m=t[0].ti_min;
d(170,m,6,14);
}
if (t[0].ti_hour!=h)
{
d(150,h,30,7);
h=t[0].ti_hour;
d(150,h,30,7);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
}
}
2、笑脸(初学者作品 很漂亮的!)
#include "Conio.h"
#include "graphics.h"
#define closegr closegraph
void initgr(void)
{
int graphdriver=DETECT, graphmode=0;
initgraph(&graphdriver, &graphmode, " ");
}
int main()
{
int i,j;
initgr();
setbkcolor(0);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论