Draw a cube using display file in computer graphics
Draw A Cube Using Display File
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<string.h>
#define r(a) (a+0.5)
#define size 25
int gdriver=DETECT, gmode;
float df_penx, df_peny, frame_penx, frame_peny, df_op[size], df_x[size], df_y[size];
float hs, he, h, we, ws, w, x, y;
int op, fre, erase_flag=0, x1, y1, n;
void new_frame();
void putpoint(int op, float x, float y);
void display_file(int op);
void move_abs(float x, float y);
void line_abs(float dx, float dy);
void house();
void getpoint(int n);
void domove(float x, float y);
void doline(float x, float y);
void interpret(int s, int count);
void make_pic();
void bresenham(int xa, int ya, int xb, int yb);
float max(float a, float b);
float min(float a, float b);
void erase();
void initialize();
void main()
{
clrscr();
initgraph(&gdriver, &gmode, "C:\\Turboc3\\BGI");
setcolor(WHITE);
initialize();
new_frame();
move_abs(0.4, 0.3);
house();
make_pic();
getch();
}
void initialize()
{
fre=0;
df_penx=0;
df_peny=0;
frame_penx=0;
frame_peny=0;
hs=0;
he=getmaxy();
h=he-hs;
ws=0;
we=getmaxy();
w=we-ws;
}
void move_abs(float x, float y)
{
df_penx=x;
df_peny=y;
display_file(1);
}
void display_file(int op)
{
putpoint(op, df_penx, df_peny);
}
void putpoint(int op, float x, float y)
{
if(fre>size)
printf("Display file full...");
else
{
df_op[fre]=op;
df_x[fre]=x;
df_y[fre]=y;
fre++;
}
}
void house()
{
line_abs(0.8, 0.3);//-
line_abs(0.9, 0.2);// /
line_abs(0.5, 0.2);//-
line_abs(0.4, 0.3);// /
line_abs(0.4, 0.7);// |
line_abs(0.8, 0.7);//-
line_abs(0.8, 0.3);// |
move_abs(0.9, 0.2);
line_abs(0.9, 0.6);// |
line_abs(0.8, 0.7);// /
move_abs(0.9, 0.6);
line_abs(0.5, 0.6);// -
line_abs(0.4, 0.7);
move_abs(0.5, 0.6);
line_abs(0.5, 0.2);
}
void line_abs(float dx, float dy)
{
df_penx=dx;
df_peny=dy;
display_file(2);
}
void make_pic()
{
if(erase_flag==1)
{
erase();
erase_flag=0;
}
if(fre>1)
interpret(0, fre-1);
fre=0;
}
void interpret(int s, int count)
{
for(n=s;n<=count;n++)
{
getpoint(n);
if(op==1)
domove(x, y);
else if(op==2)
doline(x, y);
else
printf("Wrong OPCode...");
}
}
void getpoint(int n)
{
op=df_op[n];
x=df_x[n];
y=df_y[n];
}
void domove(float x, float y)
{
float p1, q1;
p1=min(we, (x*w+ws));
q1=min(he, (y*h+hs));
frame_penx=max(ws, p1);
frame_peny=max(hs, q1);
}
void doline(float x, float y)
{
float p1, q1;
p1=min(we, (x*w+ws));
q1=min(he, (y*h+hs));
x1=frame_penx;
y1=frame_peny;
frame_penx=max(ws, p1);
frame_peny=max(hs, q1);
bresenham(r(x1), r(y1), r(frame_penx), r(frame_peny));
}
float max(float a, float b)
{
if(a>b)
return (a);
else
return (b);
}
float min(float a, float b)
{
if(a<b)
return (a);
else
return (b);
}
void bresenham(int xa, int ya, int xb, int yb)
{
int p, x, y, dx, dy, twodydx, twody, twodxdy, twodx, xend, yend;
float m;
dx=abs(xa-xb);
dy=abs(ya-yb);
m=(float)(yb-ya)/(xb-xa);
if(dx>dy)
{
p=2*dy-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x, y, 3);
while(x<xend)
{
x++;
if(p<0)
p=p+twody;
else
{
if(0<m && m<1)
y++;
else
y--;
p=p+twodydx;
}
delay(10);
putpixel(x, y, 3);
}
}
else
{
p=2*dx-dy;
twodx=2*dx;
twodxdy=2*(dx-dy);
if(ya>yb)
{
x=xb;
y=yb;
yend=ya;
}
else
{
x=xa;
y=ya;
yend=yb;
}
putpixel(x, y, 3);
while(y<yend)
{
y++;
if(p<0)
p=p+twodx;
else
{
if(m>=1)
x++;
else
x--;
p=p+twodxdy;
}
delay(10);
putpixel(x, y, 3);
}
}
}
void new_frame()
{
erase_flag=1;
}
void erase()
{
int j;
for(n=1;n<we+1;n++)
for(j=1;j<he+1;j++)
putpixel(n,j,0);
}
Draw a house using display file in computer graphics
Concept of polygon absolute in computer graphics
Comment your views on this Article :)
No comments
Comment your views on this article