OOPCGL
OOPCGL
Class: SE-CO
Div: A
Roll no: 14
OOP
PRACTICALS
Practical No. 1
/*
Implement a class Complex which represents
the
Complex Number data type. Implement the
following 1. Constructor (including a default
constructor which creates the complex number
0+0i).
2. Overload operator+ to add two complex
numbers. 3. Overload operator* to multiply
two complex numbers.
4. Overload operators << and >> to print and
read
Complex Numbers
*/
Practical No. 2
/*
Develop a program in C++ to create a database
of student’s information system
containing the following information: Name,
Roll
number, Class, Division,
Date of Birth, Blood group,
Contact address, Telephone number, Driving
license no. and other. Construct the database
with suitable member functions. Make use of
constructor, default constructor, copy
constructor,
destructor, static member functions, friend
class, this pointer, inline code and dynamic
memory allocation operators-new and delete as
well as exception handling.
*/
#include<iostream>
#include<string.h>
using namespace std;
class StudData;
class Student{
string name;
int roll_no;
string cls;
char* division;
string dob;
char*
bloodgroup;
static int count;
public:
Student() // Default
Constructor
{
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
{
delete
division;
delete[] bloodgroup;
}
static int getCount()
{
return count;
}
void
getData(StudData*);
void dispData(StudData*);
};
class
StudData{
string
caddress;
long int* telno;
long int* dlno;
friend class Student;
public:
StudData()
{
caddress="";
telno=new long;
dlno=new long;
}
~StudData()
{
delete
telno;
delete dlno;
}
void getStudData()
{
cout<<"Enter Contact Address : ";
cin.get();
getline(cin,caddress);
cout<<"Enter Telephone Number : ";
cin>>*telno;
cout<<"Enter Driving License Number
: "; cin>>*dlno;
}
void dispStudData()
{
cout<<"Contact Address :
"<<caddress<<endl;
cout<<"Telephone Number : "<<*telno<<endl;
cout<<"Driving License Number :
"<<*dlno<<endl;
}
};
inline void Student::getData(StudData* st)
{
cout<<"Enter Student Name : ";
getline(cin,name); cout<<"Enter
Roll Number : ";
cin>>roll_no;
cout<<"Enter Class : ";
cin.get();
getline(cin,cls);
cout<<"Enter Division :
"; cin>>division;
cout<<"Enter Date of Birth : ";
cin.get();
getline(cin,dob);
cout<<"Enter Blood Group : ";
cin>>bloodgroup;
st>getStudData();
count++;
}
inline void Student::dispData(StudData* st1)
{
cout<<"Student Name :
"<<name<<endl; cout<<"Roll
Number : "<<roll_no<<endl;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
cout<<"Date of Birth :
"<<dob<<endl; cout<<"Blood Group :
"<<bloodgroup<<endl;
st1->dispStudData();
}
int Student::count;
int main()
{
Student* stud1[100];
StudData* stud2[100];
int n=0; char ch;
do
{
stud1[n]=new Student;
stud2[n]=new
StudData;
stud1[n]->getData(stud2[n]); n++;
cout<<"Do you want to add another student
(y/n) :
";
cin>>ch;
cin.get();
} while (ch=='y' || ch=='Y');
for(int i=0;i<n;i++)
{
cout<<"------------------------------------------------------
---
------"<<endl;
stud1[i]->dispData(stud2[i]);
}
cout<<"------------------------------------------------------
------ ---"<<endl; cout<<"Total Students :
"<<Student::getCount();
cout<<endl<<"--------
------------------------------------------------------
"<<endl;
for(int i=0;i<n;i++)
{
delete
stud1[i];
delete stud2[i];
}
return 0;
}
Output:-
Enter Student Name : Enter Roll Number : Enter Class : Enter
Division : Enter Date of Birth : Enter Blood Group : Enter
Contact Address : Enter Telephone Number : Enter Driving License
Number : Do you want to add another student (y/n) :
------------------------------------------------------------
-
Student Name : Roll
Number : 0 Class :
Division :
Date of Birth : dd/mm/yyyy Blood
Group :
Contact Address :
Telephone Number : 0
Driving License Number : 0
------------------------------------------------------------
-
Total Students : 1
------------------------------------------------------------
---
Practical No. 3
/*
Imagine a publishing company which does
marketing for book and audiocassette versions.
Create a class publication that stores the title
(a string) and price (type float) of a
publication.From this class derive two classes:
book,
which adds a page count(type int), and
tape, which adds a playing time in
minutes(type float).
Write a program that instantiates the book and
tape classes,
allows user to enter data and
displays the data members.If an exception is
caught, replace all the data member values
with zero values.
*/
# include<iostream> #
include<stdio.h>
using namespace std;
class publication //
declaring class Publication
{
priva
te:
strin
g
title;
float
price
;
publi
c:
void
add(
)
{
cout << "\nEnter the Publication information :
" << endl;
cout << "Enter Title of the Publication : ";
cin.ignore(); getline(cin, title);
cout << "Enter Price of
Publication : "; cin >> price;
}
void display()
{
cout <<
"\n--------------------------------------------------";
cout << "\nTitle of Publication : " << title;
cout << "\nPublication Price : " << price;
}
}
;
class book : public publication // declaring
class book
which inherits class publication in public mode.
{
private:
int
page_co
unt;
public:
void add_book()
t
r
y
{
add();
cout << "Enter Page Count of Book
: "; cin >> page_count; if
(page_count <=
0)
{
throw page_count;
}
}
catch(...)
{
cout << "\nInvalid Page Count!!!";
page_count = 0;
}
}
void display_book()
{
display();
cout << "\nPage Count : " <<
page_count;
cout <<
"\n--------------------------------------------------\n";
}
};
class tape : public publication //
declaring class tape which inherits class
publication in public mode
{
private:
float
play_time;
public:
void add_tape()
{
t
r
y
{
add();
cout << "Enter Play Duration of the
Tape : "; cin >> play_time; if
(play_time <= 0) throw play_time;
}
catch(...)
{
cout << "\nInvalid Play Time!!!";
play_time = 0;
}
}
void display_tape()
{
display();
cout << "\nPlay Time : " <<
play_time << " min";
cout <<
"\n--------------------------------------------------\n";
}
};
int main()
{
book b1[10]; // object of class
book
tape t1[10]; // object
of class tape int ch, b_count = 0,
t_count = 0; do
{
cout << "\n* * * * * PUBLICATION DATABASE
SYSTEM *
* * * *";
cout <<
"\n--------------------MENU-----------------------";
cout << "\n1. Add Information to
Books"; cout << "\n2. Add
Information to Tapes"; cout
<< "\n3. Display Books Information"; cout <<
"\n4. Display Tapes Information";
cout << "\n5. Exit";
cout << "\n\nEnter your choice : ";
cin >> ch;
switch(ch)
{
case 1:
b1[b_count].add_book(); b_count
+ +;
bre
ak;
cas
e 2:
t1[t_count].add_t
ape(); t_count
+ +; break;
case 3:
cout << "\n* * * * BOOK PUBLICATION
DATABASE SYSTEM * * * *";
for (int j=0;j < b_count;j++)
{
b1[j].display_book();
}
brea
k;
case
4:
cout << "\n* * * * TAPE PUBLICATION
DATABASE SYSTEM * * * *"; for (int
j=0;j < t_count;j++)
{
t1[j].display_tape();
}
brea
k;
case
5:
exit(
0);
}
}while (ch != 5);
return 0;
}
Output:-
Practical No. 4
/*
Write a C++ program that creates an output file,
writes information to it, closes the file, open it
again as an input file and read the information
from the file.
*/
#include<iostream> #include<fstream>
using namespace std; class Employee
// declaring class employee
{
string
Name;
int ID;
double
salary;
public:
void
accept(
)
{
cout<<"\n Name : ";
cin.ignore();
getline(cin,Name);
cout<<"\n Id : ";
cin>>ID; cout<<"\n
Salary : ";
cin>>salary;
}
void display()
{
cout<<"\n Name : "<<Name;
cout<<"\n Id : "<<ID;
cout<<"\n Salary : "<<salary<<endl;
}
};
int main()
{
Employee o[5];
fstream f;
int i,n;
f.open("demo.txt",ios::out); cout<<"\n Enter
the number of employees you want to store : ";
cin>>n; for(i=0;i<n;i++)
{
cout<<"\n Enter information of Employee
"<<i+1<<"\n"; o[i].accept();
f.write((char*)&o[i],sizeof o[i]);
}
f.close();
f.open("demo.txt",ios::in); cout<<"\n
Information of Employees is as follows : \n";
for(i=0;i<n;i++)
{
cout<<"\nEmployee "<<i+1<<"\n";
f.write((char*)&o[i],sizeof o[i]);
o[i].display();
}
f.close();
return 0;
}
Output:-
srl@srl-Lenovo-G550:~/Desktop/OOPL$ g++
b16.cpp srl@srl-Lenovo-G550:~/Desktop/OOPL$
./a.out
How many employee information wanted to
store:3
Enter information of 3 employees (Enter name,
emp_id, salary)
Enter information of 0 employeea 1
100
Enter information of 1 employeeb 2
200
Enter information of 2 employeec 3
300
Information of employee is as follows a
1 100 b 2 200 c 3 300
Practical No. 5
/*
Write a function template for selection sort that
inputs, sorts and outputs an integer array and a
float array.
*/
#include<iostream>
using namespace std;
int n;
#define size 10
template<class T>
void sel(T A[size])
{
int i,j,min;
T temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
cout<<"\nSorted array:";
for(i=0;i<n;i++)
{
cout<<" "<<A[i];
}
}
int main()
{
int
A[size];
float
B[size];
int i; int ch;
do
{
cout<<"\n* * * * * SELECTION SORT SYSTEM *
* * * *";
cout<<"\n--------------------MENU-------------------
----";
cout<<"\n1. Integer
Values"; cout<<"\n2. Float
Values"; cout<<"\n3. Exit";
cout<<"\n\nEnter
your choice : "; cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter total no of int
elements:";
cin>>n;
cout<<"\nEnter int
elements:";
for(i=0;i<n;i++)
{
cin>>A[i];
}
sel(A);
break;
case 2:
cout<<"\nEnter total no of
float elements:";
cin>>n;
cout<<"\nEnter float
elements:";
for(i=0;i<n;i++)
{
cin>>B[i];
}
sel(B);
break;
case 3:
exit(0);
}
}while(ch!=3);
return 0;
}
Output:-
Selection sort
Integer Element
Enter how many elements you want 5
Enter the Integer element
7
5
8
9
3
Sorted list= 3 5 7 8 9
Practical No.6
/*
Write C++ Program using STL for sorting and
searching user defined records such as item
records using vector container.
*/
#include <iostream> //standard input output
stream header file
#include <algorithm> //The STL algorithms are
generic because they can operate on a variety of
data structures #include <vector> //The header
file for the STL vector library is vector.
using namespace std;
class Item // creating
class Item
{
public:
char name[10]; int quantity; int cost;
int code; bool operator==(const Item& i1)
//Boolean operators allow you to create more
complex conditional statements
{
if(code==i1.code) //operator will return 1 if the
comparison is true, or 0 if the comparison is
false return 1; return 0;
}
bool operator<(const Item& i1)
{
if(code<i1.code) //operator will return 1 if the
comparison is true, or 0 if the comparison is
false return 1; return 0;
} };
vector<Item>
o1; void
print(Item
&i1); void
display(); void
insert(); void
search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{
//if (i1.name != i2.name) return i1.cost <
i2.cost; return i1.cost < i2.cost;
}
int main()
{
in
t
ch
;
do
{
cout<<"\n* * * * * Menu * * * * *";
cout<<"\n1.Insert";
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
cout<<"\nEnter your
choice : "; cin>>ch;
switch(ch)
{
case 1:
insert(); break;
case 2:
display(); break;
case 3:
search(); break;
case 4:
sort(o1.begin(),o1.end(),compare);
cout<<"\n\n Sorted on Cost : "; display();
break;
case 5:
dlt(); break;
case 6:
exit(0);
}
}while(ch!=7);
return 0;
}
void insert()
{
Item i1; cout<<"\nEnter
Item Name : ";
cin>>i1.name;
cout<<"\nEnter Item
Quantity : ";
cin>>i1.quantity;
cout<<"\nEnter Item Cost : ";
cin>>i1.cost;
cout<<"\nEnter Item Code :
"; cin>>i1.code;
o1.push_back(i1);
}
void display()
{ for_each(o1.begin(),o1.end(),print);
}
void print(Item &i1)
{
cout<<"\n"; cout<<"\nItem Name :
"<<i1.name; cout<<"\nItem Quantity :
"<<i1.quantity; cout<<"\nItem Cost :
"<<i1.cost; cout<<"\nItem Code :
"<<i1.code; cout<<"\n\n";
}
void search()
{
vector<Item>::iterator p; Item i1;
cout<<"\nEnter Item Code to search :
"; cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found!!!";
}
else
{
cout<<"\nFound!!!";
}
}
void dlt()
{
vector<Item>::iterator p; Item i1;
cout<<"\nEnter Item Code to delete :
"; cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
{
cout<<"\nNot found!!!";
}
else
{
o1.erase(p);
cout<<"\nDeleted!!!";
}
}
Output:-
Practical No 7
include <iostream>
#include <map>
#include <string>
#include <utility>
using
namespace
std; int
main() {
typedef map<string, int> mapType; mapType
populationMap;
populationMap.insert(pair<string, int>("China",
1339)); populationMap.insert(pair<string,
int>("India", 1187));
populationMap.insert(mapType::value_type("U
S",
310));
populationMap.insert(mapType::value_type("In
donesi a
", 234));
populationMap.insert(make_pair("Brasil", 193));
populationMap.insert(make_pair("Pakistan",
170));
// Erase the end element using the erase
function
// Because it's ordered map (by key),
// map elements are not in the order of the
entry // In this map it's US since it's ordered
alphabetically. mapType::iterator iter =
--populationMap.end();
populationMap.erase(iter); // output the
size of the map
cout << "Size of populationMap: " <<
populationMap.size() << '\n'; for (iter =
populationMap.begin(); iter !=
populationMap.end(); ++iter) { cout << iter->first
<<": "
<< iter->second << " million\n";
}
// find will return an iterator to the matching
element if it is found
// or to the end of the map if the key is not
found string country("Indonesia"); iter =
populationMap.find(country); if( iter !=
populationMap.end() ) cout << country <<"'s
populations is "
<< iter->second << " million\n";
else
cout << "Key is not in populationMap" << '\n';
// clear the entries in the map
populationMap.clear();
}
Output:-
CG PRACTICALS:-
Practical No 1
Write C++ program to draw a concave
polygon and fill it with desired colour using
scan fill algorithm. Apply concept of
inheritance.
#include<iostream>
#include<graphics.h>
#include<algorithm>
using namespace std;
class Shape{
public:
void scanfill(int x[],int y[],int n)
{
int i, j, k, ymin = y[0], ymax = y[0],
xi[20];
for (i = 0; i < n;
i++) { if
(y[i] < ymin) ymin = y[i];
if (y[i] > ymax) ymax = y[i];
}
for (int s = ymin; s <= ymax; s++)
{ k = 0;
for (i = 0; i < n; i++)
{
j = (i + 1) ;
if ((y[i] < s && y[j] >= s)
|| (y[j] < s && y[i]
>= s))
{ xi[k++] = x[i] + (s - y[i])
* (x[j] - x[i]) / (y[j] - y[i]);}
}
sort(xi, xi + k);
for (i = 0; i < k - 1; i += 2)
{
line(xi[i], s,
xi[i + 1], s);
delay(5);
}
}
}
};
int main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm, NULL);
Shape obj;
int v;
cout<<"enter no of vertices for
polygon ; "; cin>>v; int
x[100]; int y[100]; for (int
i=0;i<v;i++)
{
cout<<"enter cords for x : ";
cin>>x[i];
cout<<"enter cordds for
y; "; cin>>y[i];
}
x[v]=x[0];
y[v]=y[0];
for (int
i=0;i<v;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
delay(200);
}
obj.scanfill(x,y,
v);
delay(2000);
closegraph();
return 0;
}
Output:-
Practical No 2
Write C++ program to implement cohen
Sutherland line clipping algorithm.
#include <graphics.h>
#include <math.h>
#include <iostream>
using namespace std;
const int INSIDE =
0; const int LEFT
= 1; const int
RIGHT = 2;
const int BOTTOM
= 4;
const int TOP = 8;
class Clipping {
public:
int x1, y1, x2, y2;
Clipping(int x_start, int y_start, int
x_end, int y_end) {
x1
= x_start;
y1 = y_start;
x2 = x_end;
y2 = y_end;
}
void drawLine(int color) {
setcolor(color);
line(x1, y1, x2, y2);
}
void drawWindow() {
setcolor(WHITE);
rectangle(100,100,300,300);
}
int computeCode(int x, int y) {
int code = INSIDE;
if (x <
100)
code = LEFT;
else if (x > 300)
code = RIGHT;
if (y < 100)
code = BOTTOM;
else if (y > 300)
code = TOP;
return code;
}
void
cohenSutherlandClip() {
int code1 = computeCode(x1,
y1); int code2 =
computeCode(x2, y2);
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 ==
0)) {
accept = true;
break;
} else if (code1 & code2) {
break;
} else {
int code_out;
int x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if (code_out & TOP) {
• = x1 + (x2 - x1) * (300 - y1) / (y2 - y1);
y = 300;
} else if (code_out &
BOTTOM) { x=
x1 + (x2 - x1) * (100 - y1) / (y2 - y1);
y = 100;
} else if (code_out &
RIGHT) { y = y1
+ (y2 - y1) * (300 - x1) / (x2 - x1);
x = 300;
} else if (code_out &
LEFT) {
• = y1 + (y2 - y1) * (100 - x1) / (x2 - x1);
x = 100;
}
if
(code_out == code1) {
x1 = x;
y1 = y;
code1 =
computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 =
computeCode(x2, y2);
}
}
}
if (accept) {
cleardevice();
drawWindow();
drawLine(GREEN);
cout<<x1<<y1<<x2<<y2;
} else {
cout << "Line rejected.\n";
}
}
};
int main() {
int gd = DETECT, gm;
char data[] = "C:\\MinGW\\lib\\libbgi.a";
//static file
initgraph(&gd, &gm, data);
int x1, y1, x2, y2;
cout << "Enter the endpoints of the line
(x1 y1 x2 y2): ";
cin >> x1 >> y1 >> x2 >> y2;
Clipping clip(x1, y1, x2, y2);
clip.drawWindow();
clip.drawLine(RED);
delay(2000);
clip.cohenSutherlandC
lip();
delay(2000);
closegraph();
return 0;
}
Output:-
Practical No 3
Write a c++ program to draw the following
pattern . Use DDA line and Bresenham
algorithm. Apply concept of encapsulation.
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
class coord{
public:
int
dx,dy,steps;
float xi,yi;
int x,y,d;
};
class draw:public coord{
public:
void ddaline(int x1,int
y1,int x2,int y2){
dx=x2-x1; dy=y2-y1;
if(abs(dx)>abs(dy)){
steps=dx;
}else{
steps=dy;
}
xi=dx/(float)steps;
yi=dy/(float)steps;
float x=x1;
float y=y1;
for(int i=0;i<=steps;i++){
putpixel(round(x),round(y),WHITE);
x+=xi; y+=yi;
}
}
void bresenhamcircle(int xcenter,int
ycenter,int radius){
x=0;
y=radius;
d=3-2*radi
us;
while(x<=y)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,WHITE);
putpixel(xcenter+x,ycenter-y,WHITE);
putpixel(xcenter-x,ycenter-y,WHITE);
putpixel(xcenter+y,ycenter+x,WHITE);
putpixel(xcenter-y,ycenter+x,WHITE);
putpixel(xcenter+y,ycenter-x,WHITE);
putpixel(xcenter-y,ycenter-x,WHITE);
if(d<0){
d=d+4*x+6;
}
else{
d=d+4*(x-y)+10;
y--;
}
x++;
}
}
};
int main(){
int
gd=DETECT,
gm;
char
data[]="c:\\mingw\\lib\\libbgi.a";
initgraph(&gd,&gm,data);
draw drawer;
int choice;
do{
cout<<"choose an
option:\n"; cout<<"1.
draw a line\n";
cout<<"2. draw a circle\n";
cout<<"enter your choice (1 or
2): ";
cin>>choice;
switch(choice){
case 1:{
int x1,y1,x2,y2;
cout<<"enter the
coordinates of the first point (x1 y1): ";
cin>>x1>>y1;
cout<<"enter the
coordinates of the second point (x2 y2): ";
cin>>x2>>y2;
drawer.ddaline(x1,y1,x2,y2);
break;
}
case 2:{
int
xcenter,ycenter,radius;
cout<<"enter the center of the circle (x y):
";
cin>>xcenter>>ycenter;
cout<<"enter the radius
of the circle: ";
cin>>radius;
drawer.bresenhamcircle(xcenter,ycenter,radi
us); break;
}
default:
cout<<"invalid choice!
please run the
program again.";
}
}while(choice<3);
closegraph();
return 0;
}
Output:-
Practical No 4
//Write c++ program to draw 2-D object
and perform following basic
transformation a] Scaling b] Rotation c]
Translation, Apply the concept of
overloading.
#include <iostream>
#include <graphics.h>
#include <math.h>
using namespace std;
class Transform {
public: int m;
int a[20][3];
int c[20][3];
void draw(int
x[20][3]) {
cleardevice();
for (int i = 0; i < m;
i++) {
line(x[i][0], x[i][1], x[(i +
1)%m][0], x[(i +
1)%m][1]);
delay(100);
}
}
void
operator*(float b[3][3]) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < 3; j++) {
c[i][j] = 0;
for (int k = 0; k < 3; k++) {
c[i][j] += (a[i][k] *
b[k][j]);
}
}
}
}
};
int main() { int gd
= DETECT, gm;
initgraph(&gd, &gm,
NULL);
Transform t;
cout << "\nEnter the Number of Edges:
";
cin >> t.m;
cout << "\nEnter the Coordinates (x y):
";
for (int i = 0; i <
t.m; i++) { cin
>> t.a[i][0] >> t.a[i][1];
t.a[i][2] = 1;
}
t.draw(t.a
);
int ch;
do {
cout << "\nEnter your
choice"; cout << "\n1.
Translation"
<< "\n2. Scaling"
t.draw(t.a);
delay(500);
t.draw(t.c);
break;
}
case 2: {
float sx, sy;
cout <<
"\nSCALING
OPERATION\
n";
cout <<
"Enter value
for sx and sy:
";
cin >> sx >> sy;
b[0][0] = sx; b[0][1] = 0;
b[0][2] = 0; b[1][0] = 0;
b[1][1] = sy; b[1][2] = 0;
b[2][0] = 0;
b[2][1] = 0; b[2][2] = 1;
t *b;
t.draw(t.a);
delay(500);
t.draw(t.c);
break;
}
case 3: {
float deg, theta;
cout << "\nROTATION
OPERATION\n"; cout
<< "Enter value for angle (in degrees):
";
cin >> deg;
theta = deg * (3.14 /
180);
b[0][0] = cos(theta);
b[0][1] = sin(theta); b[0][2] = 0;
b[1][0] = -sin(theta);
b[1][1] = cos(theta); b[1][2] = 0;
b[2][0] = 0;
b[2][1] = 0; b[2][2] = 1;
t *b;
t.draw(t.a);
delay(500);
t.draw(t.c);
break;
}
case 4:
cout << "Exiting...";
break;
default:
cout << "\nInvalid
choice";
break;
}
} while
(ch < 4);
closegraph();
return 0;
}
Output:
Scaling operation
Rotation operation
Translation:-
Practical No 5
Write C++ program to generate fractal
pattern by using koch curves
#include <iostream>
#include <graphics.h>
#include <cmath> using
namespace std;
class KochCurve { public:
void drawKochCurve(float x1, float y1,
float x2, float y2, int depth) {
if (depth == 0) {
line(x1, y1, x2, y2);
} else {
float x3 = (2*x1 + x2) / 3;
float y3 = (2*y1 + y2) / 3;
float x5 = (x1 + 2*x2) / 3;
float y5 = (y1 + 2*y2) / 3;
float x4 = (x3 + x5) / 2 -
(sqrt(3)*(y5 - y3))/2;
float y4 = (y3 + y5) / 2 + (sqrt(3)*(x5 - x3))/2;
drawKochCurve(x1, y1, x3, y3, depth - 1);
drawKochCurve(x3, y3, x4, y4, depth - 1);
drawKochCurve(x4, y4, x5, y5, depth - 1);
drawKochCurve(x5, y5, x2, y2, depth - 1);
}
}
void startKochCurve(int depth) {
float x1 = 100, y1 =
300; float x2 = 500,
y2 = 300;
drawKochCurve(x1, y1, x2, y2,
depth);
}
};
int main() { int
gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
KochCurve
koch; int
depth;
cout << "Enter the depth of the Koch
curve : "; cin >> depth;
koch.startKochCurve(depth);
getch();
closegraph();
return 0;
}
Output:-
Practical no 6
Write C++ program to draw the line styles
using DDA and Bresenham‘s algorithm (solid,
dotted, dashed, dash dot and thick). Inherit
pixel class and Use Constructors.
#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
using namespace std;
int xmax,ymax,xmid,ymid;
class Line
{
public:
int x1,x2,y1,y2,ch;
void bss(int x1,int y1,int x2,int y2)
{
int
dx,dy,x,y,s1,s2,ex,e,i,flag=0,temp;
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
putpixel(x+xmid,ymid-y,15);
if(x2>x1)
{
s1=1;
}
if(x2==x1)
{
s1=0;
}
if(x2<x1)
{
s1=-1;
}
if(y2>y1)
{
s2=1;
}
if(y2==y1)
{
s2=0;
}
if(y2<y1)
{
s2=-1;
}
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
ex=1;
}
else
ex=0;
e=2*dy-dx;
i=1;
do
{
while(e>0)
{
if(ex==1)
x=x+s1;
else
y=y+s2;
e=e-2*dx;
}
while(e<0)
{
if(ex==1)
y=y+s2;
else
x=x+s1;
e=e+2*dy;
}
switch(ch)
{
case 1:
putpixel(x+xmid,ymid-y,15);
break;
case 2:
if(flag==0)
{putpixel(x+xmid,ymid-y,15);
delay(1000);}
if(i%5==0)
{
if(flag==1)
flag=0;
else
flag=1;
}
break;
case 3:
if(flag==0)
{
putpixel(x+xmid,ymid-y,15);
delay(100);}
if(i%5==0)
{
if(flag==1)
flag=0;
else
flag=1;
}
if(i%3==0)
{
putpixel(x+xmid,ymid-y,15);
delay(1000);}
break;
case 4:
if(flag==0)
{delay(1000);
}
else
{
if(i%3==0)
{
putpixel(x+xmid,ymid-y,15);
delay(1000);
}
}
break;
case 5:
putpixel(x+xmid,ymid-y,15);
break;
}
i=i+1;
delay(50);
}while(i<=
dx);
}
}
;
int main()
{
int gd=DETECT,gm;
int
x1,y1,x2,y2,thick,wx,
wy,i; Line B;
cout<<"Enter two end points of
line\n"; cin>>x1>>y1;
cin>>x2>>y2;
while(1)
{
cout<<"\nEnter the
Style\n";
cout<<"1.Simple\n";
cout<<"2.Dash\n";
cout<<"3.Dash
Dot\n";
cout<<"4.Dot\n";
cout<<"5.Thick\n";
cout<<"6.Exit\n";
cout<<"Enter your
Style\n"; cin>>B.ch;
if(B.ch==5)
{
cout<<"Enter The Thickness of line: ";
cin>>thick;
}
initgraph(&gd,&gm,N
ULL);
xmax=getmaxx();
ymax=getmaxy();
xmid=xmax/2;
ymid=ymax/2;
if(B.ch<=4)
{
B.bss(x1,y1,x2,y2);
delay(300);
}
else
{
B.bss(x1,y1,x2,y2);
delay(300);
if((y2-y1)/(x2-x1)<1)
{
wy=(thick-1)*sqrt(pow((x2-x1),2)+pow((y2y1)
,2))/(2*fabs(x2-x1));
for(i=0;i<wy;i++)
{
B.bss(x1,y1-i,x2,y2-i);
delay(300);
B.bss(x1,y1+i,x2,y2+i);
delay(300);
}
}
else
wx=(thick-1)*sqrt(pow((x2-x1),2)+pow((y2y1)
,2))/(2*fabs(y2-y1));
for(i=0;i<wx;i++)
{
B.bss(x1-i,y1,x2-i,y2);
delay(300);
B.bss(x1+i,y1,x2+i,y2);
delay(300);
}
}
if(B.ch==6)
{
cout<<"Exiting....";
exit(1);
}
closegraph();
}
return 0;
}
Output:-
Practical No 7
Write C++ program to draw man walking
in the rain with an umbrella. Apply the
concept of Polymorphism.
#include <iostream>
#include <graphics.h>
#include<cstdlib>
using namespace std;
class Animation { public:
void drawRain(int
frame) {
for (int i = 0; i < 100; i++) {
int x = rand() % 600;
int y = rand() % 400;
line(x, y, x+2 , y + 10);
}
}
void walk() { for (int i =
0; i < 500; i++) {
line(0, 400, 600, 400);
drawRain(i);
circle(50 + i, 250, 15);
line(50 + i, 265, 50 + i,
350);
if (i % 4 == 0) {
line(50 + i, 350, 80 + i , 400);
line(50 + i, 350, 20 + i, 400);
} else {
line(50 + i,
350, 80 + i, 400);
line(50 + i, 350, 20 +
i, 400);
}
line(50 + i, 295, 20 + i,
330);
line(50 + i, 295, 80 + i, 330);
arc(100 + i, 220, 360, 180,
50); line(100 + i, 220,
100 + i, 310);
line(50 + i, 220, 150 + i, 220);
delay(100);
if (i < 499) {
cleardevice();
}
}
}
};
int main() { int
gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
Animation animation;
animation.walk();
closegraph();
return 0;
}
Output:-
CG Mini
Project
• TITLE: Mini Project
• AIM: Program to display moving cycle
Animation Using
C++
• OBJECTIVES: To Understand and Implement
OpenGL Concepts.
• HARDWARE REQUIREMENTS:
1. Standard QWERTY serial or PS/2 keyboard.
2. AT&T Compatible mouse.
3. Processor speed of 300MHz and above.
4. 128Mb of RAM.
5. 200Mb of minimum disk space.
6.VGA256colourvideowithmin16bitcolourqualit
7. Pentium 3 processor.
• SOFTWARE REQUIREMENTS:
1. A visual C++ compiler [Microsoft Visual
Studio]. 2.
OpenGLlibraries-gl,glut,OpenGLandglaux.
3. Windows xp operating system.
• THEORY: Creating a moving cycle animation in
C++ can be an interesting project, involving
graphics programming, animation, and basic
game development concepts. Here's an outline
of how you might approach this project,
focusing on key steps and concepts.
• IMPLEMENTATION:
// C++ program to draw the moving
// cycle using computer graphics
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>
// Driver code
int main ()
{
int gd = DETECT, gm, i, a;
// Wheel
circle(150 + i, 405, 30);
circle(50 + i, 405, 30);
// Road
line(0, 436, getmaxx(), 436);
// Stone
rectangle(getmaxx() - i, 436,
650 - i, 431);
getch();