Professional Documents
Culture Documents
Include #Include #Include Int Float Void Float Float Int
Include #Include #Include Int Float Void Float Float Int
include <stdio.h>
#include <graphics.h>
#include <conio.h>
int gd,gm,maxx,maxy;
float xxx[1][2]={0.0,0.0,
};
void line1(float x2,float y2)
{
int d1,d2,d3,d4;
d1=maxx*xxx[0][0];
d2=maxy*xxx[0][1];
d3=maxx*x2;
d4=maxy*y2;
line(d1,d2,d3,d4);
xxx[0][0]=x2;
xxx[0][1]=y2;
}
bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if (n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab = (xxx[0][0]+xb)/2;
yab = (xxx[0][1]+yb)/2;
xbc = (xb+xc)/2;
ybc = (yb+yc)/2;
xcd = (xc+xd)/2;
ycd = (yc+yd)/2;
xabc = (xab+xbc)/2;
yabc = (yab+ybc)/2;
xbcd = (xbc+xcd)/2;
ybcd = (ybc+ycd)/2;
xabcd = (xabc+xbcd)/2;
yabcd = (yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
}
}
void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("CANNOT DETECT A GRAPHICS CARD");
exit(1);
}
initgraph(&gd,&gm,"f:\\tc");
}
main()
{
igraph();
maxx = getmaxx();
maxy = getmaxy();
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(12);
bezier(0.15,0.5,0.25,0.2,0.5,0.5,8);
setfillstyle(1,12);
floodfill(0.45*maxx,0.45*maxy,12);
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(12);
bezier(0.35,0.3,0.6,0,0.5,0.5,4);
setfillstyle(1,12);
floodfill(0.5*maxx,0.45*maxy,12);
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(12);
bezier(0.90,0.45,0.64,0.2,0.5,0.5,4);
setfillstyle(1,12);
floodfill(0.55*maxx,0.45*maxy,12);
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(12);
bezier(0.15,0.6,0.25,0.87,0.5,0.5,4);
setfillstyle(1,12);
floodfill(0.45*maxx,0.55*maxy,12);
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(12);
bezier(0.85,0.75,0.57,0.8,0.5,0.5,4);
setfillstyle(1,12);
floodfill(0.55*maxx,0.55*maxy,12);
xxx[0][0] = 0.5;
xxx[0][1] = 0.5;
setcolor(2);
bezier(0.5,0.75,0.57,0.8,0.5,1,4);
setcolor(14);
circle(0.5*maxx,0.5*maxy,10);
setfillstyle(11,14);
floodfill(0.5*maxx,0.5*maxy,14);
xxx[0][0] = 0.534;
xxx[0][1] = 0.9;
setcolor(2);
bezier(0.85,0.75,0.57,0.78,0.534,0.9,4);
setfillstyle(2,2);
floodfill(0.60*maxx,0.85*maxy,2);
getch();
closegraph();
}
_
Cohensutherland clipping
include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
/* Defining structure for end point of line */
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2,int cl);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm," ");
cleardevice();
printf("\n\n\t\tENTER END-POINT 1 (x,y): ");
scanf("%d,%d",&p1.x,&p1.y);
printf("\n\n\t\tENTER END-POINT 2 (x,y): ");
scanf("%d,%d",&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0: cleardevice(); /* Line conpletely visible */
drawwindow();
drawline(p1,p2,15);
break;
case 1: cleardevice(); /* Line completely invisible */
drawwindow();
break;
case 2: cleardevice(); /* line partly visible */
p1=resetendpt (p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
}
getch();
closegraph();
return(0);
}
/* Function to draw window */
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
/* Function to draw line between two points
---------------------------------------------*/
void drawline (PT p1,PT p2,int cl)
{
setcolor(cl);
line(p1.x,p1.y,p2.x,p2.y);
}
/* Function to set code of the coordinates
--------------------------------------------*/
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; /* TOP */
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; /* BOTTOM */
else
ptemp.code[1]='0';
if (p.x>450)
ptemp.code[2]='1'; /* RIGHT */
else
ptemp.code[2]='0';
if (p.x<150) /* LEFT */
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
/* Function to determine visibility of line
--------------------------------------------*/
int visibility (PT p1,PT p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1'))
flag=0;
}
if(flag==0)
return(1);
return(2);
}
drawpolygon(PT x[20],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(x[i].x,x[i].y,x[i+1].x,x[i+1].y);
}
line(x[i].x,x[i].y,x[0].x,x[0].y);
}_