You are on page 1of 83

Computer Graphics Lab

File

Index

S.N Program Title


Sameer Makker
o. 356/COE/13
1 To draw a line using DDA approach.
2 To draw a line using midpoint approach.
3 To draw a line using Bressenham
approach.
4 To draw a circle using second order
differential approach.
5 To draw a circle using Bressenham
approach.
6 To draw a ellipse, parabola and hyperbola
using midpoint approach.
7 To draw a ellipse using Bressenham
approach.
8 To draw a parabola using Bressenham
approach.
9 To draw a hyperbola using Bressenham
approach.
10 Line Clipping using:
i. Cohen Sutherland approach
ii. Cyrus Beck algorithm
iii. Midpoint Subdivision approach
iv. Nicholl Lee Nicholl approach
11 Polygon Clipping via Sutherland
Hodgeman algorithm.
12 Polygon Clipping via Weiler Atherton
approach.
13 Polygon filling through Scanline approach.
14 Polygon filling through Seedfill approach.
15 To demonstrate 2D transformations.
16 To demonstrate 3D viewing via cube.
17 To identify visible and non-visible surfaces
of a ccube when kept under a light source

2
356/COE/13
1. To draw a line using DDA approach
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <limits.h>
#define ROUND(a) ((int)(a+0.5))
void DDA(int x1, int y1, int x2, int y2, int color){

int dx = x2-x1 , dy = y2-y1 , steps ,k;


if(abs(dx)>abs(dy)){
steps = abs(dx);
}else{
steps = abs(dy);
}
float xinc,yinc,x=x1,y=y1;
xinc = dx/(float)steps ;
yinc = dy/(float)steps ;
putpixel(x,y,color);
for(k=0;k<steps;k++){
x+=xinc;
y+=yinc;
putpixel(ROUND(x),ROUND(y),color);
}

int main(){
int gd,gm ;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,NULL);
int x1,y1,x2,y2;
DDA(100,100,240,320,GREEN);
getch();
closegraph();
return 0;
}

Output

3
356/COE/13
4
356/COE/13
2. To draw a line using mid point approach
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <limits.h>
void swap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}
void midpointline(int x1,int y1,int x2,int y2,int color)
{
int dx,dy,d,incry,incre,incrne,slopegt1=0;
dx=abs(x1-x2);dy=abs(y1-y2);
if(dy>dx)
{
swap(&x1,&y1);
swap(&x2,&y2);
swap(&dx,&dy);
slopegt1=1;
}
if(x1>x2)
{
swap(&x1,&x2);
swap(&y1,&y2);
}
if(y1>y2)
incry=-1;
else
incry=1;
d=2*dy-dx;
incre=2*dy;
incrne=2*(dy-dx);
while(x1<x2)
{
if(d<=0)
d+=incre;
else
{
d+=incrne;
y1+=incry;
}
x1++;
if(slopegt1)
putpixel(y1,x1,color);
else
putpixel(x1,y1,color);
}
}
int main(){
int gd,gm ;
detectgraph(&gd,&gm);

5
356/COE/13
initgraph(&gd,&gm,NULL);
int x1,y1,x2,y2;
midpointline(100,100,240,320,GREEN);
getch();
closegraph();
return 0;
}

Output

6
356/COE/13
3. To draw a line using Bressenham approach
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <limits.h>

void BresenHam(int x0,int y0,int x1,int y1,int color){


int dx=abs(x1-x0);
int dy=abs(y1-y0);
int x,y;
if(dx>=dy){
int d=2*dy-dx;
int ds=2*dy;
int dt=2*(dy-dx);
if(x0<x1){
x=x0;
y=y0;
}
else{
x=x1;
y=y1;
x1=x0;
y1=y0;
}
putpixel(x,y,color);
while(x<x1){
if(d<0)
d+=ds;
else{
if(y<y1){
y++;
d+=dt;
}
else{
y--;
d+=dt;
}
}
x++;
putpixel(x,y,color);
}
}
else{
int d=2*dx-dy;

7
356/COE/13
int ds=2*dx;
int dt=2*(dx-dy);
if(y0<y1){
x=x0;
y=y0;
}
else{
x=x1;
y=y1;
y1=y0;
x1=x0;
}
putpixel(x,y,color);
while(y<y1){
if(d<0)
d+=ds;
else{
if(x>x1){
x--;
d+=dt;
}
else{
x++;
d+=dt;
}
}
y++;
putpixel(x,y,color);
}
}
}
int main(){
int gd,gm ;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,NULL);
int x1,y1,x2,y2;
BresenHam(100,100,240,320,GREEN);
getch();
closegraph();
return 0;
}

8
356/COE/13
Output

9
356/COE/13
4. To draw a circle using second order differential approach
#include<iostream.h>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
using namespace std;
void plotpoints(int x,int y,int x1,int y1)
{
putpixel(320+(x+x1),240-(y+y1),WHITE);
putpixel(320+(x-x1),240-(y+y1),WHITE);
putpixel(320+(x-x1),240-(y-y1),WHITE);
putpixel(320+(x+x1),240-(y-y1),WHITE);
putpixel(320+(x+y1),240-(y+x1),WHITE);
putpixel(320+(x-y1),240-(y+x1),WHITE);
putpixel(320+(x-y1),240-(y-x1),WHITE);
putpixel(320+(x+y1),240-(y-x1),WHITE);
}
void makecircle(int x,int y,int r)
{
int x1=0;
int y1=r;
int d=1-r;
int de = 3,dse = 5-2*r;
plotpoints(x,y,x1,y1);
while(x1<=y1)
{
if(d>=0)
{
d+=dse;
de+=2; dse+=4;
y1--;
}
else
{
d+=de;
de+=2; dse+=2;
}
x1++;

10
356/COE/13
plotpoints(x,y,x1,y1);
//delay(50);
}
}
int main()
{
int x1,y1,r;
int gdriver = DETECT , gmode ;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
cout<<"enter the coordinates"<<endl;
cin>>x1>>y1;
cin>>r;
makecircle(x1,y1,r);
getch();
closegraph();
return 0;
}

11
356/COE/13
Output

Input File:

12
356/COE/13
13
356/COE/13
5. Circle using Bressenham approach
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
using namespace std;
void plotpoints(int x,int y,int x1,int y1)
{
putpixel((x+x1),(y+y1),WHITE);
putpixel((x-x1),(y+y1),WHITE);
putpixel((x-x1),(y-y1),WHITE);
putpixel((x+x1),(y-y1),WHITE);
putpixel((x+y1),(y+x1),WHITE);
putpixel((x-y1),(y+x1),WHITE);
putpixel((x-y1),(y-x1),WHITE);
putpixel(
(x+y1),(y-x1),WHITE);
}
void makecircle(int xcenter,int ycenter,int r)
{
int x=0;
int y=r;
int p=3-2*r;
plotpoints(xcenter,ycenter,x,y);
while(x<=y)
{
if(p<=0)
{
p+=(2*x+3)*2;
}
else
{
p+=4*(x-y)+10;
y--;
}
x++;
plotpoints(xcenter,ycenter,x,y);
}
}
int main()
{
int x1,y1,r;
int gdriver = DETECT , gmode ;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
cout<<"enter the coordinates"<<endl;
cin>>x1>>y1;
cin>>r;
makecircle(x1,y1,r);
getch();
closegraph();
return 0;
}

14
356/COE/13
Output

Input File

15
356/COE/13
6. To draw a ellipse, parabola and hyperbola using midpoint
approach

Ellipse

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
using namespace std;
#define ROUND(a) ((int)(a+0.5))
void plotpoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);
putpixel(xcenter-x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter-x,ycenter-y,15);
}
void ellipsemid(int xc,int yc,int a,int b)
{
int x=0;
int y=yc;
int px=0;
int py=2*a*a*y;
int p;
int sqa=a*a;
int sqb=b*b;
plotpoints(xc,yc,x,y);
p=((sqb)-(sqa*b)+(0.25*sqa));
while(px<py)
{
x++;
px+=2*b*b;
if(p<0)
{
p+=b*b+px;
}
else
{
y--;
py-=2*a*a;
p+=b*b+px-py;
}
plotpoints(xc,yc,x,y);
}
p=(((sqb*(x+0.5))*(x+0.5))+(sqa*(y-1)*(y-1))-(a*a*b*b));
while(y>0)
{
y--;
py-=2*a*a;
if(p>0)

16
356/COE/13
{
p+=a*a-py;
}
else
{
x++;
px+=2*b*b;
p+=a*a-py+px;
}
plotpoints(xc,yc,x,y);
}
}
int main()
{
int x,y,a,b;
int gdriver = DETECT , gmode ;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
cout<<"enter the coordinates"<<endl;
cin>>x>>y;
cin>>a>>b;
ellipsemid(x,y,a,b);
getch();
closegraph();
return 0;
}
Output

Input

17
356/COE/13
Parabola

#include<iostream>
#include<graphics.h>
#include<stdio.h>
using namespace std;
void put_pixel(int x, int y)
{
putpixel((x+300),(240-y),15);
putpixel((x+300),(240+y),15);
}
void para(int cx, int cy, double a)
{
setcolor(BLUE);
line(300,0,300,479);
setcolor(RED);
line(0,240,639,240);
double x=0,y=0;
double d1;
d1 = (2*a) - 1;
put_pixel(x,y);
while(y<= (2*a*1.0))
{
if(d1<0)
{
d1+= 4*a-3-2*y;
x++;
y++;
}
else
{
d1-= 3 + 2*y;
y++;
}
put_pixel(x,y)

18
356/COE/13
;
}
d1 = (4.0*a*(x+1) - (y+0.5)*(y+0.5) );
while( y < 220 )
{
if(d1<0)
{
d1+= 4*a;
x++;
}
else
{
d1+= 4.0*a - 2 - 2.0*y ;
x++;
y++;
}
put_pixel(x,y);
}
}
int main(){
double a;
cout<<"Enter a : ";
cin>>a;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
para(0,0,a);
getch();
closegraph();
return 0;
}

Output

Hyperbola

#include<iostream>
#include<graphics.h>
#include<stdio.h>
using namespace std;

19
356/COE/13
void put_pixel(int x, int y, int col)
{
putpixel(x+320,240-y,col);
}
void drawaxes()
{
setcolor(RED);
line(320,0,320,480);
line(0,240,640,240);
setcolor(WHITE);
}
void plotpoints(int x,int y,int x1,int y1)
{
put_pixel((x+x1),(y+y1),15);
put_pixel((x-x1),(y+y1),15);
put_pixel((x-x1),(y-y1),15);
put_pixel((x+x1),(y-y1),15);
}
void makehyperbola(int cx,int cy,int a,int b)
{
double d=b*b+4*a*b*b-4*a*a;
double x=a;
double y=0;
plotpoints(cx,cy,x,y);
while((b*b*x)>=(a*a*y))
{
if(d>0)
d+=(-8*y*a*a-12*a*a);
else
d+=8*b*b+8*b*b*x,x++;
y++;
plotpoints(cx,cy,x,y);
}
d=4*b*b*x*x+4*b*b+8*b*b*x-4*a*a*y*y-a*a-4*a*a*y-4*a*a*b*b;
while((b*b*x)<=(a*a*y))
{
if(d<=0)
d+=-8*a*a-8*y*a*a+12*b*b+8*x*b*b,y++;
else
d+=12*b*b+8*x*b*b;
x++;
plotpoints(cx,cy,x,y);
}
}
int main(void)
{
int x,y,a,b;
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
cout<<"Enter the Centre and a and b of Hyperbola\n";
cin>>x>>y>>a>>b;
drawaxes();
makehyperbola(x,y,a,b);
getch();

20
356/COE/13
closegraph();
return 0;
}

Output

Input

21
356/COE/13
7. Ellipse using Bressenham approach
#include<graphics.h>
#include<stdio.h>
void plotellipse(int xc,int yc,int a, int b)
{
int d=b*b-a*a*b+(a*a)/4.0;
int x,y;
x=0;
y=b;
int deln=3*b*b;
int delnw=3*b*b+2*a*a*(1-b);
while(b*b*x<=a*a*y)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(320-x+xc, 240+y+yc, RED);
putpixel(x+320+xc, 240-y+yc, RED);
putpixel(320-x+xc, 240-y+yc, RED);
if(d<0)
{
d+=deln;
deln+=2*b*b;
delnw+=2*b*b;
}

22
356/COE/13
else{
d+=delnw;
deln+=2*b*b;
delnw+=2*(b*b+a*a);
y--;
}
x++;
}
d=b*b*x*(1+x)+a*a*y*(y-2)+a*a+(b*b)/4.0-a*a*b*b;
deln=a*a*(3-2*y);
delnw=deln+2*b*b*(x+1);
while(y>0)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(320-x+xc, 240+y+yc, RED);
putpixel(x+320+xc, 240-y+yc, RED);
putpixel(320-x+xc, 240-y+yc, RED);
if(d>0)
{
d+=deln;
deln+=2*a*a;
delnw+=2*a*a;
}
else
{
d+=delnw;
deln+=2*a*a;
delnw+=2*(a*a+b*b);
x++;
}
y--;
}

}
int main()
{
int gd = DETECT, gm, xc, yc, a,b;
printf("Enter centre of circle x & y\n");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of circle x2 & y2\n");
scanf("%d %d", &a,&b);
initgraph(&gd, &gm, "SATVIK MAHAJAN");
plotellipse(xc,yc,a,b);
getch();
closegraph();
}
Output

23
356/COE/13
8. Parabola using Bressenham approach
#include<graphics.h>
#include<stdio.h>
void plotparabolax(int xc,int yc,int a)
{
int d=2-4*a;
int x,y;
x=0;
y=0;
while(y<2*a)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(x+320+xc, 240-y+yc, RED);
if(d<=0)
d+=4*y+6;
else
{
d+=4*y+6-8*a;
x++;
}
y++;

24
356/COE/13
}
while(x<480)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(x+320+xc, 240-y+yc, RED);
if(d<=0)
d+=8*a;
else
{
d+=8*a-4*(y+1);
y++;
}
x++;
}
}
void plotparabolay(int xc,int yc,int a)
{
int d=2-4*a;
int x,y;
x=0;
y=0;
while(x<2*a)
{
putpixel(320-x+xc, 240-y+yc, RED);
putpixel(320+x+xc, 240-y+yc, RED);
if(d<=0)
d+=4*x+6;
else
{
d+=4*x+6-8*a;
y++;
}
x++;
}
while(y<480)
{
putpixel(320-x+xc, 240-y+yc, RED);
putpixel(320+x+xc, 240-y+yc, RED);
if(d<=0)
d+=8*a;
else
{
d+=8*a-4*(x+1);
x++;
}
y++;
}
}
int main()
{
int gd = DETECT, gm, xc, yc,r;
printf("Enter centre of circle x & y\n");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of circle\n");

25
356/COE/13
scanf("%d", &r);
initgraph(&gd, &gm, "SATVIK MAHAJAN");
plotparabolax(xc,yc,r);
plotparabolay(xc,yc,r);
getch();
closegraph();
}

Output

26
356/COE/13
9. Hyperbola using Bressenham approach
#include<graphics.h>
#include<stdio.h>
void plothyperbola(int xc,int yc,int a,int b)
{
int d=2*a*a-b*b*(1+2*a);
int x,y;
x=a;
y=0;
while(b*b*x>a*a*y)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(320-x+xc, 240+y+yc, RED);
putpixel(x+320+xc, 240-y+yc, RED);
putpixel(320-x+xc, 240-y+yc, RED);
if(d<=-b*b/2)
d+=2*a*a*(2*y+3);
else
{
d+=2*a*a*(2*y+3)-4*b*b*(x+1);
x++;
}
y++;
}
while(x<=320)
{
putpixel(x+320+xc,240+y+yc,RED);
putpixel(320-x+xc, 240+y+yc, RED);
putpixel(x+320+xc, 240-y+yc, RED);
putpixel(320-x+xc, 240-y+yc, RED);
if(d>a*a/2)
d-=2*b*b*(2*x+3);
else
{
d+=4*a*a*(y+1)-2*b*b*(2*x+3);
y++;
}
x++;
}
}
int main()
{

27
356/COE/13
int gd = DETECT, gm, xc, yc, a,b;
printf("Enter centre of circle x & y\n");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of circle x2 & y2\n");
scanf("%d %d", &a,&b);
initgraph(&gd, &gm, "SATVIK MAHAJAN");
plothyperbola(xc,yc,a,b);
getch();
closegraph();
}

Output

28
356/COE/13
10. Line Clipping using
Cohen Sutherland Approach

#include<iostream>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
using namespace std;
#define LEFT 0x01
#define RIGHT 0x4
#define BOTTOM 0x2
#define TOP 0x8
char getcode(float x, float y, float xwmin, float ywmin, float xwmax, float ywmax)
{
unsigned char code = 0x00;
if(x<xwmin)
code = code|LEFT;
if(x>xwmax)
code = code|RIGHT;
if(y>ywmin)
code = code|BOTTOM;
if(y<ywmax)
code = code|TOP;
return code;
}
void lin(float x1, float y1, float x2, float y2, float xwmin, float ywmin, float
xwmax, int ywmax)
{
int done = 0, accept = 0;
unsigned char code1, code2;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
setcolor(BLUE);
line(300,0,300,479);
setcolor(RED);
line(0,240,639,240);
setcolor(YELLOW);
rectangle(xwmin, ywmin, xwmax, ywmax);
setcolor(GREEN);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
float m;
while(done==0)

29
356/COE/13
{
code1 = getcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
code2 = getcode(x2,y2,xwmin,ywmin,xwmax,ywmax);
/* case I - accept line */
if(((code1&code2)==0) && ((code1|code2)==0))
{
accept = 1;
done = 1;
}
else if((code1&code2)!=0)
{
done = 1;
outtextxy(10,300,"\n Sorry! Line rejected");
}
else
{
if((x1>= xwmin && x1<= xwmax) && (y1>= ywmax && y1<=ywmin))
{
float temp = x1;
x1 = x2;
x2=temp;
temp = y1;
y1=y2;
y2=temp;
char t;
t=code1;
code1=code2;
code2=t;
}
if(x1!=x2)
m = (y2-y1)/(x2-x1);

if( code1 & LEFT != 0)


{
y1+= (xwmin-x1)*m;
x1 = xwmin;
}
else if(code1 & RIGHT)
{
y1+= (xwmax-x1)*m;
x1 = xwmax;
}
else if(code1 & BOTTOM)
{
if(x2!=x1)
x1+= (ywmin - y1)/m;
y1 = ywmin;
}
else
{
if(x2!=x1)
x1+= (ywmax-y1)/m;
y1 = ywmax;
}

30
356/COE/13
}
}
if(accept == 1)
line(x1,y1,x2,y2);
}
int main()
{
int gdriver = DETECT, gmode;
float xwmin, xwmax, ywmin, ywmax;
cout<<"Enter the x limits for the clipping window : ";
cin>>xwmin>>xwmax;
cout<<"\nEnter the y limits for the clipping window :";
cin>>ywmin>>ywmax;
cout<<"\nEnter end point 1 : ";
float x1,y1,x2,y2;
cin>>x1>>y1;
cout<<"\nEnter end point 2 : ";
cin>>x2>>y2;
lin(x1+300,240-y1,x2+300,240-y2,xwmin+300,240-ywmin,xwmax+300,240-
ywmax);
// i have interchanged ywmin and ywmax here beacuse we are doing 240-y
getch();
closegraph();
return 0;
}

Output

Cyrus Beck Approach

#include<iostream>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>

31
356/COE/13
#include<dos.h>
using namespace std;
int clipw[100][2],normals[100][2],linesc[100][2][2],nr[100][100],dr[100][100];
double t[100][100];
int n,m;
void clipwindow()
{
cout<<"Enter the Number of Vertices of the Clipping Window\n";
cin>>n;
cout<<"Enter the Coordinates of Vertices of Clipping Window (in Anticlockwise
Fashion)\n";
for(int i=0;i<n;i++)
cin>>clipw[i][0]>>clipw[i][1];
for(int i=0;i<n;i++)
line(320+clipw[i][0],240-clipw[i][1],320+clipw[(i+1)%n][0],240-
clipw[(i+1)%n][1]);
}
void findnormals()
{
for(int i=0;i<n;i++)
{
double x=clipw[(i+1)%n][0]-clipw[i][0];
double y=clipw[(i+1)%n][1]-clipw[i][1];
normals[i][0]=y;
normals[i][1]=-x;
}
}
void lines()
{
cout<<"Enter the Number of lines to be clipped\n";
cin>>m;
cout<<"Enter the Coordinates of the Lines in (x,y) format\n";
for(int i=0;i<m;i++)
cin>>linesc[i][0][0]>>linesc[i][0][1]>>linesc[i][1][0]>>linesc[i][1][1];
}
void findintersections()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
int temp[2],temp2[2];
temp[0]=linesc[i][0][0]-clipw[j][0];
temp[1]=linesc[i][0][1]-clipw[j][1];
temp2[0]=linesc[i][1][0]-linesc[i][0][0];
temp2[1]=linesc[i][1][1]-linesc[i][0][1];
nr[i][j]=normals[j][0]*temp[0]+normals[j][1]*temp[1];
dr[i][j]=normals[j][0]*temp2[0]+normals[j][1]*temp2[1];
if(dr[i][j]!=0)t[i][j]=nr[i][j]/(double)((-1.0)*dr[i][j]);
}
}
}
void plotclippedlines()
{

32
356/COE/13
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(dr[i][j]<0&&dr[i][(j+1)%n]>0)
{
setcolor(12);
double x=linesc[i][0][0]+(linesc[i][1][0]-linesc[i][0][0])*t[i][j];
double y=linesc[i][0][1]+(linesc[i][1][1]-linesc[i][0][1])*t[i][j];
double _x=linesc[i][0][0]+(linesc[i][1][0]-linesc[i][0][0])*t[i][(j+1)%n];
double _y=linesc[i][0][1]+(linesc[i][1][1]-linesc[i][0][1])*t[i][(j+1)%n];
line(320+x,240-y,320+_x,240-_y);
setcolor(15);
}
}
}
}
int main(void)
{
int x,y,n1,x1,y1,x2,y2;
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
clipwindow();
findnormals();
lines();
findintersections();
plotclippedlines();
getch();
closegraph();
return 0;
}

Output

Midpoint Subdivision Approach

#include<iostream.h>

33
356/COE/13
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define LEFT 0x01
#define RIGHT 0x4
#define BOTTOM 0x2
#define TOP 0x8
struct point
{
float x,y;
};
char getcode(point p, point wmin, point wmax)
{
unsigned char code = 0x00;
if(p.x<wmin.x)
code = code|LEFT;
if(p.x>wmax.x)
code = code|RIGHT;
if(p.y>wmax.y)
code = code|BOTTOM;
if(p.y<wmin.y)
code = code|TOP;
return code;
}
int isin(point p, point wmin, point wmax)
{
char stcode =0x00;
char cod = getcode(p,wmin,wmax);
if(((cod&stcode)==0) && ((cod|stcode)==0))
return 1;
else
return 0;
}
point mid( point p1, point p2)
{
point m;
m.x = (p1.x+p2.x)/2; // calculate mid point of the line segment
m.y = (p1.y+p2.y)/2;
return m;
}
float dist( point t1, point t2, point wmin, point wmax)
{
float dsx = t1.x - t2.x;
float dsy = t1.y - t2.y;
/*float ds;
ds = sqrt((dsx*dsx) + (dsy*dsy));
return ds;*/
float ds = dsx>dsy?dsx:dsy;
return ds;
}
void midpt(point wmin, point wmax, point p1, point p2)
{
setcolor(RED);
point t;

34
356/COE/13
if (p1.x > p2.x)
{
t = p1;
p1 = p2;
p2 = t;
}
char code1 = getcode(p1,wmin,wmax);
char code2 = getcode(p2,wmin,wmax);
if((code1 & code2)!=0)
{
return;
}
else if(((code1 & code2)==0) && ((code1 | code2)==0))
{
line(p1.x, p1.y, p2.x, p2.y);
}
else
{
t = mid(p1,p2);
if((dist(t,p1,wmin,wmax) >= 1) || (dist(t,p2,wmin,wmax) >= 1))
{
char codm = getcode(t, wmin, wmax);
if(isin(t,wmin,wmax))
{
if(isin(p1,wmin,wmax))
{
line(p1.x,p1.y,t.x,t.y);
p1=t;
midpt(wmin,wmax,p1,p2);
}
else if(isin(p2,wmin,wmax))
{
line(p2.x,p2.y,t.x,t.y);
p2=t;
midpt(wmin,wmax,p1,p2);
}
else
{
midpt(wmin,wmax,p1,t);
midpt(wmin,wmax,t,p2);
}
}
else
{
if ((code1&codm)!=0)
{
p1=t;
}
else if((code2&codm)!=0)
{
p2=t;
}
midpt(wmin,wmax,p1,p2);
}

35
356/COE/13
}
}
}
int main()
{
point wmin,wmax,p1,p2;
cout<<" Enter the clipping window limits : Xwmin ";
cin>>wmin.x;
cout<<" Enter the clipping window limits : Ywmin ";
cin>>wmin.y;
cout<<" Enter the clipping window limits : Xwmax ";
cin>>wmax.x;
cout<<" Enter the clipping window limits : Ywmax ";
cin>>wmax.y;
cout<<"\n Enter the end points of the line : P1->x ";
cin>>p1.x;
cout<<"\n Enter the end points of the line : P1->y ";
cin>>p1.y;
cout<<"\n Enter the end points of the line : P2->x ";
cin>>p2.x;
cout<<"\n Enter the end points of the line : P2->y ";
cin>>p2.y;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(GREEN);
rectangle(wmin.x,wmin.y,wmax.x,wmax.y);
setcolor(CYAN);
line(p1.x,p1.y,p2.x,p2.y);
getch();
midpt(wmin,wmax,p1,p2);
getch();
closegraph();
return 0;
}

Output

36
356/COE/13
Input File

Nicholl Lee Nicholl Approach

#include<conio.h>

37
356/COE/13
#include<graphics.h>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
int xmin,ymin,xmax,ymax,a,b;
int first_end_point_region(int x,int y);
int findRegionP1(int,int);
void clipline1(int,int,int,int);
void clipline2(int,int,int,int);
void clipline3(int,int,int,int);
int main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
int ch;
float m;
cout<<"\nEnter the xmin:->";
cin>>xmin;
cout<<"\nEnter the ymin:->";
cin>>ymin;
cout<<"\nEnter the xmax:->";
cin>>xmax;
cout<<"\nEnter the ymax:->";
cin>>ymax;
cout<<"Enter the x1:->";
cin>>x1;
cout<<"Enter the y1:->";
cin>>y1;
cout<<"Enter the x2:->";
cin>>x2;
cout<<"Enter the y2:->";
cin>>y2;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(12);
a=getmaxx()/2;
b=getmaxy()/2;
line(0,b,2*a,b);
line(a,0,a,2*b);
rectangle(a+xmin,b-ymin,a+xmax,b-ymax);
setcolor(10);
line(a+x1,b-y1,a+xmin,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymax);
line(a+x1,b-y1,a+xmin,b-ymax);
getch();
setcolor(12);
line(0,b,2*a,b);
line(a,0,a,2*b);
setcolor(3);
line(a+x1,b-y1,a+x2,b-y2);
getch();
ch=first_end_point_region(x1,y1);
switch(ch)
{

38
356/COE/13
case 1 : clipline1(x1,y1,x2,y2);
break;
case 2 : clipline2(x1,y1,x2,y2);
break;
case 3 : clipline3(x1,y1,x2,y2);
break;
default: cout<<"\nInvalid Input: ";
};
getch();
return 0;
}
int first_end_point_region(int x,int y)
{
if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
return 1;
else
if(x<xmin && y>=ymin && y<=ymax)
return 2;
else
if(x<=xmin && y<=ymin)
return 3;
else
return 0;
}

/* point p1 is inside the clip window */


void clipline1(int x1,int y1,int x2,int y2)
{ int draw=1;
float m,m1,m2,m3,m4;
int nx1,ny1,nx2,ny2;
/* calculate slopes for all the lines passing thru vertices
and including the input line :- */
m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmin-x1);
m2=((float)(ymin-y1))/(xmax-x1);
m3=((float)(ymax-y1))/(xmax-x1);
m4=((float)(ymax-y1))/(xmin-x1);
nx1=x1;
ny1=y1;
// point p2 is in "below" region
if(((abs(m)>=m1 && x2<x1) || (abs(m)>abs(m2) && x2>x1)) && y1>y2)
{ cout<<"working"; getch();
// point p2 is also inside clip window
if(y2>ymin)
{
nx2=x2;
ny2=y2;
}
// point p2 is outside clip window
else
{
ny2=ymin;
nx2=x1+(ymin-y1)/m;
}

39
356/COE/13
}
// point p2 is on right side of clip window
else if(m>m2 && m<m3 && x2>=x1)
{ // point p2 is inside clip window
if(x2<xmax)
{
nx2=x2;
ny2=y2;
}
// point p2 is outside clip window
else
{
nx2=xmax;
ny2=y1+(xmax-x1)*m;
}
}
// point p2 is on bottom side of clip window
else if((abs(m)>=m3 && x2>x1) || (abs(m)>abs(m4) && x2<x1))
{ // point p2 is inside clip window
if(y2<ymax)
{
nx2=x2;
ny2=y2;
}
// point p2 is outside clip window
else
{
ny2=ymax;
nx2=x1+(ymax-y1)/m;
}
}
// point p2 is on left side of clip window
else if(m>m4 && m<m1)
{ // point p2 is inside the clip window
if(x2>xmin)
{
nx2=x2;
ny2=y2;
}
// point p2 is outside the clip window
else
{
nx2=xmin;
ny2=y1+(xmin-x1)*m;
}
}
getch();
setcolor(12);
rectangle(a+xmin,b-ymin,a+xmax,b-ymax);
if(draw)
{
setcolor(10);
line(a+x1,b-y1,a+xmin,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymin);

40
356/COE/13
line(a+x1,b-y1,a+xmax,b-ymax);
line(a+x1,b-y1,a+xmin,b-ymax);
setcolor(5);
line(a+nx1,b-ny1,a+nx2,b-ny2);
}
}

/* Point p1 is in the edge region */


void clipline2(int x1,int y1,int x2,int y2)
{ int draw=1;
float m,m1,m2,m3,m4;
int nx1,ny1,nx2,ny2;
m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmin-x1);
m2=((float)(ymin-y1))/(xmax-x1);
m3=((float)(ymax-y1))/(xmax-x1);
m4=((float)(ymax-y1))/(xmin-x1);
// Point p2 is in Left-bottom region
if(m>m1 && m<m2 && x2>xmin)
{ // Point p2 is inside the clip window
if(y2>ymin)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;
}
// Point p2 is outside the clip window
else
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=ymin;
nx2=x1+(ymin-y1)/m;
}
}
// Point p2 is in Left-Right region
else if(m>m2 && m<m3 && x2>xmin)
{ // Point p2 is inside the clip window
if(x2<xmax)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;
}
// Point p2 is outside the clip window
else
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=xmax;
ny2=y1+(xmax-x1)*m;
}

41
356/COE/13
}
// Point p2 is in Left-top region
else if(m>m3 && m<m4 && x2>xmin)
{ // Point p2 is inside the clip window
if(y2<ymax)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;
}
// Point p2 is outside the clip window
else
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=ymax;
nx2=x1+(ymax-y1)/m;
}
}
else
draw=0;
setcolor(12);
rectangle(a+xmin,b-ymin,a+xmax,b-ymax);
if(draw)
{
setcolor(10);
line(a+x1,b-y1,a+xmin,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymax);
line(a+x1,b-y1,a+xmin,b-ymax);
setcolor(5);
line(a+nx1,b-ny1,a+nx2,b-ny2);
}
}

/* Point p1 is in the Corner Region */


void clipline3(int x1,int y1,int x2,int y2)
{
int draw=1;
float m,m1,m2,m3,m4,tm1,tm2;
int nx1,ny1,nx2,ny2;
int flag,t;
tm1=((float)(ymin-y1))/(xmin-x1);
tm2=((float)(ymax-ymin))/(xmax-xmin); //diagonal slope
m=((float)(y2-y1))/(x2-x1);
m1=((float)(ymin-y1))/(xmax-x1);
m2=((float)(ymax-y1))/(xmax-x1);
m3=((float)(ymin-y1))/(xmin-x1);
m4=((float)(ymax-y1))/(xmin-x1);
// Point p1 is towards the left side of the clip window (case2)
if(tm1<tm2)
{
flag=2;

42
356/COE/13
t=m2;
m2=m3;
m3=t;
}
// Point p1 is towards the top side of the clip window (case1)
else
flag=1;

// Point p2 is in the bottom-Right region


if(m>m1 && m<m2)
{
// Point p2 is outside the clip window
if(x2>xmax && y2>ymin)
{
ny1=ymin;
nx1=x1+(ymin-y1)/m;
nx2=xmax;
ny2=y1+m*(xmax-x1);
}
// Point p2 is inside the clip window
else if(y2>ymin && x2<xmax)
{
ny1=ymin;
nx1=x1+(ymin-y1)/m;
ny2=y2;
nx2=x2;
}
}
// Point p2 is Left-Right or Top-Bottom region
else if(m>m2 && m<m3)
{
// Point p2 is in Top-Bottom region (case1)
if(flag==1)
{
// Point p2 is outside the clip window
if(y2>=ymax)
{
ny1=ymin;
nx1=x1+(ymin-y1)/m;
nx2=x1+(ymax-y1)/m;
ny2=ymax;
}
// Point p2 is inside the clip window
else if(y2>=ymin)
{
ny1=ymin;
nx1=x1+(ymin-y1)/m;
nx2=x2;
ny2=y2;
}
}
// Point p2 is in Left-Right region (case2)
else
{

43
356/COE/13
// Point p2 is outside the clip window
if(x2>=xmax)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=xmax;
ny2=y1+m*(xmax-x1);
}
// Point p2 is inside the clip window
else if(x2>=xmin)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x2;
ny2=y2;
}
}
}
// Point p2 is in Left-top region
else if(m>m3 && m<m4)
{
// Point p2 is outside the clip window
if(y2>=ymax)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
nx2=x1+(ymax-y1)/m;
ny2=ymax;
}
// Point p2 is inside the clip window
else if(y2>=ymin)
{
nx1=xmin;
ny1=y1+m*(xmin-x1);
ny2=y2;
nx2=x2;
}
}
else
draw=0;
getch();
setcolor(12);
rectangle(a+xmin,b-ymin,a+xmax,b-ymax);
if(draw)
{
setcolor(10);
line(a+x1,b-y1,a+xmin,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymin);
line(a+x1,b-y1,a+xmax,b-ymax);
line(a+x1,b-y1,a+xmin,b-ymax);
setcolor(5);
line(a+nx1,b-ny1,a+nx2,b-ny2);
}
}

44
356/COE/13
Output

45
356/COE/13
11. Polygon Clipping via Sutherland Hodgeman algorithm
#include<iostream>
#include<conio.h>
#include<graphics.h>
using namespace std;
struct point
{
int x,y;
};
int createlist(int i, point cl[], int nc, point s[], int ns)
{
point t[20];
int k=0;
if(i==0) // TOP EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].y<cl[0].y && s[j+1].y>cl[0].y)
{
//find point of intersection
int ax = int(((cl[0].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[0].y;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].y>cl[0].y && s[j+1].y<cl[0].y)
{
int ax = int(((cl[0].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[0].y;
k++;
}
//i -> i
else if(s[j].y>cl[0].y && s[j+1].y>cl[0].y)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
else if(i==1) // RIGHT EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].x>cl[1].x && s[j+1].x<cl[1].x)
{
//find point of intersection

46
356/COE/13
int ay = int(((cl[1].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[1].x;
t[k].y = ay;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].x<cl[1].x && s[j+1].x>cl[1].x)
{
int ay = int(((cl[1].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[1].x;
t[k].y = ay;
k++;
}
//i -> i
else if(s[j].x<cl[1].x && s[j+1].x<cl[1].x)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
else if(i==2) // BOTTOM EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].y>cl[2].y && s[j+1].y<cl[2].y)
{
//find point of intersection
int ax = int(((cl[2].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[2].y;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].y<cl[2].y && s[j+1].y>cl[2].y)
{
int ax = int(((cl[2].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[2].y;
k++;
}
//i -> i
else if(s[j].y<cl[2].y && s[j+1].y<cl[2].y)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing

47
356/COE/13
}
}
else if(i==3) // LEFT EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].x<cl[0].x && s[j+1].x>cl[0].x)
{
//find point of intersection
int ay = int(((cl[0].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[0].x;
t[k].y = ay;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].x>cl[0].x && s[j+1].x<cl[0].x)
{
int ay = int(((cl[0].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[0].x;
t[k].y = ay;
k++;
}
//i -> i
else if(s[j].x>cl[0].x && s[j+1].x>cl[0].x)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
t[k]=t[0];

for(int l=0;l<=k;l++)
{
s[l]=t[l];
}

return k;
}
/*int is_out(point p, point cl[], int nc)
{
// 1. I have taken clockwise orientation positive
// 2. So any point is inside the polygon if it is to the left of every edge
// 3. Otherwise it is outside.
// 4. Let edge be p1p2(p1 & p2 in clockwise order). Let point be P.
// therefore, if slope(p1p2) > slope(p1P), for every edge,
// then the point is inside the polygon
int in = 0;
int out = 0;
int i=0;

48
356/COE/13
while (i<nc && out==0)
{
dec = ((c[i+1].y - c[i].y) * (p.x - c[i].x)) - ((p.y - c[i].y) * (c[i+1].x - c[i].x));
if(dec < 0)
out =1;
i++;
}
return out;
if((p.x >= cl[0].x)&&(p.x <= cl[1].x)&&(p.y >= cl[0].y)&&(p.y <= cl[2].y))
return 0;
else
return 1;
} */
void suthodg( point cl[], int nc, point s[], int ns)
{
for(int i=0;i<nc;i++)
{
ns = createlist(i, cl, nc, s, ns);
}
setcolor(GREEN);
for(int i=0;i<ns;i++)
{
line(s[i].x,s[i].y,s[i+1].x,s[i+1].y);
}
}
int main()
{
//clrscr();
point cl[4];
point sub[10], dup_sub[10];
int nc;
cout<<" Clipping Polygon ";
//min is top left and max is bottom right
cout<<"\n Enter the co-ordinates (x,y) ";
cout<<"\n Xwmin : ";
cin>>cl[0].x;
cout<<"\n Ywmin : ";
cin>>cl[0].y;
cout<<"\n Xwmax : ";
cin>>cl[2].x;
cout<<"\n Ywmax : ";
cin>>cl[2].y;
cl[1].x = cl[2].x;
cl[1].y = cl[0].y;
cl[3].x = cl[0].x;
cl[3].y = cl[2].y;
int ns;
cout<<" Subject Polygon ";
do
{
cout<<"\n Enter the no. of vertices : ";
cin>>ns;
}while(ns>10);
cout<<"\n Enter the co-ordinates in clockwise order (x,y) ";

49
356/COE/13
int i;
for(i=0;i<ns;i++)
{
cout <<i+1<<". ";
cin>>sub[i].x>>sub[i].y;
dup_sub[i] = sub[i];
}
sub[i] = sub[0];
dup_sub[i] = sub[0];
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(RED);
for(i=0;i<3;i++)
{
line(cl[i].x, cl[i].y, cl[i+1].x, cl[i+1].y);
}
line( cl[3].x, cl[3].y, cl[0].x, cl[0].y);
setcolor(YELLOW);
for(i=0;i<ns;i++)
{
line(sub[i].x,sub[i].y,sub[i+1].x,sub[i+1].y);
}
getch();
suthodg(cl,4,dup_sub,ns);
getch();
closegraph();
}

50
356/COE/13
Output

Input File

51
356/COE/13
12. Polygon Clipping via Weiler Atherton algorithm
#include<graphics.h>
#include<bits/stdc++.h>
using namespace std;
float sdx[15],sdy[15];
int i,w=0,h;
void sort(float sdy[],int h)
{
float temp;
for(int j=0;j<=h-1;j++)
{
for(i=0;i<h-1-j;i++)
{
if(sdy[i]>sdy[i+1])
{
temp=sdy[i];
sdy[i]=sdy[i+1];
sdy[i+1]=temp;
}
}
}
}
struct ather
{
float x;
float y;
float io;
float vis;
};
struct ather z[20];
int main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int n,m,s;
float px[15]={0};
float py[15]={0};
float pdx[15],pdy[10];
float outx[15]={0};
float outy[15]={0};
float xmin,ymin,xmax,ymax;
printf("enter xmin,ymin,xmax,ymax");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
rectangle(320+xmin,240-ymax,320+xmax,240-ymin);

52
356/COE/13
printf("enter the no. of vertices (n)");
scanf("%d",&n);
printf("enter the x coordinate of all vertices");
for(m=0;m<n;m++)
{
scanf("%f",&px[m]);
}
printf("enter the y coordinate of all vertices");
for(m=0;m<n;m++)
{
scanf("%f",&py[m]);
}
rectangle(320+xmin,240-ymax,320+xmax,240-ymin);
px[n]=px[0];py[n]=py[0];
for(s=0;s<n;s++)
{
line(320+px[s],240-py[s],320+px[s+1],240-py[s+1]);
}
getch();
px[n]=px[0];
py[n]=py[0]; int l=0;
for(m=0;m<n;m++)
{
if(px[m]>=xmin && px[m+1]<=xmin)
{
pdx[m]=xmin;
pdy[m]=py[m]+((py[m+1]-py[m])/(px[m+1]-px[m]))*(xmin-px[m]);
outx[l]=pdx[m];outy[l]=pdy[m];
z[l].io=1;
l++;
}
if(px[m]>=xmin && px[m+1]>=xmin)
{
outx[l]=px[m+1];outy[l]=py[m+1];
z[l].io=0;
l++;
}
if(px[m]<=xmin && px[m+1]>=xmin)
{
pdx[m]=xmin;
pdy[m]=py[m]+((py[m+1]-py[m])/(px[m+1]-px[m]))*(xmin-px[m]);
outx[l]=pdx[m];outy[l]=pdy[m];
z[l].io=0;
l++;
outx[l]=px[m+1];outy[l]=py[m+1];
z[l].io=0;
l++;
}
}
outx[l]=outx[0];outy[l]=outy[0];
setcolor(GREEN);
for(i=0;i<l;i++)
{
if(outx[i]==xmin)

53
356/COE/13
{
sdx[w]=outx[i];
sdy[w]=outy[i];
w++;
}
}
sort(sdy,w);
outx[l]=outx[0];outy[l]=outy[0];
for(i=0;i<=l;i++)
{
z[i].x=outx[i];
z[i].y=outy[i];
z[i].vis=0;
}
s=0;
for(m=0;m<=l-1;m++)
{
outx[l]=outx[0];outy[l]=outy[0];
sdx[w+1]=sdx[0];sdy[w+1]=sdy[0];
if(z[s].io==0)
{
line(320+outx[s],240-outy[s],320+outx[s+1],240-outy[s+1]);
z[s].vis=1;
z[s+l].vis=1;
}
else if(z[s].io==1)
{
for(i=0;i<=w;i++)
{
if(sdy[i]==outy[s])
{
line(320+sdx[i],240-sdy[i],320+sdx[i+1],240-sdy[i+1]);
z[s].vis=1;
z[s+l].vis=1;
break;
}
}
for(int j=0;j<l;j++)
{
if(sdy[i+1]==z[j].y)
{
s=j;
line(320+outx[s],240-outy[s],320+outx[s+1],240-outy[s+1]);
z[s].vis=1;
z[s+l].vis=1;
break;
}
}
}
if(s<=l-1)
{
s++;
}
else

54
356/COE/13
{
s=0;
}
if(s==l)
{
s=0;
}
int p=s;
while(z[s].vis == 1)
{
s++;
if(s==p+l)
{
break;
}
}
}
getch();
return 0;
}

Output

13. Polygon filling through Scanline approach.

55
356/COE/13
#include<bits/stdc++.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main()
{
int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];
//clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
/*- draw polygon -*/
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
getch();
for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}
for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)
{
if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/

56
356/COE/13
for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(5);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
return 0;
}
Output

57
356/COE/13
14. Polygon filling through Seedfill approach
#include <graphics.h>
#include<bits/stdc++.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <iostream>
using namespace std;
typedef pair<int,int> mp;
struct point
{
double x;
double y;
};
void plotpoly(point arr[],int n)
{
for(int i=0;i<n-1;i++)
line(arr[i].x,arr[i].y,arr[i+1].x,arr[i+1].y);
line(arr[0].x,arr[0].y,arr[n-1].x,arr[n-1].y);
}
void seedfill(int x,int y,int fil,int bound)
{
int cur;
cur=getpixel(x,y);
if(cur!=bound && cur!=fil)
{
putpixel(x,y,fil);
seedfill(x+1,y,fil,bound);
seedfill(x-1,y,fil,bound);
seedfill(x,y+1,fil,bound);
seedfill(x,y-1,fil,bound);
}
return;
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
int size1;
cout<<"Enter number of vertices: ";
cin>>size1;
point arr1[size1];
for(int i=0;i<size1;i++)
cin>>arr1[i].x>>arr1[i].y;
plotpoly(arr1,size1);

58
356/COE/13
cout<<"Enter the inside points of the poly "<<endl;
int x1,y1;
cin>>x1>>y1;
seedfill(x1,y1,1,15);
getch();
}

Output

Input File:

15. To demonstrate 2D transformations


#include <stdio.h>
#include <graphics.h>

59
356/COE/13
#include <math.h>
#include <limits.h>
#include </home/sameer/Desktop/Programming/myline.h>
#include <bits/stdc++.h>
#include <vector>
using namespace std ;
double c[3][3];
void multiply (double a[][3] , double b[][3]){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
}
}
int i,j,k;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
struct point{
int x,y;
};
void draw(vector< pair < int,int> > v, int n){
for(int i=0;i<n-1;i++){
myline(320+v[i].first,240-v[i].second,320+v[i+1].first,240-
v[i+1].second,WHITE);
}
myline(320+v[0].first,240-v[0].second,320+v[n-1].first,240-v[n-
1].second,WHITE);
}
pair<int,int> solve(int x,int y,double mat[][3]){
int a = mat[0][0]*x + mat[0][1]*y + mat[0][2];
int b = mat[1][0]*x + mat[1][1]*y + mat[1][2];
return pair<int,int>(a,b);
}
int main(){

int gd,gm ;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,NULL);
vector< pair<int,int> > v;
int choice = 0,j,i;
printf("Enter a choice: \n");
printf("1 - Enter an object :\n");
printf("2 - Operate :\n");
printf("3 - View operated :\n");

60
356/COE/13
printf("4 - Exit :\n");
scanf("%d",&choice);
double mat[3][3],opmat[3][3];
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(i==j){
mat[i][j]=1;
}
else{
mat[i][j]=0;
}
}
}
double x,y;
int n;

while(choice!=4){

if(choice==1){
printf("Enter number of vertices:\n");
scanf("%d",&n);
v.clear();
printf("Enter the vertices: \n");
for(int i=0;i<n;i++){
scanf("%lf%lf",&x,&y);
v.push_back(pair<double,double> (x,y));
}
for(int i=0;i<n;i++){
cout<<v[i].first<<" "<<v[i].second<<endl;
}
}
else if(choice==2){
int op;
printf("Enter an operation. Press 4 to exit:\n");
printf("1 - translate \n");
printf("2 - rotate \n");
printf("3 - scale \n");
printf("4 - exit \n");
scanf("%d",&op);
while(op!=4){
if(op==1){
printf("Enter x and y for translation:\n");
scanf("%lf%lf",&x,&y);
opmat[0][0]=1;
opmat[0][1]=0;
opmat[0][2]=x;
opmat[1][0]=0;
opmat[1][1]=1;
opmat[1][2]=y;
opmat[2][0]=0;

61
356/COE/13
opmat[2][1]=0;
opmat[2][2]=1;
multiply(opmat,mat);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
mat[i][j]=c[i][j];
}
}
}
else if(op==2){
double theta;
printf("Enter an angle for anticlockwise
rotation:\n");
scanf("%lf",&theta);
printf("Enter a point around which the figure
is rotated:\n");
scanf("%lf%lf",&x,&y);
opmat[0][0]=1;
opmat[0][1]=0;
opmat[0][2]=-x;
opmat[1][0]=0;
opmat[1][1]=1;
opmat[1][2]=-y;
opmat[2][0]=0;
opmat[2][1]=0;
opmat[2][2]=1;
multiply(opmat,mat);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
mat[i][j]=c[i][j];
}
}
opmat[0][0]=cos(theta);
opmat[0][1]=-sin(theta);
opmat[0][2]=0;
opmat[1][0]=sin(theta);
opmat[1][1]=cos(theta);
opmat[1][2]=0;
opmat[2][0]=0;
opmat[2][1]=0;
opmat[2][2]=1;
multiply(opmat,mat);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
mat[i][j]=c[i][j];
}
}
opmat[0][0]=1;
opmat[0][1]=0;

62
356/COE/13
opmat[0][2]=x;
opmat[1][0]=0;
opmat[1][1]=1;
opmat[1][2]=y;
opmat[2][0]=0;
opmat[2][1]=0;
opmat[2][2]=1;
multiply(opmat,mat);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
mat[i][j]=c[i][j];
}
}
}
else if(op==3){
printf("Enter xfactor and yfactor for scaling
abot origin:\n");
scanf("%lf%lf",&x,&y);

opmat[0][0]=x;
opmat[0][1]=0;
opmat[0][2]=0;
opmat[1][0]=0;
opmat[1][1]=y;
opmat[1][2]=0;
opmat[2][0]=0;
opmat[2][1]=0;
opmat[2][2]=1;
multiply(opmat,mat);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
mat[i][j]=c[i][j];
}
}
}else{
break;
}

printf("1 - translate \n");


printf("2 - rotate \n");
printf("3 - scale \n");
printf("4 - exit \n");
scanf("%d",&op);
}
}
else if(choice==3){
printf("Display the figure:\n");
for(int i=0;i<n;i++){
v[i]=solve(v[i].first,v[i].second,mat);

63
356/COE/13
}
for(int i=0;i<n;i++){
printf("%d %d\n",v[i].first,v[i].second);
}
draw(v,n);

}else if(choice==4){
break;
}
printf("Enter a choice:\n");
printf("1 - Enter an object :\n");
printf("2 - Operate :\n");
printf("3 - View operated :\n");
printf("4 - Exit : \n");
scanf("%d",&choice);
}
getch();
closegraph();
return 0;
}
Output

64
356/COE/13
16. To demonstrate 3D viewing via cube
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
#define PI acos(-1.0)
#define l 1.25
using namespace std;
double thetay=45*PI/180;
double thetax=asin(1/sqrt(3));
double thetax1=45*PI/180;
double thetay1=asin(sqrt((2-(l*l))/(2+(l*l))));
double ang=5*PI/180;
double s=1.0;
double persplane[4][3]={{-25,0,100},{25,0,100},{25,50,100},{-25,50,100}};
double c1=10,c2=10,c3=400;
void newpointsx(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=a[i][j];
break;
case 1: newpts[i][j]=(a[i][j]*cos(ang)-a[i][j+1]*sin(ang));

65
356/COE/13
break;
case 2: newpts[i][j]=(a[i][j-1]*sin(ang)+a[i][j]*cos(ang));
break;
}
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsy(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(ang)+a[i][j+2]*sin(ang));
break;
case 1: newpts[i][j]=a[i][j];
break;
case 2: newpts[i][j]=((-1)*a[i][j-2]*sin(ang)+a[i][j]*cos(ang));
break;
}
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsz(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(ang)-a[i][j+1]*sin(ang));
break;
case 1: newpts[i][j]=(a[i][j-1]*sin(ang)+a[i][j]*cos(ang));
break;
case 2: newpts[i][j]=a[i][j];

66
356/COE/13
break;
}
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointssci(double a[][3],double s)
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
newpts[i][j]=(a[i][j]*(s+0.1)/s);
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsscd(double a[][3],double s)
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
newpts[i][j]=(a[i][j]*(s-0.1)/s);
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void plotfv(double a[][3])
{
line(getmaxx()/6+a[0][0],getmaxy()/4-a[0][1],getmaxx()/6+a[1]
[0],getmaxy()/4-a[1][1]);
line(getmaxx()/6+a[1][0],getmaxy()/4-a[1][1],getmaxx()/6+a[2]
[0],getmaxy()/4-a[2][1]);

67
356/COE/13
line(getmaxx()/6+a[2][0],getmaxy()/4-a[2][1],getmaxx()/6+a[3]
[0],getmaxy()/4-a[3][1]);
line(getmaxx()/6+a[3][0],getmaxy()/4-a[3][1],getmaxx()/6+a[0]
[0],getmaxy()/4-a[0][1]);
line(getmaxx()/6+a[4][0],getmaxy()/4-a[4][1],getmaxx()/6+a[5]
[0],getmaxy()/4-a[5][1]);
line(getmaxx()/6+a[5][0],getmaxy()/4-a[5][1],getmaxx()/6+a[6]
[0],getmaxy()/4-a[6][1]);
line(getmaxx()/6+a[6][0],getmaxy()/4-a[6][1],getmaxx()/6+a[7]
[0],getmaxy()/4-a[7][1]);
line(getmaxx()/6+a[7][0],getmaxy()/4-a[7][1],getmaxx()/6+a[4]
[0],getmaxy()/4-a[4][1]);
line(getmaxx()/6+a[0][0],getmaxy()/4-a[0][1],getmaxx()/6+a[4]
[0],getmaxy()/4-a[4][1]);
line(getmaxx()/6+a[1][0],getmaxy()/4-a[1][1],getmaxx()/6+a[5]
[0],getmaxy()/4-a[5][1]);
line(getmaxx()/6+a[2][0],getmaxy()/4-a[2][1],getmaxx()/6+a[6]
[0],getmaxy()/4-a[6][1]);
line(getmaxx()/6+a[3][0],getmaxy()/4-a[3][1],getmaxx()/6+a[7]
[0],getmaxy()/4-a[7][1]);
}
void plotsv(double a[][3])
{
line(getmaxx()/2+a[0][1],getmaxy()/4-a[0][2],getmaxx()/2+a[1]
[1],getmaxy()/4-a[1][2]);
line(getmaxx()/2+a[1][1],getmaxy()/4-a[1][2],getmaxx()/2+a[2]
[1],getmaxy()/4-a[2][2]);
line(getmaxx()/2+a[2][1],getmaxy()/4-a[2][2],getmaxx()/2+a[3]
[1],getmaxy()/4-a[3][2]);
line(getmaxx()/2+a[3][1],getmaxy()/4-a[3][2],getmaxx()/2+a[0]
[1],getmaxy()/4-a[0][2]);
line(getmaxx()/2+a[4][1],getmaxy()/4-a[4][2],getmaxx()/2+a[5]
[1],getmaxy()/4-a[5][2]);
line(getmaxx()/2+a[5][1],getmaxy()/4-a[5][2],getmaxx()/2+a[6]
[1],getmaxy()/4-a[6][2]);
line(getmaxx()/2+a[6][1],getmaxy()/4-a[6][2],getmaxx()/2+a[7]
[1],getmaxy()/4-a[7][2]);
line(getmaxx()/2+a[7][1],getmaxy()/4-a[7][2],getmaxx()/2+a[4]
[1],getmaxy()/4-a[4][2]);
line(getmaxx()/2+a[0][1],getmaxy()/4-a[0][2],getmaxx()/2+a[4]
[1],getmaxy()/4-a[4][2]);
line(getmaxx()/2+a[1][1],getmaxy()/4-a[1][2],getmaxx()/2+a[5]
[1],getmaxy()/4-a[5][2]);
line(getmaxx()/2+a[2][1],getmaxy()/4-a[2][2],getmaxx()/2+a[6]
[1],getmaxy()/4-a[6][2]);
line(getmaxx()/2+a[3][1],getmaxy()/4-a[3][2],getmaxx()/2+a[7]
[1],getmaxy()/4-a[7][2]);
}
void plottv(double a[][3])
{
line(5*getmaxx()/6+a[0][0],getmaxy()/4-a[0][2],5*getmaxx()/6+a[1]
[0],getmaxy()/4-a[1][2]);
line(5*getmaxx()/6+a[1][0],getmaxy()/4-a[1][2],5*getmaxx()/6+a[2]
[0],getmaxy()/4-a[2][2]);

68
356/COE/13
line(5*getmaxx()/6+a[2][0],getmaxy()/4-a[2][2],5*getmaxx()/6+a[3]
[0],getmaxy()/4-a[3][2]);
line(5*getmaxx()/6+a[3][0],getmaxy()/4-a[3][2],5*getmaxx()/6+a[0]
[0],getmaxy()/4-a[0][2]);
line(5*getmaxx()/6+a[4][0],getmaxy()/4-a[4][2],5*getmaxx()/6+a[5]
[0],getmaxy()/4-a[5][2]);
line(5*getmaxx()/6+a[5][0],getmaxy()/4-a[5][2],5*getmaxx()/6+a[6]
[0],getmaxy()/4-a[6][2]);
line(5*getmaxx()/6+a[6][0],getmaxy()/4-a[6][2],5*getmaxx()/6+a[7]
[0],getmaxy()/4-a[7][2]);
line(5*getmaxx()/6+a[7][0],getmaxy()/4-a[7][2],5*getmaxx()/6+a[4]
[0],getmaxy()/4-a[4][2]);
line(5*getmaxx()/6+a[0][0],getmaxy()/4-a[0][2],5*getmaxx()/6+a[4]
[0],getmaxy()/4-a[4][2]);
line(5*getmaxx()/6+a[1][0],getmaxy()/4-a[1][2],5*getmaxx()/6+a[5]
[0],getmaxy()/4-a[5][2]);
line(5*getmaxx()/6+a[2][0],getmaxy()/4-a[2][2],5*getmaxx()/6+a[6]
[0],getmaxy()/4-a[6][2]);
line(5*getmaxx()/6+a[3][0],getmaxy()/4-a[3][2],5*getmaxx()/6+a[7]
[0],getmaxy()/4-a[7][2]);
}
void plotiso(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(thetay)+a[i][j+2]*sin(thetay));
break;
case 1: newpts[i][j]=(a[i][j-1]*sin(thetax)*sin(thetay)+a[i]
[j]*cos(thetax)-a[i][j+1]*sin(thetax)*cos(thetay));
break;
case 2: newpts[i][j]=a[i][j];
break;
}
}
}
line(getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1][1]);
line(getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2][1]);
line(getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3][1]);
line(getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0][1]);
line(getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4]
[1],getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5]
[1],getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6]
[1],getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);

69
356/COE/13
line(getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7]
[1],getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
}
void plotdia(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(thetay1)+a[i][j+2]*sin(thetay1));
break;
case 1: newpts[i][j]=(a[i][j-1]*sin(thetax1)*sin(thetay1)+a[i]
[j]*cos(thetax1)-a[i][j+1]*sin(thetax1)*cos(thetay1));
break;
case 2: newpts[i][j]=a[i][j];
break;
}
}
}
line(getmaxx()/2+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],getmaxx()/2+newpts[1][0],3*getmaxy()/4-newpts[1][1]);
line(getmaxx()/2+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],getmaxx()/2+newpts[2][0],3*getmaxy()/4-newpts[2][1]);
line(getmaxx()/2+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],getmaxx()/2+newpts[3][0],3*getmaxy()/4-newpts[3][1]);
line(getmaxx()/2+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],getmaxx()/2+newpts[0][0],3*getmaxy()/4-newpts[0][1]);
line(getmaxx()/2+newpts[4][0],3*getmaxy()/4-newpts[4]
[1],getmaxx()/2+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(getmaxx()/2+newpts[5][0],3*getmaxy()/4-newpts[5]
[1],getmaxx()/2+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(getmaxx()/2+newpts[6][0],3*getmaxy()/4-newpts[6]
[1],getmaxx()/2+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
line(getmaxx()/2+newpts[7][0],3*getmaxy()/4-newpts[7]
[1],getmaxx()/2+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(getmaxx()/2+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],getmaxx()/2+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(getmaxx()/2+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],getmaxx()/2+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(getmaxx()/2+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],getmaxx()/2+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(getmaxx()/2+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],getmaxx()/2+newpts[7][0],3*getmaxy()/4-newpts[7][1]);

70
356/COE/13
}
void plotface1(double newpts[][3])
{
line(5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1][1]);
line(5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2][1]);
line(5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3][1]);
line(5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0][1]);
}

void plotface2(double newpts[][3])


{
line(5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1][1]);
line(5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4]
[1],5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
}
void plotface3(double newpts[][3])
{
line(5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4]
[1],5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5]
[1],5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6]
[1],5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
line(5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7]
[1],5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
}
void plotface4(double newpts[][3])
{
line(5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
line(5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2][1]);
line(5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6]
[1],5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
}
void plotface5(double newpts[][3])
{
line(5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0]
[1],5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
line(5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7][1]);
line(5*getmaxx()/6+newpts[3][0],3*getmaxy()/4-newpts[3]
[1],5*getmaxx()/6+newpts[0][0],3*getmaxy()/4-newpts[0][1]);

71
356/COE/13
line(5*getmaxx()/6+newpts[7][0],3*getmaxy()/4-newpts[7]
[1],5*getmaxx()/6+newpts[4][0],3*getmaxy()/4-newpts[4][1]);
}
void plotface6(double newpts[][3])
{
line(5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5][1]);
line(5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2]
[1],5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
line(5*getmaxx()/6+newpts[1][0],3*getmaxy()/4-newpts[1]
[1],5*getmaxx()/6+newpts[2][0],3*getmaxy()/4-newpts[2][1]);
line(5*getmaxx()/6+newpts[5][0],3*getmaxy()/4-newpts[5]
[1],5*getmaxx()/6+newpts[6][0],3*getmaxy()/4-newpts[6][1]);
}
void plotcube(double a[][3])
{
plotface1(a);
plotface2(a);
plotface3(a);
plotface4(a);
plotface5(a);
plotface6(a);
}
void plotpers(double a[][3])
{
double x=persplane[1][0]-persplane[0][0],y=persplane[1][1]-persplane[0]
[1],z=persplane[1][2]-persplane[0][2];
double _x=persplane[3][0]-persplane[0][0],_y=persplane[3][1]-persplane[0]
[1],_z=persplane[3][2]-persplane[0][2];
double n1=y*_z-_y*z,n2=_x*z-x*_z,n3=x*_y-_x*y;
double d1=persplane[0][0]*n1+persplane[0][1]*n2+persplane[0][2]*n3;
double d2=c1*n1+c2*n2+c3*n3;
double d=d1-d2;
double newpts[8][3];
for(int i=0;i<8;i++)
{
double h=n1*a[i][0]+n2*a[i][1]+n3*a[i][2]-d2;
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*(c1*n1+d)+a[i][j+1]*c1*n2+a[i]
[j+2]*c1*n3-c1*d1)/h;
break;
case 1: newpts[i][j]=(a[i][j-1]*c2*n1+a[i][j]*(c2*n2+d)+a[i]
[j+1]*c2*n3-c2*d1)/h;
break;
case 2: newpts[i][j]=(a[i][j-2]*c3*n1+a[i][j-1]*c3*n2+a[i][j]*(c3*n3+d)-
c3*d1)/h;
break;
}
}
}

72
356/COE/13
plotcube(newpts);
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
double a[][3]={{-50,-50,50},{50,-50,50},{50,50,50},{-50,50,50},{-50,-50,-
50},{50,-50,-50},{50,50,-50},{-50,50,-50}};
while(1)
{
cleardevice();
outtextxy(3,0,"Side of Cube : 50");
outtextxy(3,20,"1: X Rotation, 2: Y Rotation, 3: Z Rotation, 4: Zoom In, 5:
Zoom Out");
outtextxy(3,40,"Front View");
outtextxy(getmaxx()/3+3,40,"Side View");
outtextxy(2*getmaxx()/3+3,40,"Top View");
outtextxy(3,getmaxy()/2+2,"Isometric View");
outtextxy(getmaxx()/3+3,getmaxy()/2+2,"L : 1.25");
outtextxy(getmaxx()/3+3,getmaxy()/2+22,"Diametric View");
outtextxy(2*getmaxx()/3+3,getmaxy()/2+2,"COP : (10,10,400)");
outtextxy(2*getmaxx()/3+3,getmaxy()/2+22,"Perspective View");
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(getmaxx()/3,40,getmaxx()/3,getmaxy());
line(2*getmaxx()/3,40,2*getmaxx()/3,getmaxy());
plotfv(a);
plotsv(a);
plottv(a);
plotiso(a);
plotdia(a);
plotpers(a);
char key=getch();
switch(key)
{
case '1':newpointsx(a);
break;
case '2':newpointsy(a);
break;
case '3':newpointsz(a);
break;
case '4':newpointssci(a,s);
s=s+0.1;
break;
case '5':newpointsscd(a,s);
s=s-0.1;
break;
}
}
closegraph();
return 0;
}

Output

73
356/COE/13
Simple View:

Rotation about x:

74
356/COE/13
17. To identify the visible and non-visible surfaces of a cube when
illuminated by a light source
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include <LINEFUNC.h>
//#include <CIRCLEFUNC.h>
#include <dos.h>
#include<graphics.h>
#define PI acos(-1.0)
#include<bits/stdc++.h>
using namespace std;

double thetay=45*PI/180;
double thetax=asin(1/sqrt(3));
double ang=5*PI/180;
double s=1.0;
double a[][3]={{-50,-50,50},{50,-50,50},{50,50,50},{-50,50,50},{-50,-50,-50},
{50,-50,-50},{50,50,-50},{-50,50,-50}};
double n[6][3];
double m[6][3];
double l[6][3];
double li[3];
int f1,f2,f3,f4,f5,f6;
void newpointsx(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=a[i][j];
break;
case 1: newpts[i][j]=(a[i][j]*cos(ang)-a[i][j+1]*sin(ang));
break;
case 2: newpts[i][j]=(a[i][j-1]*sin(ang)+a[i][j]*cos(ang));
break;
}
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)

75
356/COE/13
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsy(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(ang)+a[i][j+2]*sin(ang));
break;
case 1: newpts[i][j]=a[i][j];
break;
case 2: newpts[i][j]=((-1)*a[i][j-2]*sin(ang)+a[i][j]*cos(ang));
break;
}
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsz(double a[][3])
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
switch(j)
{
case 0: newpts[i][j]=(a[i][j]*cos(ang)-a[i][j+1]*sin(ang));
break;
case 1: newpts[i][j]=(a[i][j-1]*sin(ang)+a[i][j]*cos(ang));
break;
case 2: newpts[i][j]=a[i][j];
break;
}
}
}
for(int i=0;i<8;i++)

76
356/COE/13
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointssci(double a[][3],double s)
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
newpts[i][j]=(a[i][j]*(s+0.1)/s);
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void newpointsscd(double a[][3],double s)
{
double newpts[8][3];
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
newpts[i][j]=(a[i][j]*(s-0.1)/s);
}
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=newpts[i][j];
}
}
}
void plotface1(double newpts[][3])
{
if(f1) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0]
[1],getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1][1]);
line(getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1]
[1],getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2][1]);

77
356/COE/13
line(getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2]
[1],getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3][1]);
line(getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3]
[1],getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0][1]);
setlinestyle(0,1,1);
}
void plotface2(double newpts[][3])
{
if(f2) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0]
[1],getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4][1]);
line(getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1]
[1],getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5][1]);
line(getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0]
[1],getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1][1]);
line(getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4]
[1],getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5][1]);
setlinestyle(0,1,1);
}
void plotface3(double newpts[][3])
{
if(f3) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4]
[1],getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5][1]);
line(getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5]
[1],getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6][1]);
line(getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6]
[1],getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7][1]);
line(getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7]
[1],getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4][1]);
setlinestyle(0,1,1);
}
void plotface4(double newpts[][3])
{
if(f4) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3]
[1],getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7][1]);
line(getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2]
[1],getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6][1]);
line(getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3]
[1],getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2][1]);
line(getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6]
[1],getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7][1]);
setlinestyle(0,1,1);
}
void plotface5(double newpts[][3])
{
if(f5) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0]
[1],getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4][1]);

78
356/COE/13
line(getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3]
[1],getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7][1]);
line(getmaxx()/2+newpts[3][0],getmaxy()/2-newpts[3]
[1],getmaxx()/2+newpts[0][0],getmaxy()/2-newpts[0][1]);
line(getmaxx()/2+newpts[7][0],getmaxy()/2-newpts[7]
[1],getmaxx()/2+newpts[4][0],getmaxy()/2-newpts[4][1]);
setlinestyle(0,1,1);
}
void plotface6(double newpts[][3])
{
if(f6) setlinestyle(1,1,1);
line(getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1]
[1],getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5][1]);
line(getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2]
[1],getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6][1]);
line(getmaxx()/2+newpts[1][0],getmaxy()/2-newpts[1]
[1],getmaxx()/2+newpts[2][0],getmaxy()/2-newpts[2][1]);
line(getmaxx()/2+newpts[5][0],getmaxy()/2-newpts[5]
[1],getmaxx()/2+newpts[6][0],getmaxy()/2-newpts[6][1]);
setlinestyle(0,1,1);
}
void plotcube(double a[][3])
{
plotface1(a);
plotface2(a);
plotface3(a);
plotface4(a);
plotface5(a);
plotface6(a);
}
void findnormals()
{
double px=a[7][0]-a[0][0], py=a[7][1]-a[0][1],pz=a[7][2]-a[0][2],x=a[3][0]-
a[0][0],y=a[3][1]-a[0][1],z=a[3][2]-a[0][2];
double _x=a[1][0]-a[0][0],_y=a[1][1]-a[0][1],_z=a[1][2]-a[0][2];
double temp=px*(y*_z-_y*z)+py*(_x*z-x*_z)+pz*(x*_y-_x*y);
if(temp>0) n[0][0]=_y*z-y*_z,n[0][1]=x*_z-_x*z,n[0][2]=_x*y-x*_y;
else n[0][0]=y*_z-_y*z,n[0][1]=_x*z-x*_z,n[0][2]=x*_y-_x*y;
n[2][0]=(-1)*n[0][0],n[2][1]=(-1)*n[0][1],n[2][2]=(-1)*n[0][2];
px=a[7][0]-a[0][0], py=a[7][1]-a[0][1],pz=a[7][2]-a[0][2],x=a[4][0]-a[0]
[0],y=a[4][1]-a[0][0],z=a[4][2]-a[0][0];
_x=a[1][0]-a[0][0],_y=a[1][1]-a[0][1],_z=a[1][2]-a[0][2];
temp=px*(y*_z-_y*z)+py*(_x*z-x*_z)+pz*(x*_y-_x*y);
if(temp>0) n[1][0]=_y*z-y*_z,n[1][1]=x*_z-_x*z,n[1][2]=_x*y-x*_y;
else n[1][0]=y*_z-_y*z,n[1][1]=_x*z-x*_z,n[1][2]=x*_y-_x*y;
n[3][0]=(-1)*n[1][0],n[3][1]=(-1)*n[1][1],n[3][2]=(-1)*n[1][2];
px=a[2][0]-a[0][0], py=a[2][1]-a[0][1],pz=a[2][2]-a[0][2],x=a[4][0]-a[0]
[0],y=a[4][1]-a[0][0],z=a[4][2]-a[0][0];
_x=a[3][0]-a[0][0],_y=a[3][1]-a[0][1],_z=a[3][2]-a[0][2];
temp=px*(y*_z-_y*z)+py*(_x*z-x*_z)+pz*(x*_y-_x*y);

79
356/COE/13
if(temp>0) n[4][0]=_y*z-y*_z,n[4][1]=x*_z-_x*z,n[4][2]=_x*y-x*_y;
else n[4][0]=y*_z-_y*z,n[4][1]=_x*z-x*_z,n[4][2]=x*_y-_x*y;
n[5][0]=(-1)*n[4][0],n[5][1]=(-1)*n[4][1],n[5][2]=(-1)*n[4][2];
}
void findmeanpts()
{
m[0][0]=(a[0][0]+a[1][0]+a[2][0]+a[3][0])/4;
m[0][1]=(a[0][1]+a[1][1]+a[2][1]+a[3][1])/4;
m[0][2]=(a[0][2]+a[1][2]+a[2][2]+a[3][2])/4;
m[1][0]=(a[0][0]+a[1][0]+a[4][0]+a[5][0])/4;
m[1][1]=(a[0][1]+a[1][1]+a[4][1]+a[5][1])/4;
m[1][2]=(a[0][2]+a[1][2]+a[4][2]+a[5][2])/4;
m[2][0]=(a[4][0]+a[5][0]+a[6][0]+a[7][0])/4;
m[2][1]=(a[4][1]+a[5][1]+a[6][1]+a[7][1])/4;
m[2][2]=(a[4][2]+a[5][2]+a[6][2]+a[7][2])/4;
m[3][0]=(a[2][0]+a[3][0]+a[6][0]+a[7][0])/4;
m[3][1]=(a[2][1]+a[3][1]+a[6][1]+a[7][1])/4;
m[3][2]=(a[2][2]+a[3][2]+a[6][2]+a[7][2])/4;
m[4][0]=(a[0][0]+a[1][0]+a[3][0]+a[7][0])/4;
m[4][1]=(a[0][1]+a[1][1]+a[3][1]+a[7][1])/4;
m[4][2]=(a[0][2]+a[1][2]+a[3][2]+a[7][2])/4;
m[5][0]=(a[1][0]+a[2][0]+a[5][0]+a[6][0])/4;
m[5][1]=(a[1][1]+a[2][1]+a[5][1]+a[6][1])/4;
m[5][2]=(a[1][2]+a[2][2]+a[5][2]+a[6][2])/4;
}
void findedges()
{
l[0][0]=m[0][0]-li[0];
l[0][1]=m[0][1]-li[1];
l[0][2]=m[0][2]-li[2];
l[1][0]=m[1][0]-li[0];
l[1][1]=m[1][1]-li[1];
l[1][2]=m[1][2]-li[2];
l[2][0]=m[2][0]-li[0];
l[2][1]=m[2][1]-li[1];
l[2][2]=m[2][2]-li[2];
l[3][0]=m[3][0]-li[0];
l[3][1]=m[3][1]-li[1];
l[3][2]=m[3][2]-li[2];
l[4][0]=m[4][0]-li[0];
l[4][1]=m[4][1]-li[1];
l[4][2]=m[4][2]-li[2];
l[5][0]=m[5][0]-li[0];
l[5][1]=m[5][1]-li[1];
l[5][2]=m[5][2]-li[2];
setlinestyle(1,1,1);
line(320+li[0],240-li[1],320+m[0][0],240-m[0][1]);
line(320+li[0],240-li[1],320+m[1][0],240-m[1][1]);
line(320+li[0],240-li[1],320+m[2][0],240-m[2][1]);
line(320+li[0],240-li[1],320+m[3][0],240-m[3][1]);

80
356/COE/13
line(320+li[0],240-li[1],320+m[4][0],240-m[4][1]);
line(320+li[0],240-li[1],320+m[5][0],240-m[5][1]);
setlinestyle(0,1,1);
}
void setflags()
{
double temp=l[0][0]*n[0][0]+l[0][1]*n[0][1]+l[0][2]*n[0][2];
if(temp>0) f1=1;
else f1=0;
temp=l[1][0]*n[1][0]+l[1][1]*n[1][1]+l[1][2]*n[1][2];
if(temp>0) f2=1;
else f2=0;
temp=l[2][0]*n[2][0]+l[2][1]*n[2][1]+l[2][2]*n[2][2];
if(temp>0) f3=1;
else f3=0;
temp=l[3][0]*n[3][0]+l[3][1]*n[3][1]+l[3][2]*n[3][2];
if(temp>0) f4=1;
else f4=0;
temp=l[4][0]*n[4][0]+l[4][1]*n[4][1]+l[4][2]*n[4][2];
if(temp>0) f5=1;
else f5=0;
temp=l[5][0]*n[5][0]+l[5][1]*n[5][1]+l[5][2]*n[5][2];
if(temp>0) f6=1;
else f6=0;
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode,"C:\\TC\\BGI");
li[0]=-10;
li[1]=-60;
li[2]=-60;
while(1)
{
cleardevice();
findnormals();
findmeanpts();
findedges();
setflags();
plotcube(a);
char key=getch();
switch(key)
{
case '1':newpointsx(a);
break;
case '2':newpointsy(a);
break;
case '3':newpointsz(a);
break;
case '4':newpointssci(a,s);

81
356/COE/13
s=s+0.1;
break;
case '5':newpointsscd(a,s);
s=s-0.1;
break;
}
}
closegraph();
return 0;
}

OUTPUT

82
356/COE/13
83
356/COE/13

You might also like