CG Practical File
CG Practical File
Graphics
Practical File
Code:
#include <iostream>
#include <graphics.h>
initwindow(400, 300);
setbkcolor(9);
line(80,100,300,100);
// ri
// ti
// k
setbkcolor(BLACK);
setfillstyle(0, 0);
getch();
return 0;
Output:
2. Draw five concentric circles filled with different colors.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
setfillstyle(1, i);
getch();
return 0;
Output:
3. Draw circles that move in a pattern of increasing and decreasing radius.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(808, 800);
while (true){
if (i <= 200){
r++;
r--;
r++;
r--;
cleardevice();
if (i >= 800-r){
turn = "b";
i += 5;
if (i <= 0+r){
turn = "f";
i -= 5;
delay(1);
getch();
return 0;
Output:
4. Write a program of a moving car.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
if (turn == "f"){
if (i >= 590){
turn = "b";
i += 10;
if (i <= 210){
turn = "f";
i -= 10;
// body of car
setcolor(BLACK);
getch();
return 0;
Output:
5. Write a program to draw random circles on the screen with random position, random radius, random
pattern and color.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int x, y, r = 0;
while (true){
x = rand();
y = rand();
r = rand();
break;
int k = rand();
if (k % 2 == 0){
circle(x, y, r);
else{
setfillstyle(i, i);
fillellipse(x, y, r, r);
// delay(16);
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
#include <time.h>
int main(){
initwindow(800, 800);
int i, x, y, j;
srand(time(NULL));
// man
if(j % 2 == 0){
else{
delay(20);
//umbrela
line(70+j, 250, 70+j, 330);
// rain
x = rand() % 800;
y = rand() % 800;
outtextxy(x, y, "/");
delay(100);
cleardevice();
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
#include <time.h>
int main(){
initwindow(800, 800);
cout << "Enter the bounce direction: \n1. Left-Right \n2. Up-Down" << endl;
srand(time(NULL));
while (true){
cleardevice();
int k = rand();
if (op == 1){
else{
fillellipse(400, i, r, r);
if (turn == "f"){
if (i >= 800-r){
turn = "b";
setfillstyle(k%13, k%16);
}
i += 5;
if (i <= 0+r){
turn = "f";
setfillstyle(k%13, k%16);
i -= 5;
delay(1);
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int y = 200;
setfillstyle(1, BLACK);
cleardevice();
y += 1;
delay(1);
getch();
return 0;
Output:
9. Write a program to enter the passing percentage of students in the last 5 years, and show them by
making a graph using bar3d() and bar() functions.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int marks[5];
char* percentage[] = {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%",
"100%"};
int k = 10, l = 0;
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
// Wheel
// Road
// Stone
delay(10);
cleardevice();
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int top = 0;
cleardevice();
setlinestyle(0, 0, 1);
// man
setlinestyle(0, 0, 5);
top++;
delay(8);
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
#include <time.h>
int main(){
initwindow(1650, 800);
int x = 0, y = 0;
srand(time(NULL));
// stars
x = rand() % 1650;
y = rand() % 800;
putpixel(x, y, WHITE);
// sun
setcolor(RED);
setcolor(WHITE);
// mercury
setcolor(WHITE);
// venus
setfillstyle(1, LIGHTGRAY);
setcolor(BLACK);
setcolor(WHITE);
// earth
setcolor(BROWN);
setcolor(WHITE);
// mars
setcolor(BLACK);
setcolor(WHITE);
// jupiter
setcolor(BLACK);
setcolor(WHITE);
outtextxy(720, 520, "JUPITER");
// saturn
setcolor(BLACK);
setcolor(WHITE);
// rings
setlinestyle(1, 1, 10);
setcolor(WHITE);
// uranus
setlinestyle(0, 1, 1);
setcolor(WHITE);
setcolor(WHITE);
// rings
setlinestyle(0, 1, 2);
setcolor(WHITE);
// nepture
setlinestyle(0, 1, 1);
setcolor(BLUE);
// pluto :(
setcolor(BROWN);
setcolor(WHITE);
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int y = 600;
while(y != -110){
cleardevice();
// baloon 1
// baloon 2
// baloon 3
y--;
delay(1);
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int x = 0;
while (1){
cleardevice();
setfillstyle(1, WHITE);
// wheels
x += 10;
getch();
return 0;
Output:
17. Write a program of flying a kite moving up and down in y-direction as well as in x-direction.
Code:
#include <graphics.h>
#include <iostream>
#include <time.h>
int main(){
initwindow(800, 800);
srand(time(NULL));
while (1){
cleardevice();
// drawing kite
int kite[] = {350+x, 400+y, 400+x, 300+y, 450+x, 400+y, 400+x, 500+y, 350+x, 400+y, 450+x,
400+y, 400+x, 300+y, 400+x, 500+y};
drawpoly(8, kite);
k = rand() % 4;
if (time_s % 100 == 0)
switch (k){
case 0: dir = 0;
break;
case 1: dir = 1;
break;
case 2: dir = 2;
break;
case 3: dir = 3;
break;
switch (dir){
case 0: x--;
y--;
break;
case 1: x++;
y--;
break;
case 2: x++;
y++;
break;
case 3: x--;
y++;
break;
}
time_s++;
delay(1);
getch();
return 0;
Output:
18. Program of Trigonometric function of Sin wave, Cos wave, Tan wave.
Code:
#include <iostream>
#include <cmath>
#include <graphics.h>
int main(){
double x, y;
initwindow(800, 800);
if (wave == 1){
y = 50 * sin(angle * 3.141/180);
y = 50 * cos(angle * 3.141/180);
y = 50 * tan(angle * 3.141/180);
}
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(1);
// increment angle
angle += 5;
getch();
return 0;
Output:
Code:
#include<math.h>
#include<graphics.h>
#define N getmaxy()
#define Radius 40
int main()
//graphics variables
initwindow(800, 600);
int i,xc,yc;
int rx1,ry1,sep;
//initialise yc it is constant
yc=getmaxy()/2-Radius;
i=0;
while(i<N&&!kbhit())
xc=i;
delay(50);
clearviewport();
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
//draw wheel
circle(xc,yc,Radius);
if(i%2==0)
line(xc,yc+Radius,xc,yc-Radius);
line(xc-Radius,yc,xc+Radius,yc);
else
rx1=xc-(Radius/sqrt(2));
ry1=yc-(Radius/sqrt(2));
sep=sqrt(2)*Radius;
line(rx1,ry1,rx1+sep,ry1+sep);
line(rx1,ry1+sep,rx1+sep,ry1);
}
//increment i
i+=9;
getch();
return 0;
Output:
Code:
#include <graphics.h>
#include <iostream>
using namespace std;
int main(){
initwindow(1600, 800);
int x = 0, y = 0;
while (1){
cleardevice();
int wing1[] = {250+x, 650+y, 225+x, 550+y, 280+x, 550+y, 300+x, 650+y};
int wing2[] = {250+x, 700+y, 200+x, 750+y, 250+x, 750+y, 300+x, 700+y};
// ground
// plane body
drawpoly(4, wing1);
drawpoly(4, wing2);
drawpoly(3, backwing);
// takeoff
if (x >= 700){
y--;
x++;
delay(1);
getch();
return 0;
Output:
21. Write a program of Rotating Fan with different speeds: slow, medium and high.
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
int x = 0, ch, j = 1;
while (1){
cleardevice();
switch (j){
break;
break;
break;
}
// resetting the fan speed to slow
if (j > 11){
j = 1;
if (kbhit()){
ch = getch();
if (ch == 72){
// fan blades
// fan middle
delay(1);
x += j;
}
getch();
return 0;
Output:
22. Write a program of Rotating a small circle w.r.t a bigger circle in following manners.
c. Two small circles one inside and another outside in opposite directions.
Code:
#include <iostream>
#include <graphics.h>
#include <math.h>
void circleMotion(int radius, int midx, int midy, int x[360], int y[360]) {
int i, j = 0;
// positions of circle
j++;
int main() {
initwindow(1200, 1000);
cout << "1. Inner rotation \n2. Outer rotation \n3. Both\n";
midx = getmaxx() / 2;
midy = getmaxy() / 2;
while (1) {
setcolor(WHITE);
// main circle
switch (choice){
break;
case 3: // rotate the ball around both the inner and outer circle
delay(16);
cleardevice();
i = (i + 1) % 360;
getch();
return 0;
Output:
23. Write a program of Rotating a Moon around the Earth
Code:
#include <iostream>
#include <graphics.h>
#include <math.h>
void moonMotion(int radius, int midx, int midy, int x[60], int y[60]) {
int i, j = 0;
j++;
int main() {
initwindow(1200, 1000);
midy = getmaxy() / 2;
while(!kbhit()) {
setcolor(BLUE);
setcolor(WHITE);
setlinestyle(DASHED_LINE, 1, 1);
setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
setlinestyle(SOLID_LINE, 1, 1);
delay(200);
cleardevice();
// checks for one complete rotation
i = (i + 1) % 60;
getch();
return 0;
Output:
24. Write a program of launching a Rocket
Code:
#include <iostream>
#include <graphics.h>
#include <time.h>
int main() {
initwindow(800, 1100);
srand(time(NULL));
k = rand() % 800;
l = rand() % 1100;
star_arr_x[i] = k;
star_arr_y[i] = l;
while (1){
cleardevice();
int fire[] = {560, 900-y, 540, 930-y, 570, 915-y, 580, 945-y, 590, 915-y, 620, 930-y, 600, 900-y};
int top[] = {560, 630-y, 580, 600-y ,600, 630-y, 560, 630-y};
// stars
// earth
setfillstyle(1, BLUE);
// rocket
setfillstyle(1, COLOR(62,0,114));
// fire
setfillstyle(9, COLOR(255,98,28));
fillpoly(7, fire);
//top
fillpoly(4, top);
y += 10;
delay(1);
}
getch();
return 0;
Output:
25. Write a program of Moving man made of Sticks( line)
Code:
#include <iostream>
#include <graphics.h>
int main() {
initwindow(1200, 800);
int x = 0;
while (1){
cleardevice();
// ground
// head
// body
// legs
x++;
delay(1);
getch();
return 0;
Output:
26. Write a program of Star randomly changing its color
Code:
#include <iostream>
#include <graphics.h>
#include <time.h>
int main() {
initwindow(800, 800);
int star[] = {400, 300, 430, 360, 490, 330, 430, 405, 460, 480, 400, 435, 340, 480, 370, 405, 310, 330,
370, 360};
int x = 0;
srand(time(NULL));
while (1){
if (x % 17 == 0){
cleardevice();
fillpoly(10, star);
}
delay(10);
x += rand() % 100;
getch();
return 0;
Output:
28. Write a program of a Moving bike.
Code:
#include <iostream>
#include <graphics.h>
int main() {
initwindow(1000, 800);
int x = 0;
while (1){
cleardevice();
// land
// body
// seat
// handle
// tyres
x++;
delay(10);
getch();
return 0;
Output:
#include <graphics.h>
#include <iostream>
void Go(){
for(int l=0;l<50;l++){
//Road
line(0,392,900,392);
//Garage
rectangle(0,392,180,300);
line(180,300,220,320);
//mountain
line(0,199,200,100);
arc(220,157,49,118,60);
line(259,112,460,300);
line(353,200,600,60);
arc(625,115,49,118,60);
//Sun
arc(356,200,33,139,40);
line(327,169,300,140);
line(335,165,322,148);
line(345,162,327,129);
line(355,160,348,137);
line(365,161,367,124);
line(375,164,382,137);
line(381,168,410,134);
line(387,172,413,157);
// Helicopter
line(216+c,235-b,75+c,235-b);
line(115+c,247-b,120+c,240-b); //UpperBackSmallInclinedLine
line(166+c,247-b,161+c,240-b); //UpperFrontSmallInclinedLine
line(120+c,240-b,161+c,240-b); //UpperHorizontallLIne
line(139+c,240-b,139+c,235-b); //SmallBAckVerticalLIne
line(141+c,240-b,141+c,235-b); //SmallFrontVerticalLine
arc(141+c,272-b,140,180,40); //BackArc
arc(140+c,272-b,0,40,40); //FrontArc
line(165+c,248-b,165+c,266-b); //Glass
line(165+c,266-b,180+c,266-b); //Glass
rectangle(130+c,282-b,150+c,255-b); //Door
circle(147+c,268-b,1); //KeyHole
line(110+c,247-b,171+c,247-b); //MiddleLine
line(30+c,275-b,100+c,272-b); //backUpperLine
line(180+c,273-b,180+c,285-b); //frontArcVerticalLine
line(25+c,285-b,180+c,285-b); //BottomBigLine
line(30+c,275-b,20+c,260-b); //BackEndInclinedLine
line(20+c,260-b,14+c,260-b); //BackUpperLine
line(14+c,260-b,25+c,285-b); //LastEndInclinedLine
circle(18+c,262-b,1); //BackSmallCircle
//Legs
line(80+c,299-b,167+c,299-b); //LowerBigLine
line(80+c,295-b,167+c,295-b); //UpperBigLine
line(80+c,295-b,80+c,299-b); //BackVerticalLine
arc(167+c,279-b,270,310,20); //LowerCurve
arc(167+c,275-b,270,310,20); //UpperCurve
line(180+c,291-b,180+c,293-b); //FrontSmallestLine
line(110+c,285-b,110+c,295-b); //StraightLine
line(113+c,285-b,113+c,295-b); //StraightLine
line(165+c,285-b,160+c,295-b); //VerticalLine
line(163+c,285-b,158+c,295-b);
setcolor(WHITE);
delay(30);
cleardevice();
//For Helicopter
void Helicopter(){
for(int i=0;i<=350;i++) {
int c=i*2; //Speed of x-axis of helicoptor. It will move twice faster than y a-axis.
// Helicopter
line(115+c,247-b,120+c,240-b); //UpperBackSmallInclinedLine
line(166+c,247-b,161+c,240-b); //UpperFrontSmallInclinedLine
line(120+c,240-b,161+c,240-b); //UpperHorizontallLIne
line(139+c,240-b,139+c,235-b); //SmallBAckVerticalLIne
line(141+c,240-b,141+c,235-b); //SmallFrontVerticalLine
arc(141+c,272-b,140,180,40); //BackArc
arc(140+c,272-b,0,40,40); //FrontArc
line(165+c,248-b,165+c,266-b); //Glass
line(165+c,266-b,180+c,266-b); //Glass
rectangle(130+c,282-b,150+c,255-b); //Door
circle(147+c,268-b,1); //KeyHole
line(110+c,247-b,171+c,247-b); //MiddleLine
line(30+c,275-b,100+c,272-b); //backUpperLine
line(180+c,273-b,180+c,285-b); //frontArcVerticalLine
line(25+c,285-b,180+c,285-b); //BottomBigLine
line(30+c,275-b,20+c,260-b); //BackEndInclinedLine
line(20+c,260-b,14+c,260-b); //BackUpperLine
line(14+c,260-b,25+c,285-b); //LastEndInclinedLine
circle(18+c,262-b,1); //BackSmallCircle
//Legs
line(80+c,299-b,167+c,299-b); //LowerBigLine
line(80+c,295-b,167+c,295-b); //UpperBigLine
line(80+c,295-b,80+c,299-b); //BackVerticalLine
arc(167+c,279-b,270,310,20); //LowerCurve
arc(167+c,275-b,270,310,20); //UpperCurve
line(180+c,291-b,180+c,293-b); //FrontSmallestLine
line(110+c,285-b,110+c,295-b); //StraightLine
line(113+c,285-b,113+c,295-b); //StraightLine
line(165+c,285-b,160+c,295-b); //VerticalLine
line(163+c,285-b,158+c,295-b); //VerticalLine
//MovingWings
if(i%2==0){
line(141+c,235-b,75+c,243-b);
line(141+c,235-b,83+c,258-b);
line(141+c,235-b,164+c,278-b);
line(141+c,235-b,99+c,268-b);
line(141+c,235-b,196+c,243-b);
line(141+c,235-b,195+c,258-b);
else{
line(141+c,235-b,71+c,236-b);
line(141+c,235-b,79+c,250-b);
line(141+c,235-b,110+c,278-b);
line(141+c,235-b,197+c,236-b);
line(141+c,235-b,196+c,250-b);
line(141+c,235-b,185+c,267-b);
line(141+c,235-b,141+c,282-b);
//BackGround image
//Road
line(0,392,900,392);
//Garage
rectangle(0,392,180,300);
line(180,300,220,320);
//mountain
line(0,199,200,100);
arc(220,157,49,118,60);
line(259,112,460,300);
line(353,200,600,60);
arc(625,115,49,118,60);
//Sun
arc(356,200,33,139,40);
line(327,169,300,140);
line(335,165,322,148);
line(345,162,327,129);
line(355,160,348,137);
line(365,161,367,124);
line(375,164,382,137);
line(381,168,410,134);
line(387,172,413,157);
delay(30);
cleardevice();
int main()
{
initwindow(800, 500);
Go();
Helicopter();
// Close();
getch();
return 0;
Output:
30. Write a program of a man rowing boat on the sea in the rain.
Code:
#include <graphics.h>
#include <iostream>
#include <time.h>
int main(){
initwindow(1000, 800);
int x = 0, j, k;
srand(time(NULL));
while (1){
cleardevice();
// rain
j = rand() % 1000;
k = rand() % 650;
outtextxy(j, k, "/");
// water
setfillstyle(1, COLOR(4,101,177));
bar(0, 700, 1000, 800);
// boat
setfillstyle(1, WHITE);
int boat[] = {100+x, 650, 300+x, 650, 270+x, 710, 130+x, 710};
fillpoly(4, boat);
// man
if (x % 2 == 0){
// row 1
setlinestyle(0, 1, 6);
setcolor(RED);
setlinestyle(0, 1, 1);
setcolor(WHITE);
else{
// row 2
setlinestyle(0, 1, 6);
setcolor(RED);
setlinestyle(0, 1, 1);
setcolor(WHITE);
delay(1);
x += 5;
getch();
return 0;
Output:
31. Write a program of any cartoon (preferably doraemon).
#include <graphics.h>
// Driver code
int main()
initwindow(1000, 800);
setfillstyle(SOLID_FILL, CYAN);
setfillstyle(SOLID_FILL, CYAN);
setfillstyle(SOLID_FILL, RED);
// NOSE
circle(502, 214, 10);
// Left Eye
// Right Eye
// Line
// Line
// Line
// Line
// Line
line(570, 214, 620, 196);
// Line
// Nose-Mouth Connector
setfillstyle(SOLID_FILL, CYAN);
// Right Up Hand
setfillstyle(SOLID_FILL, CYAN);
// Left Up Hand
// Pocket Horizental
// Pocket Tangent
// Pocket Tangent
setfillstyle(SOLID_FILL, RED);
setfillstyle(SOLID_FILL, YELLOW);
// Bell
setcolor(BLACK);
setcolor(WHITE);
// Left Leg Left Line
setfillstyle(SOLID_FILL, CYAN);
setfillstyle(SOLID_FILL, CYAN);
getch();
return 0;
Output:
32. Write a program of Analog Clock & Digital Clock both, showing the Current system time.
Code:
#include <iostream>
#include <graphics.h>
#include <cmath>
#include <ctime>
return x % 360;
void clock(int radius, int midx, int midy, int x[60], int y[60], int x1[12], int y1[12]){
int i, j = 0;
j++;
j = 0;
radius -= radius/4;
j++;
int main() {
initwindow(800, 800);
while (1){
cleardevice();
setcolor(WHITE);
time_t t = time(0);
h = now->tm_hour % 12;
m = now->tm_min;
s = now->tm_sec;
cout << h << " : " << m << " : " << s << endl;
// seconds
setcolor(WHITE);
// minute
setcolor(GREEN);
setlinestyle(0, 1, 2);
setlinestyle(0, 1, 1);
setcolor(RED);
//hour
setlinestyle(0, 1, 4);
setlinestyle(0, 1, 1);
delay(1000);
getch();
return 0;
Output:
33. Write a program to Draw a Eiffel tower.
Code:
#include <iostream>
#include <graphics.h>
int main()
initwindow(1200, 1200);
// Declared Variables
// Left Side
// Lower Base
// Inside Decoration
// 1st Base
// Inside Decoration
// 2nd Base
line(360, 880, 510, 880);
// Inside Decoration
// 3rd Base
// Left Tangent
// Right Tangent
// Joining Line
// Half Circle
// Right Side
// Lower Base
// Inside Decoration
// 1st Base
// Inside Decoration
// 2nd Base
// Inside Decoration
// 3rd Base
// Left Tangent
// Right Tangent
// Triangle Decoration
a = a + 20;
b = b + 20;
// Left Side
// Upper Base
// Inside Decoration
// 1st Base
// 2nd base
// Left Tangent
// Right Tangent
// Right Side
// Upper Base
// Inside Decoration
// 1st Base
// Inside Decoration
// Left Tangent
// Right Tangent
// Joining Line
// Redeclaring Variable
b = 470;
b = b + 10;
// Redeclaring Variable
a = 600;
b = 500;
// Middle Line
if (b == c)
break;
else {
line(b, a, c, a);
a = a - 40;
b = b + 10;
c = c - 10;
// Tangent Lines
getch();
return 0;
}
Output:
34. Write a program to Draw a Robot using basic polygons & shapes (circle, rectangle, square, line, and
triangle).
Code:
#include <graphics.h>
#include <iostream>
int main(){
initwindow(800, 800);
// head
// eyes
// mouth
// body
// legs
// arms
setlinestyle(0, 1, 12);
getch();
return 0;
Output:
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// in this program
void taj_body();
void left_minars();
void right_minars();
// Driver Code
int main()
initwindow(1400, 800);
taj_body();
left_minars();
right_minars();
getch();
return 0;
}
void taj_body()
// Main Base
// Main Finial
// Left Side
// Left Tangent
// Divider
// Right Side
// Right Finial
// Right Tangent
// Divider
void left_minars()
// 1st Step
// 2nd Step
// 3rd Step
// 2nd Step
// 3rd Step
void right_minars()
// 1st Step
// 2nd Step
// 3rd Step
// 1st Step
// 2nd Step
// 3rd Step
}:
Output:
Code:
#include <graphics.h>
#include <iostream>
int main(){
int gdriver=DETECT,gmode;
int i,j,c1,c2;
initgraph(&gdriver,&gmode,"c:\\turboc3\\BGI");
for(i=0;i<120 ;i++)
cleardevice();
// delay(10);
for( j=0;j<35;j++)
if(190-i>130)
c1=4,c2=1;
else
c1=14,c2=11;
setcolor(c1);
circle(320,190-i,j-10);
setcolor(c2);
arc(100+i,200,0,180,j-25);
arc(115+i,200,0,180,j-20);
arc(140+i,200,0,180,j-15);
arc(165+i,200,0,180,j-25);
arc(405+i,150,0,180,j-25);
arc(430+i,150,0,180,j-15);
arc(455+i,150,0,180,j-25);
int b=0;
while(b+250!=300)
b++;
setcolor(2);
line(0,300,250,250+b);
line(250,250+b,310,300);
line(380,250+b,220,300);
line(380,250+b,660,300);
} delay(150);
for( j=0;j<35;j++)
if(190-i>130)
c1=4,c2=1;
else
c1=14,c2=11;
setcolor(c1);
circle(320,190-i,j-10);
setcolor(c2);
arc(100+i,200,0,180,j-25);
arc(115+i,200,0,180,j-20);
arc(140+i,200,0,180,j-15);
arc(165+i,200,0,180,j-25);
arc(405+i,150,0,180,j-25);
arc(430+i,150,0,180,j-15);
arc(455+i,150,0,180,j-25);
getch();
return 0;
Output:
ALGORITHM QUESTIONS
1. Plot a Straight line using the Digital Differential Analyzer (DDA) algorithm.
Code:
#include <iostream>
#include <graphics.h>
#include <math.h>
int main(){
initwindow(400, 400);
dx = x2- x1;
dy = y2 - y1;
steps=abs(dx);
}
else{
steps=abs(dy);
xincr = dx / steps;
yincr = dy / steps;
x = x1;
y = y1;
x += xincr;
y += yincr;
getch();
return 0;
}
float round(float a)
int b=a+0.5;
return b;
Output:
Code:
#include<iostream>
#include <graphics.h>
// calculate dx & dy
int dx = X2 - X1;
int dy = Y2 - Y1;
if(dy<=dx){
int d = dy - (dx/2);
putpixel(x, y, WHITE);
delay(10);
{
x++;
// E or East is chosen
if (d < 0)
d = d + dy;
else
d += (dy - dx);
y++;
putpixel(x, y, WHITE);
delay(10);
else if(dx<dy){
int d = dx - (dy/2);
putpixel(x, y, WHITE);
delay(10);
y++;
// E or East is chosen
if (d < 0)
d = d + dx;
else
d += (dx - dy);
x++;
putpixel(x, y, WHITE);
delay(10);
int main()
initwindow(800, 800);
getch();
return 0;
Output:
Code:
#include <iostream>
#include <graphics.h>
dx = x1 - x0;
dy = y1 - y0;
x = x0;
y = y0;
p = 2*dy - dx;
if (p >= 0){
putpixel(x, y, 7);
delay(10);
y++;
p = p + 2*dy - 2*dx;
else{
putpixel(x, y, 7);
delay(10);
p=p+2*dy;
x++;
}
int main()
initwindow(400, 400);
getch();
return 0;
Code:
#include <iostream>
#include <graphics.h>
int x = 0, y = r;
int d = 3 - 2 * r;
while (y >= x)
x++;
if (d > 0)
y--;
d = d + 4 * (x - y) + 10;
else{
d = d + 4 * x + 6;
int main(){
initwindow(300, 300);
getch();
return 0;
Output:
#include <math.h>
#include <iostream>
int main(){
initwindow(500, 500);
cin >> R;
getch();
return 0;
}
void draw_circle(int xc, int yc, int rad){
int x = 0;
int y = rad;
int p = 1 - rad;
for (x=0;y>x;x++){
if(p < 0)
p += 2*x + 3;
else{
p += 2 * (x-y) + 5;
y--;
delay(1);
delay(1);
putpixel(xc+y,yc-x, GREEN); //For pixel (y,x)
delay(1);
delay(1);
delay(1);
delay(1);
delay(1);
delay(1);
delay(1);
}:
Output:
6. Plot an Ellipse using Midpoint Algorithm.
Code:
#include <iostream>
#include <graphics.h>
#include <math.h>
void disp();
float x, y;
int main(){
initwindow(400, 400);
x = 0;
y = ry;
disp();
x++;
if(p1<=0)
p1=p1+(2.0*ry*ry*x)+(ry*ry);
else{
y--;
disp();
x =- x;
disp();
x =- x;
x = rx;
y = 0;
disp();
y++;
if(p2>0)
p2=p2+(rx*rx)-(2.0*rx*rx*y);
else
x--;
p2=p2+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(rx*rx);
disp();
y =- y;
disp();
y =- y;
getch();
return 0;
void disp(){
delay(5);
Output:
7. Draw an object i.e Triangle or Rectangle and apply the following transformations on it.
Translation w.r.t to origin in X and Y direction
Code:
#include <graphics.h>
#include <iostream>
#include <cmath>
int i, j, k;
temp[i][j] = mat2[i][j];
mat2[i][j] = 0;
int i, j, k;
temp[i][j] = mat2[i][j];
mat2[i][j] = 0;
setcolor(RED);
setcolor(GREEN);
setcolor(BLUE);
setcolor(WHITE);
obj[0][k] += step_x;
obj[1][k] += step_y;
delay(10);
cleardevice();
draw_object(obj);
double step, tx, ty, SIN, COS, cx, cy, sx, sy, I1, K1, I2, K2;
tx = 0;
ty = 0;
rot = 'n';
deg = 0.0;
deg += step;
deg = degrees_to_radian(deg);
SIN = sin(deg);
COS = cos(deg);
cx = COS * tx;
cy = COS * ty;
sx = SIN * tx;
sy = SIN * ty;
I1 = -cx + sy + tx;
K1 = -sx - cy + ty;
I2 = -cx - sy + tx;
K2 = sx - cy + ty;
double posRot[3][3] = {{COS, -SIN, I1}, {SIN, COS, K1}, {0, 0, 1}};
double negRot[3][3] = {{COS, SIN, I2}, {-SIN, COS, K2}, {0, 0, 1}};
if (rot == 'p'){
matMul34(posRot, obj);
else{
matMul34(negRot, obj);
delay(10);
cleardevice();
draw_object(obj);
}
double temp[3][4];
temp[i][j] = obj[i][j];
step_x += temp_x;
step_y += temp_y;
delay(10);
cleardevice();
draw_object(obj);
shx = 0;
shy = 0;
double temp[3][4];
temp[i][j] = obj[i][j];
shx += tempX;
shy += tempY;
delay(10);
cleardevice();
draw_object(obj);
int main(){
initwindow(800, 800);
double x_arr[4], y_arr[4], one_arr[4] = {1, 1, 1, 1}, obj[3][4] = {{200, 400, 400, 200}, {200, 200, 400,
400}, {1, 1, 1, 1}};
// matricies
// drawing object
draw_object(obj);
while (1){
cout << "\n1. Translation \n2. Rotation \n3. Scaling \n4. Shearing \n5. EXIT\n";
switch (option){
break;
rotate(obj, deg);
break;
break;
cout << "1. x-axis \n2. y-axis \n3. Both x and y axes\n";
switch (sh_opt){
break;
shear(obj, shy);
break;
break;
default: shear(obj);
break;
case 5: exit(0);
getch();
return 0;
Output:
8. Draw an object i.e Triangle and perform the following Reflection on it.
Reflection along X-axis
#include <iostream>
#include <cmath>
setcolor(WHITE);
line(o, 0, o, 800);
int i, j, k;
mat2[i][j] = 0;
setcolor(RED);
setcolor(GREEN);
setcolor(BLUE);
}
void translate(double obj[3][3], double tx, double ty){
obj[0][k] += step_x;
obj[1][k] += step_y;
// delay(10);
// cleardevice();
// draw_object(obj);
double step, tx, ty, SIN, COS, cx, cy, sx, sy, I1, K1, I2, K2;
tx = 400;
ty = 400;
rot = 'n';
}
deg = 0.0;
deg += step;
deg = degrees_to_radian(deg);
SIN = sin(deg);
COS = cos(deg);
cx = COS * tx;
cy = COS * ty;
sx = SIN * tx;
sy = SIN * ty;
I1 = -cx + sy + tx;
K1 = -sx - cy + ty;
I2 = -cx - sy + tx;
K2 = sx - cy + ty;
double posRot[3][3] = {{COS, -SIN, I1}, {SIN, COS, K1}, {0, 0, 1}};
double negRot[3][3] = {{COS, SIN, I2}, {-SIN, COS, K2}, {0, 0, 1}};
if (rot == 'p'){
matMul33(posRot, obj);
else{
matMul33(negRot, obj);
cleardevice();
draw_grid(ORIGIN);
draw_object(obj);
if (axis == "x"){
obj[1][i] = -obj[1][i];
obj[1][i] += 2*ORIGIN;
cleardevice();
draw_grid(ORIGIN);
draw_object(obj);
}
else if (axis == "y"){
obj[0][i] = -obj[0][i];
obj[0][i] += 2*ORIGIN;
cleardevice();
draw_grid(ORIGIN);
draw_object(obj);
rotate(obj, 45);
reflect(obj, "x");
rotate(obj, -45);
rotate(obj, 45);
reflect(obj, "y");
rotate(obj, -45);
int main(){
initwindow(800, 800);
draw_grid(ORIGIN);
int option = 1;
draw_object(obj);
while (1){
cout << "\n1. x-axis \n2. y-axis \n3. y=x line \n4. x=y line \n5. EXIT\n";
switch (option){
break;
break;
case 3: reflectyx(obj);
break;
case 4: reflectxy(obj);
break;
case 5: exit(0);
getch();
return 0;
Output:
9. Implement cohen-sutherland algorithm for Line clipping
Code:
#include<iostream>
#include<cmath>
#include<graphics.h>
int x,y;
char code[4];
}PT;
void drawwindow();
PT setcode(PT p);
int main()
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
cin>>p1.x>>p1.y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
void drawwindow()
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
line(p1.x,p1.y,p2.x,p2.y);
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 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 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];
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: