CG

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Practical No: 1

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

// algorithm. Apply the concept of inheritance.

#include <conio.h>

#include <iostream>

#include <graphics.h>

#include <stdlib.h>

using namespace std;

//Declaration of class point

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++) //ACCEPT THE VERTICES

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.";

//FUNCTION FOR FINDING

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;

//DISPLAY FUNCTION

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) //DEFINE FUNCTION INTS

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 // used to make changes in x. so that we can fill

//our polygon after certain distance

x=((x2-x1)*(z-y1))/(y2-y1);

x=x+x1;

if(x<=xmax && x>=xmin)

inter[c++]=x;

}}

void poly::sort(int z) //SORT FUNCTION {

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); // used to make

//hollow outlines of a polygon

delay(100);

for(i=0; i<c;i+=2) {

delay(100);

line(inter[i],z,inter[i+1],z); // Used to fill the polygon ....

int main() //START OF 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(); //CLOSE OF GRAPH

getch();

return 0;

Output:
Practical No: 2

// line clipping
#include<iostream>

#include<stdlib.h>

#include<math.h>

#include<graphics.h>

#include<dos.h>

using namespace std;

typedef struct coordinate

int x,y;

char code[4];

}PT;

void drawwindow();

void drawline(PT p1,PT p2);

PT setcode(PT p);

int visibility(PT p1,PT p2);

PT resetendpt(PT p1,PT p2);

int main()

int gd=DETECT,v,gm;

PT p1,p2,p3,p4,ptemp;

cout<<"\nEnter x1 and y1\n";

cin>>p1.x>>p1.y;

cout<<"\nEnter x2 and y2\n";

cin>>p2.x>>p2.y;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

drawwindow();

delay(5000);

drawline(p1,p2);

delay(5000);

cleardevice();

delay(5000);

p1=setcode(p1);

p2=setcode(p2);
v=visibility(p1,p2);

delay(5000);

switch(v)

case 0: drawwindow();

delay(5000);

drawline(p1,p2);

break;

case 1: drawwindow();

delay(5000);

break;

case 2: p3=resetendpt(p1,p2);

p4=resetendpt(p2,p1);

drawwindow();

delay(5000);

drawline(p3,p4);

break;

delay(5000);

closegraph();

return 0;

void drawwindow()

line(150,100,450,100);

line(450,100,450,350);

line(450,350,150,350);

line(150,350,150,100);

void drawline(PT p1,PT p2)

line(p1.x,p1.y,p2.x,p2.y);

PT setcode(PT p) //for setting the 4 bit code

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)

ptemp.code[3]='1'; //Left

else

ptemp.code[3]='0';

ptemp.x=p.x;

ptemp.y=p.y;

return(ptemp);

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);

PT resetendpt(PT p1,PT p2)

PT 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[0]=='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);

Output:
Practical No: 3

/*EXPERIMENT Write C++ program to draw the following pattern. Use DDA

line and Bresenham circle algorithm. Apply the concept of

encapsulation. */
#include <graphics.h>

#include<iostream>

#include <math.h>

using namespace std;

void circlebres(float x1,float y1,float r)

float x,y,p;

x=0;

y=r;

p=3-(2*r); // initial decision parameter

while(x<=y)

putpixel(x1+x,y1+y,WHITE); /* drawing pixel in each

octant*/

putpixel(x1-x,y1+y,WHITE);

putpixel(x1+x,y1-y,WHITE);

putpixel(x1-x,y1-y,WHITE);

putpixel(x1+y,y1+x,WHITE);

putpixel(x1+y,y1-x,WHITE);

putpixel(x1-y,y1+x,WHITE);

putpixel(x1-y,y1-x,WHITE);

x=x+1;

if(p<0)

p=p+4*(x)+6;

else

p=p+4*(x-y)+10;

y=y-1;
}

delay(40);

void drawline(int x1,int y1,int x2,int y2)

int dx,dy,m,s;

float xi,yi,x,y;

dx = x2 - x1;

dy = y2 - y1;

if (abs(dx) > abs(dy))

s = abs(dx);

else

s = abs(dy);

xi = dx / (float) s;

yi = dy / (float) s;

x = x1;

y = y1;

putpixel(x1, y1, WHITE);

for (m = 0; m < s; m++)

putpixel(x, y, WHITE);

x += xi;

y += yi;

delay(500);

int main()

int xc,yc,r;

cout<<" enter center coordinates : ";

cin>>xc>>yc;

cout<<"enter redius : ";

cin>>r;

int gd=DETECT,gm=DETECT,x1,y1,x2,y2;

initgraph(&gd,&gm,NULL);
circlebres(xc,yc,r); //inside circle

double height,side;

//side=r/0.577;

//height=1.73*side;

side=1.73*r;

height=1.73*side;

drawline(xc-side,yc+r,xc+side,yc+r); //base line

delay(300);

drawline(xc-side,yc+r,xc,yc+r-height);// left line

drawline(xc,yc+r-height,xc+side,yc+r); // right line

circlebres(xc,yc,height-r);//outer circle

delay(3000);

closegraph();

Output:
Practical No: 4

// Write C++ program to draw 2-D object and perform following basic transformations,

//Scaling b) Translation c) Rotation. Apply the concept of operator overloading.


#include<iostream>

#include<math.h>

#include<graphics.h>

using namespace std;

class matrix {

public:

int n,i,j,tx,ty,k,sum,sx,sy;

double a[6][3],b[6][3],mult[6][3],mat3[6][3];

double p,q,r;

double ang=0,angle=0;

public:

void get()

cout<<"\n enter the number of vertices of polygon : ";

cin>>n;

// cout<<"n Entering user matix\n";

for(i=0;i<n;i++)

cout<<"enter x n y co ordinates";

cin>>b[i][0];

cin>>b[i][1];

b[i][2]=1;

//display object matrix

cout<<"\n original co ordinates are"<<"\n";

for(i=0;i<n;i++)

for(j=0;j<3;j++)

cout<<b[i][j]<<"\t";

}cout<<"\n";

}
void identitymat()

for(i=0;i<n;i++)

for(j=0;j<3;j++)

if(i==j)

a[i][j]=1;

else

a[i][j]=0;

}}

}}

void trans()

cout<<"enter values of tx and ty";

cin>>tx>>ty;

a[2][0]=tx;

a[2][1]=ty;

cout<<"matrix is"<<"\n";

for(i=0;i<n;i++) {

for(j=0;j<3;j++)

cout<<a[i][j]<<"\t";

}cout<<"\n";

void scale() {

cout<<"\n Enter the values of sx and sy";

cin>>sx>>sy;

a[0][0]=sx;

a[1][1]=sy;

cout<<"\n Matrix is:"<<"\n";

//To display scaling matrix


for(i=0;i<3;i++) {

for(j=0;j<3;j++) {

cout<<a[i][j]<<"\t";

}cout<<"\n";

}}

void rot()

cout<<"Enter the angle";

cin>>ang;

angle=(ang*3.142)/180;

q=sin(angle);

p=cos(angle);

r=-sin(angle);

a[0][0]=p;

a[0][1]=q;

a[1][0]=r;

a[1][1]=p;

cout<<"tranformation matrix is"<<"\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++) {

cout<<a[i][j]<<"\t";

}cout<<"\n";

void multi() {

cout<<"\nMultiplying two matrices...";

for(i=0; i<n; i++)

for(j=0; j<3; j++) {

sum=0;

for(k=0; k<3; k++) {

sum = sum + b[i][k] * a[k][j];

mat3[i][j] = sum;

} }
}

void display() {

cout<<"\nMultiplication of two Matrices : \n";

for(i=0; i<n; i++)

for(j=0; j<3; j++)

cout<<mat3[i][j]<<" ";

cout<<"\n";

int gd=DETECT,gm;

initgraph(&gd,&gm,NULL);

for(int i=0;i<n-1;i++)

line(b[i][0],b[i][1],b[i+1][0],b[i+1][1]);

line(b[2][0],b[2][1],b[0][0],b[0][1]);

for(int i=0;i<n-1;i++)

line(mat3[i][0],mat3[i][1],mat3[i+1][0],mat3[i+1][1]);

line(mat3[2][0],mat3[2][1],mat3[0][0],mat3[0][1]);

delay(5000);

closegraph();

};

int main() {

matrix g;

int ch;

char ans;

g.get();

g.identitymat();

do {

cout<<"menu\n1.translation\n2.scaling\n3.rotation";

cin>>ch;
switch(ch)

case 1:

g.trans();

g.multi();

g.display();

break;

case 2:

g.scale();

g.multi();

g.display();

break;

case 3:

g.rot();

g.multi();

g.display();

break;

}cin>>ans;

}while(ans=='Y'&& ans=='y');

return 0;

Output:
Practical No: 5

// Write C++ program to generate fractal patterns by using Koch curves.


#include<iostream>

#include<graphics.h>

#include<math.h>

using namespace std;

void snow(int x1, int y1, int x2, int y2, int it)

float angle = 60*M_PI/180;

int x3 = (2*x1+x2)/3;

int y3 = (2*y1+y2)/3;

int x4 = (x1+2*x2)/3;

int y4 = (y1+2*y2)/3;

int x = x3+(x4-x3)*cos(angle)+(y4-y3)*sin(angle);

int y = y3-(x4-x3)*sin(angle)+(y4-y3)*cos(angle);

if(it > 0)

snow(x1, y1, x3, y3, it-1);

snow(x3, y3, x, y, it-1);

snow(x, y, x4, y4, it-1);

snow(x4, y4, x2, y2, it-1);

else

line(x1, y1, x3, y3);

line(x3, y3, x, y);

line(x, y, x4, y4);

line(x4, y4, x2, y2);

int main()

int gd = DETECT,gm;

initgraph(&gd, &gm, NULL);

int x1 = 150, y1 = 100, x2 = 350, y2 = 100;


snow(x1, y1, x2, y2,2);

snow(250,350,150,100,2);

snow(350,100,250,350,2);

getch();

return 0;

Output:

Snowflow:
#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(0.5);

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;

Output:
Practical No: 6
#include<iostream>

#include<graphics.h>

#include<cstdlib>

#include<conio.h> // Added for kbhit()

#include<cmath> // Math functions (though not used in this code)

using namespace std;

int main()

initwindow(800, 500); // Initialize graphics window

int x0, y0;

int gdriver = DETECT, gmode, errorcode;

int xmax, ymax;

errorcode = graphresult(); // Get graphics initialization result

if (errorcode != grOk)

cout << "Graphics error: " << grapherrormsg(errorcode) << endl;

cout << "Press any key to halt";

exit(1); // Exit if there is a graphics error

int i, j;

setbkcolor(BLUE); // Set background color to BLUE

setcolor(RED); // Set drawing color to RED

rectangle(0, 0, getmaxx(), getmaxy()); // Draw a rectangle around the window

outtextxy(250, 240, "::::PRESS ANY KEY TO CONTINUE:::::");

while (!kbhit()); // Wait for a key press

for (i = 50, j = 0; i <= 250 && j <= 250; i += 5, j += 5)

delay(120);

cleardevice(); // Clear screen

if (i <= 150)

{
setcolor(YELLOW); // Set color to YELLOW

setfillstyle(1, YELLOW); // Set fill pattern to solid YELLOW

fillellipse(i, 300 - j, 20, 20); // Draw filled ellipse

setcolor(GREEN); // Set color to GREEN

setfillstyle(1, GREEN); // Set fill pattern to solid GREEN

fillellipse(i, 300 - j, 20, 20); // Draw filled ellipse

delay(1000);

cleardevice();

setcolor(RED); // Set color to RED

setfillstyle(1, RED); // Set fill pattern to solid RED

fillellipse(300, 50, 20, 20); // Draw filled ellipse

delay(150);

int k, l;

for (k = 305, l = 55; k <= 550 && l <= 300; k += 5, l += 5)

delay(120);

cleardevice(); // Clear screen

if (k <= 450)

setcolor(GREEN); // Set color to GREEN

setfillstyle(1, GREEN); // Set fill pattern to solid GREEN

fillellipse(k, l, 20, 20); // Draw filled ellipse

setcolor(YELLOW); // Set color to YELLOW

setfillstyle(1, YELLOW); // Set fill pattern to solid YELLOW

fillellipse(k, l, 20, 20); // Draw filled ellipse

return 0;

}
Output:
Practical No: 7
#include<stdio.h>

#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); // rain

void draw(int); // walking man

};

void WalkingMan::draw(int i){

line(20,380,580,380); //platform

if(i%2==0) {

line(25+i,380,35+i,340); //leftleg

line(45+i,380,35+i,340);//right leg

line(35+i,310,25+i,330);//left hand

delay(20);

Else {

line(35+i,380,35+i,340);

line(35+i,310,40+i,330);

delay(20);

line(35+i,340,35+i,310); //body

circle(35+i,300,10); //head

line(35+i,310,50+i,330); // hand

line(50+i,330,50+i,280); //umbrella stick

line(15+i,280,85+i,280); //umbrella right

arc(50+i,280,0,180,35); //umbrella body

arc(55+i,330,180,360,5);//umbrella handle arc(x, y, start_angle, end_angle, radius);

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+=5). {

obj.draw(i);

rhx=getmaxx();

rhy=getmaxy();

obj.draw(rhx,rhy);

delay(150);

cleardevice();

getch();

Output:

You might also like