0% found this document useful (0 votes)
27 views23 pages

CG Code

Uploaded by

huks7781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views23 pages

CG Code

Uploaded by

huks7781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

/*Problem Statement:- 1

Write C++ program to draw a concave polygon and fill it with desired color using scan fill
algorithm.
*/

#include <conio.h>
#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()
{ //MAX,MIN
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 ch1;
char ch='y';
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;
}while(ch=='y' || ch=='Y');
}
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;
initwindow(500,600);
cleardevice();
poly x;
x.read();
x.calcs();
cleardevice();
cout<<"\n\tEnter the colour u want:(0-15)->"; //Selecting colour
cin>>cl;
setcolor(cl);
x.display();
closegraph();
getch();
return 0;
}
/*OUTPUT:-

*/

/*PROBLEM STATEMENT:-2
Write C++ program to implement Cohen Southerland line clipping algorithm.*/

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.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,"");
lc.drawwindow();
delay(1000);
lc.drawline (p1,p2);
delay(1000);
cleardevice();
delay(1000);
p1=lc.setcode(p1);
p2=lc.setcode(p2);
v=lc.visibility(p1,p2);
delay(1000);
switch(v)
{
case 0: lc.drawwindow();
delay(1000);
lc.drawline(p1,p2);
break;
case 1:lc.drawwindow();
delay(1000);
break;
case 2:p3=lc.resetendpt(p1,p2);
p4=lc.resetendpt(p2,p1);
lc.drawwindow();
delay(1000);
lc.drawline(p3,p4);
break;
}
delay(1000);
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);
}
/*
Input :

X1 , Y1:
100
200
X2, Y2 :
500
100

OUTPUT:-
*/

/*PROBLEM STATEMENT:-3. A)
Write a c++ program inscribed and circumscribed circles in triangle
*/

#include<iostream>
#include<graphics.h>
#include<stdio.h>
void ddaAlg(int x1,int y1,int x2,int y2)
{
int dx=x2-x1;
int dy=y2-y1;
int steps=dx>dy?dx:dy;
float xInc=dx/(float)steps;
float yInc=dy/(float)steps;
float x=x1;
float y=y1;
for(int i=0;i<=steps;i++)
{
putpixel(x,y,14);
x+=xInc;
y+=yInc;
}
}
void display(int xc,int yc,int x,int y)
{
putpixel(xc+x, yc+y, 3);
putpixel(xc-x, yc+y, 3);
putpixel(xc+x, yc-y, 3);
putpixel(xc-x, yc-y, 3);
putpixel(xc+y, yc+x, 3);
putpixel(xc-y, yc+x, 3);
putpixel(xc+y, yc-x, 3);
putpixel(xc-y, yc-x, 3);
}
void CircleB(int x1,int y1,int r)
{
int x=0,y=r;
int d=3-2*r;
display(x1,y1,x,y);
while(y>=x)
{
x++;
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else
{
d=d+4*x+6;
}
display(x1,y1,x,y);
}
}
int main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
CircleB(150,180,57);
CircleB(150,180,57/2);
ddaAlg(102,150,198,150);
ddaAlg(102,150,150,236);
ddaAlg(150,236,198,150);
getch();
closegraph();
return 0;
/*OUTPUT:-

*/
/*PROBLEM STATEMENT:-5 b)
Write C++ program to generate Hilbert curve using concept of fractals
*/

#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
using namespace std;
void move(int j,int h,int &x,int &y)
{
if(j==1)
y-=h;
else if(j==2)
x+=h;
else if(j==3)
y+=h;
else if(j==4)
x-=h;
lineto(x,y);
}
void hilbert(int r,int d,int l,int u,int i,int h,int &x,int &y)
{
if(i>0)
{
i--;
hilbert(d,r,u,l,i,h,x,y);
move(r,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(d,h,x,y);
hilbert(r,d,l,u,i,h,x,y);
move(l,h,x,y);
hilbert(u,l,d,r,i,h,x,y);
}
}
int main()
{
int n,x1,y1;
int x0=50,y0=150,x,y,h=10,r=2,d=3,l=4,u=1;
cout<<"\nGive the value of n: ";
cin>>n;
x=x0;y=y0;
int gm,gd=DETECT;
initgraph(&gd,&gm,NULL);
moveto(x,y);
hilbert(r,d,l,u,n,h,x,y);
delay(10000);
closegraph();
return 0;
}

*/OUTPUT:-

*/

*/PROBLEM STATEMENT:- 6.C)


Write OpenGL program to draw Sun Rise and Sunset.
*/

#include<iostream>
#include<graphics.h>
#include<cstdlib>
#include<dos.h>
#include<cmath>
using namespace std;
int main()
{
initwindow(800,500);
int x0,y0;
int gdriver = DETECT,gmode,errorcode;
int xmax,ymax;
errorcode=graphresult();
if(errorcode!=0)
{
cout<<"Graphics error:"<<grapherrormsg(errorcode);
cout<<"Press any ket to halt";
exit(1);
}
int i,j;
setbkcolor(BLUE);
setcolor(RED);
rectangle(0,0,getmaxx(),getmaxy());
outtextxy(250,240,"::::PRESS ANY KEY TO CONTINUE:::::");
while(!kbhit());
for(i=50,j=0;i<=250,j<=250;i+=5,j+=5)
{
delay(120);
cleardevice();
if(i<=150)
{
setcolor(YELLOW);
setfillstyle(1,YELLOW);
fillellipse(i,300-j,20,20);
}
else
{
setcolor(GREEN^RED);
setfillstyle(1,GREEN^RED);
fillellipse(i,300-j,20,20);
}
}
delay(1000);
cleardevice();
setcolor(RED);
setfillstyle(1,RED);
fillellipse(300,50,20,20);
delay(150);
int k,l;
for(k=305,l=55;k<=550,l<=300;k+=5,l+=5)
{
delay(120);
cleardevice();
if(k<=450)
{
setcolor(GREEN^RED);
setfillstyle(1,GREEN^RED);
fillellipse(k,l,20,20);
}
else
{
setcolor(YELLOW);
setfillstyle(1,YELLOW);
fillellipse(k,l,20,20);
}
}
return 0;
}
*/OUTPUT:-

*/

/*PROBLEM STATEMENT:-7.C)
Write C++ program to draw man walking in the rain with an umbrella.Apply the
concept of polymorphism*/

#include<iostream>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
using namespace std;
class walkingman
{
int rhx,rhy;
public:
void draw(int,int);
void draw(int);
};
void walkingman::draw(int i)
{
line(20,380,580,380);
if(i%2)
{
line(25+i,380,35+i,340);
line(45+i,380,35+i,340);
line(35+i,310,25+i,330);
delay(20);
}
else
{
line(35+i,340,35+i,310);
line(35+i,310,40+i,330);
delay(20);
}
line(35+i,340,35+i,310);
circle(35+i,300,10);
line(35+i,310,50+i,330);
line(50+i,330,50+i,280);
line(15+i,280,85+i,280);
arc(50+i,280,0,180,35);
arc(55+i,330,180,360,5);
}
void walkingman::draw(int x,int y)
{
int j;
rhx=x;
rhy=y;
for
(j=0;j<100;j++)
{
outtextxy(rand()%rhx,rand()%(rhy-50),"|");
setcolor(WHITE);
}
}
int main()
{
int gd=DETECT,gm;
int rhx,rhy,j,i;
walkingman obj;
initgraph(&gd,&gm,"");
for(i=0;i<500;i++)
{
obj.draw(i);
rhx=getmaxx();
rhy=getmaxy();
obj.draw(rhx,rhy);
delay(150);
cleardevice();
}
getch();
}

/*OUTPUT:-

*/

You might also like