0% found this document useful (0 votes)
63 views19 pages

A05 A06 A07 Merged

The document describes a C++ program that implements Cohen-Sutherland line clipping algorithm. It includes function definitions for drawing the window boundaries, setting codes for points, checking point visibility, and resetting end points if needed. The main function takes input coordinates, draws the line without and with clipping, and handles the different clipping cases by calling the defined functions.

Uploaded by

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

A05 A06 A07 Merged

The document describes a C++ program that implements Cohen-Sutherland line clipping algorithm. It includes function definitions for drawing the window boundaries, setting codes for points, checking point visibility, and resetting end points if needed. The main function takes input coordinates, draws the line without and with clipping, and handles the different clipping cases by calling the defined functions.

Uploaded by

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

1

Name :- Sonu Shriram Vishwakarma

Subject :- Computer Graphics lab

Roll No. :- SE63


----------------------------------------------------------------------------------

Assignment No. 6
Problem Statement: Write C++ Program To Generate Fractal Patterns By Using
Koch Curves

Source Code :-
#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;
}

Output:
1
2
Name :- Sonu Shriram Vishwakarma
Subject :- Computer Graphics
Roll No. :- SE63
Assignment :- Assignment No. 7

Problem Statement: Write a Program To Implement The Game Tic


Tac Toe. Apply The Concept of Polymorphism.

Source Code:
//Tic Tac Toe Game in C++
//Importing the inbuild libraries in CPP
#include <iostream>
#include <stdlib.h>
using namespace std;
//Array for the board
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
//Variable Declaration
int choice;
int row,column;
char turn = 'X';
bool draw = false;
//Function to show the current status of the gaming board
void display_board(){
//Rander Game Board LAYOUT
cout<<"PLAYER - 1 [X]t PLAYER - 2 [O]nn";
cout<<"tt | | n";
cout<<"tt "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" n";
cout<<"tt_____|_____|_____n";
cout<<"tt | | n";
cout<<"tt "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" n";
cout<<"tt_____|_____|_____n";
cout<<"tt | | n";
cout<<"tt "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" n";
cout<<"tt | | n";
}
//Function to get the player input and update the board
void player_turn(){
if(turn == 'X'){
cout<<"ntPlayer - 1 [X] turn : ";
}
else if(turn == 'O'){
cout<<"ntPlayer - 2 [O] turn : ";
}
//Taking input from user
//updating the board according to choice and reassigning the turn Start
cin>> choice;
//switch case to get which row and column will be update
switch(choice){
case 1: row=0; column=0; break;
case 2: row=0; column=1; break;
case 3: row=0; column=2; break;
case 4: row=1; column=0; break;
case 5: row=1; column=1; break;
case 6: row=1; column=2; break;
case 7: row=2; column=0; break;
case 8: row=2; column=1; break;
case 9: row=2; column=2; break;
default:
cout<<"Invalid Move";
}
if(turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O'){
//updating the position for 'X' symbol if
//it is not already occupied
board[row][column] = 'X';
turn = 'O';
}else if(turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O'){
//updating the position for 'O' symbol if
//it is not already occupied
board[row][column] = 'O';
turn = 'X';
}else {
//if input position already filled
cout<<"Box already filled!n Please choose another!!nn";
player_turn();
}
/* Ends */
display_board();
}
//Function to get the game status e.g. GAME WON, GAME DRAW GAME IN CONTINUE MODE

bool gameover(){
//checking the win for Simple Rows and Simple Column
for(int i=0; i<3; i++)
if(board[i][0] == board[i][1] && board[i][0] == board[i][2] || board[0][i] ==
board[1][i]
&& board[0][i] == board[2][i])
return false;
//checking the win for both diagonal
if(board[0][0] == board[1][1] && board[0][0] == board[2][2] || board[0][2] ==
board[1][1]
&& board[0][2] == board[2][0])
return false;
//Checking the game is in continue mode or not
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(board[i][j] != 'X' && board[i][j] != 'O')
return true;
//Checking the if game already draw
draw = true;
return false;
}
//Program Main Method
int main()
{
cout<<"tttT I C K -- T A C -- T O E -- G A M Ettt";
cout<<"nttttFOR 2 PLAYERSnttt";
while(gameover()){
display_board();
player_turn();
gameover();
}
if(turn == 'X' && draw == false){
cout<<"nnCongratulations!Player with 'X' has won the game";
}
else if(turn == 'O' && draw == false){
cout<<"nnCongratulations!Player with 'O' has won the game";
}
else
cout<<"nnGAME DRAW!!!nn";
}
Output:
1
2
3
4
5
6
Name: Sonu Shriram Vishwakarma
Roll No. : SE63

Assignment No. 2
Title : Write C++ program to implement Cohen Southerland line clipping algorithm.

Sample Code:
#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(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);
}

Input :
x1 , y1:
200
200

x2, y2:
600
100

Output :

Without clipping :

With clipping :

You might also like