CG PCL CODE
CG PCL CODE
Aim: Write C++ program to draw a concave polygon and fill it with desired color using
scan fill algorithm. Apply the concept of inheritance.
#include<iostream>
#include<graphics.h>
#include <stdlib.h>
using namespace std;
class point
{
public:
int x,y;
};
class poly
{
private:
point p[20];
int inter[20],x,y;
int v,xmin,ymin,xmax,ymax;
public:
int c;
void read();
void calcs();
void display();
void ints(float);
void sort(int);
};
void poly::read()
{
int i;
cout<<"\n\t SCAN_FILL ALGORITHM";
cout<<"\n Enter the no of vertices of polygon:";
cin>>v;
if(v>2)
{
for(i=0;i<v; i++)
{
cout<<"\nEnter the co-ordinate no.- "<<i+1<<" : ";
cout<<"\n\tx"<<(i+1)<<"=";
cin>>p[i].x;
cout<<"\n\ty"<<(i+1)<<"=";
cin>>p[i].y;
}
p[i].x=p[0].x;
p[i].y=p[0].y;
xmin=xmax=p[0].x;
ymin=ymax=p[0].y;
}
else
cout<<"\n Enter valid no. of vertices.";
}
void poly::calcs()
{
for(int i=0;i<v;i++)
{
if(xmin>p[i].x)
xmin=p[i].x;
if(xmax<p[i].x)
xmax=p[i].x;
if(ymin>p[i].y)
ymin=p[i].y;
if(ymax<p[i].y)
ymax=p[i].y;
}
}
void poly::display()
{
int ch,ch1;
float s,s2;
do
{
cout<<"\n\nMENU:";
cout<<"\n\n\t1 . Scan line Fill ";
cout<<"\n\n\t2 . Exit ";
cout<<"\n\nEnter your choice: ";
cin>>ch1;
switch(ch1)
{
case 1:
s=ymin+0.01;
delay(100);
cleardevice();
while(s<=ymax)
{
ints(s);
sort(s);
s++;
}
break;
case 2:
exit(0);
}
cout<<"Do you want to continue?: ";
cin>>ch;
if(ch==1)
{
continue;
}
else if(ch==2)
{
exit(0);
}
}while(true);
}
void poly::ints(float z)
{
int x1,x2,y1,y2,temp;
c=0;
for(int i=0;i<v;i++)
{
x1=p[i].x;
y1=p[i].y;
x2=p[i+1].x;
y2=p[i+1].y;
if(y2<y1)
{
temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
if(z<=y2&&z>=y1)
{
if((y1-y2)==0)
x=x1;
else
{
x=((x2-x1)*(z-y1))/(y2-y1);
x=x+x1;
}
if(x<=xmax && x>=xmin)
inter[c++]=x;
}
}
}
void poly::sort(int z)
{
int temp,j,i;
for(i=0;i<v;i++)
{
line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
}
delay(100);
for(i=0; i<c;i+=2)
{
delay(100);
line(inter[i],z,inter[i+1],z);
}
}
int main()
{
int cl;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
cleardevice();
poly x;
x.read();
x.calcs();
cleardevice();
cout<<"\n\tEnter the colour u want:(0-15)->";
cin>>cl;
setcolor(cl);
x.display();
closegraph();
getch();
return 0;
}
Assingment-2
Aim: Write C++ program to implement Cohen Southerland line clipping algorithm.
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
using namespace std;
class Coordinate
{
public:
int x,y;
char code[4];
};
class Lineclip
{
public:
Coordinate PT;
void drawwindow();
void drawline(Coordinate p1,Coordinate p2);
Coordinate setcode(Coordinate p);
int visibility(Coordinate p1,Coordinate p2);
Coordinate resetendpt(Coordinate p1,Coordinate p2);
};
int main()
{
Lineclip lc;
int gd = DETECT,v,gm;
Coordinate p1,p2,p3,p4,ptemp;
cout<<"\n Enter x1 and y1\n";
cin>>p1.x>>p1.y;
cout<<"\n Enter x2 and y2\n";
cin>>p2.x>>p2.y;
initgraph(&gd,&gm,(char*)"");
lc.drawwindow();
delay(2000);
lc.drawline (p1,p2);
delay(2000);
cleardevice();
delay(2000);
p1=lc.setcode(p1);
p2=lc.setcode(p2);
v=lc.visibility(p1,p2);
delay(2000);
switch(v)
{
case 0:
{
lc.drawwindow();
delay(2000);
lc.drawline(p1,p2);
break;
}
case 1:
{
lc.drawwindow();
delay(2000);
break;
}
case 2:
{
p3=lc.resetendpt(p1,p2);
p4=lc.resetendpt(p2,p1);
lc.drawwindow();
delay(2000);
lc.drawline(p3,p4);
break;
}
}
delay(2000);
closegraph();
}
void Lineclip::drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void Lineclip::drawline(Coordinate p1,Coordinate p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
Coordinate Lineclip::setcode(Coordinate p)
{
Coordinate ptemp;
if(p.y<100)
{
ptemp.code[0]='1';
}
else
{
ptemp.code[0]='0';
}
if(p.y>350)
{
ptemp.code[1]='1';
}
else
{
ptemp.code[1]='0';
}
if(p.x>450)
{
ptemp.code[2]='1';
}
else
{
ptemp.code[2]='0';
}
if(p.x<150)
{
ptemp.code[3]='1';
}
else
{
ptemp.code[3]='0';
}
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
};
int Lineclip:: visibility(Coordinate p1,Coordinate p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if(p1.code[i]!='0' || (p2.code[i]=='1'))
{
flag='0';
}
}
if(flag==0)
{
return(0);
}
for(i=0;i<4;i++)
{
if(p1.code[i]==p2.code[i] && (p2.code[i]=='1'))
{
flag='0';
}
}
if(flag==0)
{
return(1);
}
return(2);
}
Coordinate Lineclip::resetendpt(Coordinate p1,Coordinate p2)
{
Coordinate temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
{
x=150;
}
if(p1.code[2]=='1')
{
x=450;
}
if((p1.code[3]=='1') || (p1.code[2])=='1')
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
{
temp.code[i]=p1.code[i];
}
if(temp.y<=350 && temp.y>=100)
{
return (temp);
}
}
if(p1.code[0]=='1')
{
y=100;
}
if(p1.code[1]=='1')
{
y=350;
}
if((p1.code[1]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m; temp.x=k;
temp.y=y; for(i=0;i<4;i++) temp.code[i]=p1.code[i];
return(temp);
}
else
{
return(p1);
}
}
Assingment-3
Aim:Write C++ program to draw the pattern. Use DDA line and Bresenham s̳ circle
drawing algorithm. Apply the concept of encapsulation.
#include <iostream>
#include <graphics.h>
using namespace std;
class DDA
{
int x1,y1,x2,y2;
public:
DDA(int xx1,int yy1,int xx2,int yy2)
{
x1=xx1;
y1=yy1;
x2=xx2;
y2=yy2;
}
void read(){
cout<<"Enter x and y coordinates of start point:: ";
cin>>x1>>y1;
cout<<"Enter x and y coordinates of end point:: ";
cin>>x2>>y2;
}
void drawline();
};
void DDA::drawline()
{
float dx=x2-x1;
float dy=y2-y1;
float length;
if (abs(dx)>=abs(dy))
length=abs(dx);
else
length = abs(dy);
float xi=dx/length;
float yi=dy/length;
float x=x1;
float y=y1;
for(int i=1;i<=length;i++)
{
putpixel(x,y,WHITE);
x+=xi;
y+=yi;
}
}
class BCA
{
int r,xc,yc;
public:
BCA(int rr,int x1, int y1)
{
r=rr;
xc=x1;
yc=y1;
}
void read()
{
cout <<"Enter the radius: ";
cin >> r;
cout <<"Enter the x and y coordinates: ";
cin >> xc>>yc;
}
void drawCircle();
void plot8WaySymmetry(int,int);
};
void BCA::plot8WaySymmetry(int x,int y)
{
putpixel(x+xc,y+yc,WHITE);
putpixel(y+yc,x+xc,WHITE);
putpixel(xc-x,y+yc,WHITE);
putpixel(y+yc,xc-x,WHITE);
putpixel(x+xc,yc-y,WHITE);
putpixel(yc-y,x+xc,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(yc-y,xc-x,WHITE);
}
void BCA::drawCircle(){
int x=0,y=r;
int d=3-2*r;
putpixel(xc,yc,WHITE);
while(x<=y){
if (d<=0){
d+=4*x+6;
x++;
}
else{
d+=4*(x-y)+10;
x++;
y--;
}
plot8WaySymmetry(x,y);
}
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
// To draw first shape
BCA circle1(50,120,120),circle2(100,120,120);
DDA line1(34,170,120,20),line2(206,170,120,20),line3(206,170,30,170);
circle1.drawCircle();
circle2.drawCircle();
line1.drawline();
line2.drawline();
line3.drawline();
// To draw second shape
BCA Circle1(45,350,350);
DDA
Line1(250,300,450,300),Line2(450,300,450,400),Line3(450,400,250,400),Line4(250,400,250,300);
DDA
Line5(350,300,450,350),Line6(450,350,350,400),Line7(350,400,250,350),Line8(250,350,350,300);
Circle1.drawCircle();
Line1.drawline();
Line2.drawline();
Line3.drawline();
Line4.drawline();
Line5.drawline();
Line6.drawline();
Line7.drawline();
Line8.drawline();
getch();
closegraph();
return 0;
}
Assingment-4
Aim: Write C++ program to draw 2-D object and perform following basic transformations a)
Scaling b) Translation c) Rotation. Apply the concept of operator overloading.
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
class transform
{
public:
int m,a[20][20],c[20][20];
int i,j,k;
public:
void display();
void accept();
void operator *(float b[20][20])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{ c[i][j]=0;
for(int k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}}}}};
void transform::display()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,(char*)"");
line((300),0,(300),600);
line(0,(300),600,(300));
for( i=0;i<m-1;i++)
{
line(300+a[i][0],300-a[i][1],300+a[i+1][0],300-a[i+1][1]);
}
line(300+a[0][0],300-a[0][1],300+a[i][0],300-a[i][1]);
for( i=0;i<m-1;i++)
{
line(300+c[i][0],300-c[i][1],300+c[i+1][0],300-c[i+1][1]);
}
line(300+c[0][0],300-c[0][1],300+c[i][0],300-c[i][1]);
delay(5000);
closegraph();
}
void transform::accept()
{
cout<<"\n";
cout<<"Enter the Number Of Edges: ";
cin>>m;
cout<<"\nEnter The Coordinates"<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<3;j++)
{
if(j>=2)
{
a[i][j]=1;
}
else
{
cin>>a[i][j];
}
}
}
}
int main()
{
int ch,tx,ty;
float sx,sy;
float deg,theta,b[20][20];
transform t;
t.accept();
while(true)
{
cout<<"\nEnter your choice\n1.Translation\n2.Scaling\n3.Rotation\n4.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nTRANSLATION OPERATION\nEnter value for tx and ty: ";
cin>>tx>>ty;
b[0][0]=b[2][2]=b[1][1]=1;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=tx;
b[2][1]=ty;
t * b;
t.display();
break;
case 2:
cout<<"\nSCALING OPERATION\nEnter value for sx,sy: ";
cin>>sx>>sy;
b[0][0]=sx;
b[1][1]=sy;
b[0][1]=b[0][2]=b[1][0]=b[1][2]=0;
b[2][0]=b[2][1]=0;
b[2][2] = 1;
t * b;
t.display();
break;
case 3:
cout<<"\nROTATION OPERATION\nEnter value for angle: ";
cin>>deg;
theta=deg*(3.14/180);
b[0][0]=b[1][1]=cos(theta);
b[0][1]=sin(theta);
b[1][0]=sin(-theta);
b[0][2]=b[1][2]=b[2][0]=b[2][1]=0;
b[2][2]=1;
t * b;
t.display();
break;
case 4:
exit(0);
default:
cout<<"\nInvalid choice";
}
}
getch();
return 0;
}
Assingment-5
Aim: Write C++ program to generate Hilbert curve using concept of fractals.
#include <iostream>
#include <math.h>
#include <graphics.h>
using namespace std;
class kochCurve
{
public:
void koch(int it,int x1,int y1,int x5,int y5)
{
int x2,y2,x3,y3,x4,y4;
int dx,dy;
if (it==0)
{
line(x1,y1,x5,y5);
}
else
{
delay(10); dx=(x5-x1)/3;
dy=(y5-y1)/3;
x2=x1+dx;
y2=y1+dy;
x3=(int)(0.5*(x1+x5)+sqrt(3)*(y1-y5)/6);
y3=(int)(0.5*(y1+y5)+sqrt(3)*(x5-x1)/6);
x4=2*dx+x1;
y4=2*dy+y1;
koch(it-1,x1,y1,x2,y2);
koch(it-1,x2,y2,x3,y3);
koch(it-1,x3,y3,x4,y4);
koch(it-1,x4,y4,x5,y5);
}
}
};
int main()
{
kochCurve k;
int it;
cout<<"Enter Number Of Iterations : "<<endl;
cin>>it;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
k.koch(it,150,20,20,280);
k.koch(it,280,280,150,20);
k.koch(it,20,280,280,280);
getch();
closegraph();
return 0;
}
Assingment-6
Aim: Write OpenGL program to draw Sun Rise and Sunset.