Draw a polygon using polygon absolute concept in computer graphics
Polygon Absolute
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<string.h>
#define size 25
#define r(a) (a+0.5)
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;
float a_x[]={0.5, 0.3, 0.45, 0.3, 0.45, 0.3, 0.45, 0.45, 0.55, 0.55, 0.7, 0.55, 0.7, 0.55, 0.7};
float a_y[]={0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.7, 0.7, 0.5, 0.5, 0.4, 0.4, 0.3, 0.3};
void erase()
{
int j;
for(n=1;n<we+1;n++)
for(j=1;j<he+1;j++)
putpixel(n, j, 0);
}
void new_frame()
{
erase_flag=1;
}
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 putpoint(int op, float x, float y)
{
df_x[fre]=x;
df_y[fre]=y;
if(df_x[fre]==0 || df_y[fre]==0)
{
df_x[fre]=x;
df_y[fre]=y;
}
df_op[fre]=op;
fre++;
}
void display_file(int op)
{
putpoint(op, df_penx, df_peny);
}
void move_rel(float dx, float dy)
{
df_penx=dx;
df_peny=dy;
display_file(2);
}
void move_abs(float a_x[], float a_y[], int j)
{
if(j<3)
printf("It not ploygone...");
else
{
df_penx=a_x[j-1];
df_peny=a_y[j-1];
display_file(j);
for(n=0;n<j;n++)
{
df_penx=a_x[n];
df_peny=a_y[n];
if(df_penx==0 || df_peny==0)
{
df_penx=a_x[n];
df_peny=a_y[n];
}
move_rel(df_penx, df_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);
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));
}
void dopol(float x, float y)
{
domove(x, y);
}
void getpoint(int n)
{
op=df_op[n];
x=df_x[n];
y=df_y[n];
}
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
dopol(x, y);
}
}
void make_pic()
{
if(erase_flag==1)
{
erase();
erase_flag=0;
}
if(fre>1)
interpret(0, fre-1);
fre=0;
}
void main()
{
clrscr();
initgraph(&gdriver, &gmode, "C:\\TurboC3\\BGI");
initialize();
new_frame();
move_abs(a_x, a_y, 15);
make_pic();
getch();
}
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;
}
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;
}
putpixel(x, y, 3);
}
}
}
Draw a cube using display file concept
Comment your views on this Article :)
No comments
Comment your views on this article