0% found this document useful (0 votes)
11 views

CG Practical File

Cg file
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)
11 views

CG Practical File

Cg file
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/ 150

Computer

Graphics
Practical File

Name: Ritik Tandon


Class: BCA 5B
Enrollment no. – 07514902019
1. Write your name in hindi.

Code:

#include <iostream>

#include <graphics.h>

using namespace std;

int main (void) {

initwindow(400, 300);

setbkcolor(9);

line(80,100,300,100);

// ri

line(100, 100, 100, 170);

arc(120, 100, 0, 180, 20);

arc(135, 115, 270, 70, 15);

line(135, 130, 155, 170);

// ti

line(175, 100, 175, 170);

arc(195, 100, 0, 180, 20);

line(215, 100, 215, 170);

line(215, 145, 195, 145);


line(195, 145, 198, 170);

// k

line(260, 100, 260, 170);

setbkcolor(BLACK);

setfillstyle(0, 0);

sector(260, 135, 90, 270, 25, 18);

line(260, 135, 290, 135);

line(290, 135, 280, 165);

getch();

return 0;

Output:
2. Draw five concentric circles filled with different colors.

Code:

#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

for (int i = 5; i >= 1; i--){

setfillstyle(1, i);

fillellipse(450, 320, i*30, i*30);

getch();

return 0;

Output:
3. Draw circles that move in a pattern of increasing and decreasing radius.

Code:

#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(808, 800);

int r = 50, i = 50;

string turn = "f";

while (true){

if (i <= 200){

r++;

else if (i <= 400){

r--;

else if (i <= 600){

r++;

else if (i <= 800){

r--;

cleardevice();

fillellipse(i, 320, r, r);


if (turn == "f"){

if (i >= 800-r){

turn = "b";

i += 5;

else if (turn == "b"){

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>

using namespace std;

int main(){

initwindow(800, 800);

string turn = "f";

for (int i = 0; i <= 800; i += 0){

if (turn == "f"){

if (i >= 590){

turn = "b";

i += 10;

else if (turn == "b"){

if (i <= 210){

turn = "f";

i -= 10;

// Set color of car as green


setcolor(GREEN);

// Thease lines for bonnet and

// body of car

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

// For left wheel of car

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);

// For right wheel of car

circle(145 + i, 330, 15);

circle(145 + i, 330, 2);

// Line left of left wheel

line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel

line(80 + i, 330, 130 + i, 330);

// Line right of right wheel

line(210 + i, 330, 160 + i, 330);


delay(100);

// To erase previous drawn car, draw

// the whole car at same position

// but color using black

setcolor(BLACK);

// Lines for bonnet and body of car

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330);

line(210 + i, 300, 210 + i, 330);

// For left wheel of car

circle(65 + i, 330, 15);

circle(65 + i, 330, 2);

// For right wheel of car

circle(145 + i, 330, 15);

circle(145 + i, 330, 2);

// Line left of left wheel

line(0 + i, 330, 50 + i, 330);


// Line middle of both wheel

line(80 + i, 330, 130 + i, 330);

// Line right of right wheel

line(210 + i, 330, 160 + i, 330);

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>

using namespace std;

int main(){

initwindow(800, 800);

int x, y, r = 0;

for (int i = 0; i <= 20; i++){

while (true){

x = rand();

y = rand();

r = rand();

if (x <= 750 && y <= 750 && r <= 100)

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:

6. Write a program of moving men with umbrellas in the rain.

Code:
#include <graphics.h>

#include <iostream>

#include <time.h>

using namespace std;

int main(){

initwindow(800, 800);

int i, x, y, j;

srand(time(NULL));

// man

for(j = 1; j < 600; j = j+5){

line(0, 400, 800, 400); // land

circle(30+j, 280, 20); //head

line(30+j, 300, 30+j, 350); //body

line(30+j, 330, 70+j, 330); //hand

if(j % 2 == 0){

line(30+j, 350, 25+j, 400); //left leg

line(30+j, 350, 45+j, 400); // right

else{

line(30+j, 350, 35+j, 400); //transition

line(30+j, 350, 35+j, 400);

delay(20);

//umbrela
line(70+j, 250, 70+j, 330);

pieslice(70+j, 250, 0, 180, 80);

// rain

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

x = rand() % 800;

y = rand() % 800;

outtextxy(x, y, "/");

delay(100);

cleardevice();

getch();

return 0;

Output:

7. Write a program of bouncing ball

a. which bounce at upper and lower side of the screen


b. Which bounce from center of side of the screen

Code:

#include <graphics.h>

#include <iostream>

#include <time.h>

using namespace std;

int main(){

initwindow(800, 800);

int r = 100, i = 50, op = 0;

string turn = "f";

cout << "Enter the bounce direction: \n1. Left-Right \n2. Up-Down" << endl;

cin >> op;

srand(time(NULL));

while (true){

cleardevice();

int k = rand();

if (op == 1){

fillellipse(i, 400, r, r);

else{

fillellipse(400, i, r, r);

if (turn == "f"){

if (i >= 800-r){

turn = "b";

setfillstyle(k%13, k%16);
}

i += 5;

else if (turn == "b"){

if (i <= 0+r){

turn = "f";

setfillstyle(k%13, k%16);

i -= 5;

delay(1);

getch();

return 0;

Output:

8. Write a program of a flying parachute.

Code:
#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

int y = 200;

setfillstyle(1, BLACK);

for (int j = 0; j < 290; j++){

cleardevice();

line(0, 790, 800, 790); // ground

sector(400, y, 0, 180, 200, 150);

for (int i = 0; i < 5; i++){

arc(240 + (i*80), y, 0, 180, 40);

line(200 + (i*80), y, 400, y + 200);

line(600, y, 400, y + 200); // last parachute line

rectangle(350, y + 200, 450, y + 300);

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>

using namespace std;

int main(){

initwindow(800, 800);

int marks[5];

// = {100, 80, 20, 80, 95};

cout << "Enter the average passing percentage of students:\n";

for (int i = 0; i < 5; i++){

cout << "Year : " << 2016 + i << endl;

cin >> marks[i];

line(200, 180, 200, 600); //y-axis

line(200, 600, 620, 600); //x-axis

char* percentage[] = {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%",
"100%"};

int k = 10, l = 0;

for (int i = 200; i <= 600; i += 40){

line(190, i, 210, i);

outtextxy(160, i-10, percentage[k]);


k--;

for (int l = 0; l < 5; l ++){

bar3d(240 + l*60, 600 - marks[l]*4 , 280 + l*60, 600, 20, 1);

outtextxy(350, 620, "year");

getch();

return 0;

Output:

10. Write a program of moving cycles from left to right.

Code:
#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

// Move the cycle

for (int i = 0; i < 600; i++) {

// Upper body of cycle

line(50 + i, 405, 100 + i, 405);

line(75 + i, 375, 125 + i, 375);

line(50 + i, 405, 75 + i, 375);

line(100 + i, 405, 100 + i, 345);

line(150 + i, 405, 100 + i, 345);

line(80 + i, 345, 100 + i, 345);

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


// Stop the screen for 10 secs

delay(10);

// Clear the screen

cleardevice();

getch();

return 0;

Output:

11. Write a program of a person hosting a flag.

Code:
#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

int top = 0;

while (top <= 400){

cleardevice();

setlinestyle(0, 0, 1);

line(0, 700, 800, 700); // ground

rectangle(390, 200, 410, 700); // pole

circle(400, 180, 20); // pole top

rectangle(410, 600 - top, 500, 660 - top); // flag on ground

// man

circle(150, 550, 30); // head

line(150, 580, 154, 650); // body

line(154, 650, 170, 700); // right leg

line(154, 650, 140, 700); // left leg

line(152, 610, 195, 584); // top hand

line(152, 610, 190, 600); // bottom hand


// rope

setlinestyle(0, 0, 5);

line(400, 200, 190, 600);

top++;

delay(8);

getch();

return 0;

Output:

13. Write a program to draw SolarSystem.

Code:
#include <graphics.h>

#include <iostream>

#include <time.h>

using namespace std;

int main(){

initwindow(1650, 800);

int x = 0, y = 0;

srand(time(NULL));

// stars

for (int i = 0; i <= 500; i++){

x = rand() % 1650;

y = rand() % 800;

putpixel(x, y, WHITE);

// sun

setfillstyle(1, COLOR(232, 91, 9));

setcolor(RED);

fillellipse(-100, 400, 300, 300);

setcolor(WHITE);

outtextxy(100, 700, "SUN");

// mercury

setfillstyle(1, COLOR(219, 206, 202));


setcolor(LIGHTGRAY);

fillellipse(250, 400, 5, 5);

setcolor(WHITE);

outtextxy(220, 420, "MERCURY");

// venus

setfillstyle(1, LIGHTGRAY);

setcolor(BLACK);

fillellipse(340, 400, 30, 30);

setcolor(WHITE);

outtextxy(320, 450, "VENUS");

// earth

setfillstyle(1, COLOR(54, 68, 228));

setcolor(BROWN);

fillellipse(430, 400, 27, 27);

setcolor(WHITE);

outtextxy(410, 450, "EARTH");

// mars

setfillstyle(1, COLOR(193, 68, 14));

setcolor(BLACK);

fillellipse(530, 400, 14, 14);

setcolor(WHITE);

outtextxy(510, 430, "MARS");

// jupiter

setfillstyle(1, COLOR(165, 145, 134));

setcolor(BLACK);

fillellipse(750, 400, 100, 100);

setcolor(WHITE);
outtextxy(720, 520, "JUPITER");

// saturn

setfillstyle(1, COLOR(206, 184, 184));

setcolor(BLACK);

fillellipse(1050, 400, 95, 95);

setcolor(WHITE);

outtextxy(1025, 520, "SATURN");

// rings

setlinestyle(1, 1, 10);

setcolor(WHITE);

ellipse(1050, 400, 0, 360, 160, 40);

// uranus

setlinestyle(0, 1, 1);

setfillstyle(1, COLOR(93, 178, 242));

setcolor(WHITE);

fillellipse(1300, 400, 35, 35);

setcolor(WHITE);

outtextxy(1270, 520, "URANUS");

// rings

setlinestyle(0, 1, 2);

setcolor(WHITE);

ellipse(1300, 400, 0, 360, 20, 100);

// nepture

setlinestyle(0, 1, 1);

setfillstyle(1, COLOR(56, 75, 210));

setcolor(BLUE);

fillellipse(1440, 400, 36, 36);


setcolor(WHITE);

outtextxy(1410, 450, "NEPTUNE");

// pluto :(

setfillstyle(1, COLOR(211, 177, 145));

setcolor(BROWN);

fillellipse(1600, 400, 6, 6);

setcolor(WHITE);

outtextxy(1580, 420, "PLUTO :(");

getch();

return 0;

Output:

14. Write a program to draw flying balloons.

Code:
#include <graphics.h>

#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

int y = 600;

while(y != -110){

cleardevice();

// baloon 1

ellipse(100, y, 0, 360, 20, 25);

fillellipse(100, y+30, 5, 5);

line(100, y+25, 100, y+100);

// baloon 2

ellipse(200, y+10, 0, 360, 20, 25);

fillellipse(200, y+40, 5, 5);

line(200, y+35, 200, y+110);

// baloon 3

ellipse(300, y, 0, 360, 20, 25);

fillellipse(300, y+30, 5, 5);

line(300, y+25, 310, y+120);


// baloon 4

ellipse(600, y, 0, 360, 40, 50);

fillellipse(600, y+60, 10, 10);

line(600, y+60, 590, y+200);

y--;

delay(1);

getch();

return 0;

Output:

16. Write a program to draw a moving train in X-direction

Code:

#include <graphics.h>
#include <iostream>

using namespace std;

int main(){

initwindow(800, 800);

int x = 0;

while (1){

cleardevice();

rectangle(-400+x, 400, 500+x, 550); // body

rectangle(450+x, 400, 500+x, 480); // front window

line(0, 580, 800, 580); // tracks

line(-400+x, 520, 500+x, 520);

line(-400+x, 510, 500+x, 510);

setfillstyle(1, WHITE);

// wheels

fillellipse(-300+x, 550, 30, 30);

fillellipse(-150+x, 550, 30, 30);

fillellipse(0+x, 550, 30, 30);

fillellipse(150+x, 550, 30, 30);

fillellipse(300+x, 550, 30, 30);

fillellipse(450+x, 550, 30, 30);


delay(10);

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>

using namespace std;

int main(){

initwindow(800, 800);

int x = 0, y = 0, time_s = 0, k = 0, dir = 1;

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

line(400+x, 500+y, 450+x, 700+y);

k = rand() % 4;

// timer to switch direction of kite

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>

using namespace std;

int main(){

int angle = 0, wave = 1;

double x, y;

initwindow(800, 800);

cout << "What wave you you wamt to display?\n";

cout << "1. Sine \n2. Cosine \n3. Tangent?\n";

cin >> wave;

line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

// generate a sine wave

for(x = 0; x < getmaxx(); x += 3){

// calculate y value given

if (wave == 1){

y = 50 * sin(angle * 3.141/180);

else if (wave == 2){

y = 50 * cos(angle * 3.141/180);

else if (wave == 3){

y = 50 * tan(angle * 3.141/180);
}

y = getmaxy()/2 - y;

// color a pixel at the given position

putpixel(x, y, 15);

delay(1);

// increment angle

angle += 5;

getch();

return 0;

Output:

19. Rotating wheel with translation from left to right.

Code:

#include<math.h>

#include<graphics.h>
#define N getmaxy()

#define Radius 40

int main()

//graphics variables

initwindow(800, 600);

//xc,yc coordinate of center

//i for loop iteration

int i,xc,yc;

//rx1,ry1,sep used for wheel's cross

int rx1,ry1,sep;

//initialise yc it is constant

yc=getmaxy()/2-Radius;

i=0;

//loop stops when keyboard key down or i>=N

//kbhit checks whether keyboard pressed or not

while(i<N&&!kbhit())

//increment xc for moving the wheel

xc=i;
delay(50);

//clear previous screen

clearviewport();

//represent road or some ground as stright line

line(0,getmaxy()/2,getmaxx(),getmaxy()/2);

//draw wheel

circle(xc,yc,Radius);

//following if else block draws

//wheel cross in two arrangments shows alternatively

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:

20. 20. Program of translation of an Aeroplane while take off.

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

int backwing[] = {100+x, 650+y, 100+x, 610+y, 150+x, 650+y};

// ground

line(0, 750, 1600, 750);

// plane body

rectangle(100+x, 650+y, 400+x, 700+y);

pieslice(400+x, 675+y, 270, 90, 25); // head

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>

using namespace std;

int main(){

initwindow(800, 800);

int x = 0, ch, j = 1;

while (1){

cleardevice();

// writing out current fan speed

switch (j){

case 1: outtextxy(365, 700, "SLOW");

break;

case 6: outtextxy(365, 700, "MEDIUM");

break;

case 11: outtextxy(365, 700, "FAST");

break;

}
// resetting the fan speed to slow

if (j > 11){

j = 1;

// detecting if UP ARROW key is pressed

if (kbhit()){

ch = getch();

if (ch == 72){

j += 5; // increasing the fan speed

// fan blades

pieslice(400, 400, (115+x) % 360, (125+x) % 360, 200);

pieslice(400, 400, (235+x) % 360, (245+x) % 360, 200);

pieslice(400, 400, (355+x) % 360, (5+x) % 360, 200);

// fan middle

fillellipse(400, 400, 30, 30);

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.

a. Small circle rotating along the inner side of a bigger circle.

b. Small circle rotating along the outer side of a bigger circle.

c. Two small circles one inside and another outside in opposite directions.

Code:

#include <iostream>

#include <graphics.h>

#include <math.h>

using namespace std;

// finds the points on a circle

void circleMotion(int radius, int midx, int midy, int x[360], int y[360]) {

int i, j = 0;

// positions of circle

for (i = 360; i > 0; i -= 1) {

x[j] = midx - (radius * cos((i * 3.14) / 180));

y[j] = midy - (radius * sin((i * 3.14) / 180));

j++;

int main() {

initwindow(1200, 1000);

int midx, midy, m_circle, s_circle;


int i = 0, in_x[360], in_y[360], out_x[360], out_y[360], choice = 0;

cout << "Select the circle rotation you want: \n";

cout << "1. Inner rotation \n2. Outer rotation \n3. Both\n";

cin >> choice;

m_circle = 300, s_circle = 30;

// mid position in x and y-axis

midx = getmaxx() / 2;

midy = getmaxy() / 2;

// get the inner and outer circle orbits

circleMotion(m_circle+s_circle, midx, midy, in_x, in_y);

circleMotion(m_circle-s_circle, midx, midy, out_x, out_y);

while (1) {

setcolor(WHITE);

// main circle

circle(midx, midy, m_circle);

switch (choice){

case 1: // rotate the ball around the outer circle

pieslice(out_x[i], out_y[i], 0, 360, s_circle);

break;

case 2: // rotate the ball around the inner circle

pieslice(in_x[i], in_y[i], 0, 360, s_circle);


break;

case 3: // rotate the ball around both the inner and outer circle

pieslice(in_x[i], in_y[i], 0, 360, s_circle);

pieslice(out_x[i], out_y[i], 0, 360, s_circle);

delay(16);

cleardevice();

// checks for one complete rotation

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>

// finds the location of moon on the virtual orbit around earth

void moonMotion(int radius, int midx, int midy, int x[60], int y[60]) {

int i, j = 0;

// positions of moon around the earth

for (i = 360; i > 0; i -= 6) {

x[j] = midx - (radius * cos((i * 3.14) / 180));

y[j] = midy - (radius * sin((i * 3.14) / 180));

j++;

int main() {

initwindow(1200, 1000);

int midx, midy, earth, orbit, moon;

int i = 0, x[60], y[60];

earth = 100, orbit = 400, moon = 30;

// mid position in x and y-axis


midx = getmaxx() / 2;

midy = getmaxy() / 2;

// get the positions of moon around earth for rotation

moonMotion(orbit, midx, midy, x, y);

while(!kbhit()) {

// drawing earth at the center

setcolor(BLUE);

setfillstyle(SOLID_FILL, COLOR(54, 68, 228));

pieslice(midx, midy, 0, 360, earth);

// drawing virtual orbit around the earth

setcolor(WHITE);

setlinestyle(DASHED_LINE, 1, 1);

circle(midx, midy, orbit);

// rotate moon around the earth

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

setlinestyle(SOLID_LINE, 1, 1);

pieslice(x[i], y[i], 0, 360, moon);

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>

using namespace std;

int main() {

initwindow(800, 1100);

int y = 0, k, l, star_arr_x[300], star_arr_y[300];

srand(time(NULL));

for (int i = 0; i <= 300; i++){

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

for (int i = 0; i <= 300; i++){

putpixel(star_arr_x[i], star_arr_y[i], WHITE);

// earth

setfillstyle(1, BLUE);

sector(400, 1100, 0, 180, 500, 160);

// rocket

setfillstyle(1, COLOR(62,0,114));

bar(560, 630 - y, 600, 900 - y);

// 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>

using namespace std;

int main() {

initwindow(1200, 800);

int x = 0;

while (1){

cleardevice();

// ground

bar(0, 780, 1200, 800);

// head

circle(100+x, 650, 20);

// body

line(100+x, 670, 100+x, 750);

// legs

line(100+x, 750, 80+x, 780);

line(100+x, 750, 120+x, 780);


// arms

line(100+x, 700, 130+x, 730);

line(100+x, 700, 70+x, 730);

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>

using namespace std;

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

// randomizing the color

setfillstyle(1, COLOR(rand() % 256, rand() % 256, rand() % 256));

if (x % 17 == 0){

cleardevice();

fillpoly(10, star);

}
delay(10);

// random time delay

x += rand() % 100;

getch();

return 0;

Output:
28. Write a program of a Moving bike.

Code:

#include <iostream>

#include <graphics.h>

using namespace std;

int main() {

initwindow(1000, 800);

int x = 0;

while (1){

cleardevice();

// land

line(0, 700, 1000, 700);

// body

ellipse(140+x, 655, 0, 360, 40, 12);

// seat

line(120+x, 645, 115+x, 635);

fillellipse(115+x, 635, 5, 2);

// handle

line(160+x, 645, 170+x, 625);


line(170+x, 625, 160+x, 615);

line(170+x, 625, 160+x, 620);

// tyres

circle(100+x, 680, 20);

circle(180+x, 680 , 20);

x++;

delay(10);

getch();

return 0;

Output:

29. Write a program of Flying Helicopter


Code:

#include <graphics.h>

#include <iostream>

//This Go is only to make it stand for some time

void Go(){

//........... For Background .........

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

int b=0; //Speed of y-axis of helicoptor

int c=0; //Speed of x-axis of helicoptor

// 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 b=i; //Speed of y-axis of helicoptor

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>

using namespace std;

int main(){

initwindow(1000, 800);

int x = 0, j, k;

srand(time(NULL));

while (1){

cleardevice();

// rain

for (int i = 0; i <= 300; i++){

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

fillellipse(160+x, 600, 15, 20); // head

line(160+x, 600, 160+x, 650); // body

line(160+x, 630, 180+x, 625); // arms

line(160+x, 630, 180+x, 635); // arms

if (x % 2 == 0){

// row 1

setlinestyle(0, 1, 6);

setcolor(RED);

line(180+x, 625, 150+x, 710);

fillellipse(150+x, 710, 10, 20);

setlinestyle(0, 1, 1);

setcolor(WHITE);

else{

// row 2

setlinestyle(0, 1, 6);
setcolor(RED);

line(180+x, 625, 210+x, 710);

fillellipse(210+x, 710, 10, 20);

setlinestyle(0, 1, 1);

setcolor(WHITE);

delay(1);

x += 5;

getch();

return 0;

Output:
31. Write a program of any cartoon (preferably doraemon).

Code: #include <iostream>

#include <graphics.h>

// Driver code

int main()

initwindow(1000, 800);

setfillstyle(SOLID_FILL, CYAN);

// Head Outer Circle

circle(500, 200, 100);

// Head Inner Circle

circle(500, 212, 88);

floodfill(502, 102, 15);

setfillstyle(SOLID_FILL, CYAN);

// Body Outer Circle

circle(500, 400, 100);

// Body Inner Circle

circle(500, 388, 88);

floodfill(502, 498, 15);

setfillstyle(SOLID_FILL, RED);

// NOSE
circle(502, 214, 10);

floodfill(504, 216, 15);

// Left Eye

circle(460, 170, 15);

// Right Eye

circle(540, 170, 15);

// Left Mustache Middle

// Line

line(430, 214, 380, 214);

// Left Mustache Upper

// Line

line(430, 214, 380, 196);

// Left Mustache Lower

// Line

line(430, 214, 380, 234);

// Right Mustache Middle

// Line

line(570, 214, 620, 214);

// Right Mustache Upper

// Line
line(570, 214, 620, 196);

// Right Mustache Lower

// Line

line(570, 214, 620, 234);

// Nose-Mouth Connector

line(502, 224, 502, 240);

// Mouth Horizental Line

line(465, 240, 535, 240);

// Mouth Tangent Line

line(465, 240, 502, 260);

// Mouth Tangent Line

line(502, 260, 535, 240);

setfillstyle(SOLID_FILL, CYAN);

// Right Up Hand

line(500, 300, 650, 320);

// Right Low Hand

line(500, 300, 650, 340);

// Right Hand Joining


line(650, 320, 650, 340);

floodfill(645, 332, 15);

setfillstyle(SOLID_FILL, CYAN);

// Left Up Hand

line(500, 300, 350, 320);

// Left Down Hand

line(500, 300, 350, 340);

// Left Hand Join

line(350, 320, 350, 340);

floodfill(355, 332, 15);

circle(665, 331, 15);

circle(335, 331, 15);

// Pocket Horizental

line(445, 380, 555, 380);

// Pocket Tangent

line(445, 380, 500, 420);

// Pocket Tangent

line(500, 420, 555, 380);

setfillstyle(SOLID_FILL, RED);

// Bell Pad Upper Horizental


line(430, 298, 570, 298);

// Bell Pad Lower Horizental

line(430, 308, 570, 308);

// Bell Pad Left Join

line(430, 298, 430, 308);

// Bell Pad Right Join

line(570, 298, 570, 308);

floodfill(432, 302, 15);

floodfill(568, 302, 15);

floodfill(500, 307, 15);

setfillstyle(SOLID_FILL, YELLOW);

// Bell

circle(500, 323, 15);

floodfill(502, 325, 15);

setcolor(BLACK);

// Inner Bell Upper Line

line(485, 323, 515, 323);

// Inner Bell Upper Line

line(485, 328, 515, 328);

setcolor(WHITE);
// Left Leg Left Line

line(450, 480, 450, 550);

// Left Leg Right Line

line(470, 490, 470, 550);

// Right Leg Right Line

line(550, 480, 550, 550);

// Right Leg Left Line

line(530, 490, 530, 550);

// Left Leg Pad

rectangle(440, 550, 480, 570);

// Right Leg Pad

rectangle(520, 550, 560, 570);

// Left Leg Color

setfillstyle(SOLID_FILL, CYAN);

floodfill(460, 500, 15);

// Right Leg Color

setfillstyle(SOLID_FILL, CYAN);

floodfill(540, 500, 15);

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>

using namespace std;

int conv(int x){

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;

for (i = 0; i < 360; i += 6) {

x[j] = midx - (radius * cos((conv(i + 90) * 3.14) / 180));

y[j] = midy - (radius * sin((conv(i + 90)* 3.14) / 180));

j++;

j = 0;

radius -= radius/4;

for (i = 0; i < 360; i += 30) {


x1[j] = midx - (radius * cos((conv(i + 90) * 3.14) / 180));

y1[j] = midy - (radius * sin((conv(i + 90) * 3.14) / 180));

j++;

int main() {

initwindow(800, 800);

int h, m, s, midx = getmaxx() / 2, midy = getmaxy() / 2, r = 200;

int second_x[60], second_y[60], hour_x[12], hour_y[12];

while (1){

cleardevice();

setcolor(WHITE);

time_t t = time(0);

tm* now = localtime(&t);

h = now->tm_hour % 12;

m = now->tm_min;

s = now->tm_sec;

cout << h << " : " << m << " : " << s << endl;

clock(r, midx, midy, second_x, second_y, hour_x, hour_y);


// clock image

circle(midx, midy, r+20);

circle(midx, midy, r+40);

// seconds

setcolor(WHITE);

line(midx, midy, second_x[s], second_y[s]);

// minute

setcolor(GREEN);

setlinestyle(0, 1, 2);

line(midx, midy, second_x[m], second_y[m]);

setlinestyle(0, 1, 1);

setcolor(RED);

//hour

setlinestyle(0, 1, 4);

line(midx, midy, hour_x[h], hour_y[h]);

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

int a = 390, b = 390, c = 700;

// Left Side

// Lower Base

line(300, 1000, 450, 1000);

// Inside Decoration

line(300, 1000, 480, 940);

line(450, 1000, 330, 940);

// 1st Base

line(330, 940, 480, 940);

// Inside Decoration

line(330, 940, 510, 880);

line(480, 940, 360, 880);

// 2nd Base
line(360, 880, 510, 880);

// Inside Decoration

line(360, 880, 540, 820);

line(390, 820, 510, 880);

// 3rd Base

line(390, 820, 540, 820);

// Left Tangent

line(300, 1000, 390, 820);

// Right Tangent

line(450, 1000, 540, 820);

// Joining Line

line(390, 820, 810, 820);

// Half Circle

ellipse(600, 900, 15, 165, 90, 80);

// Right Side

// Lower Base

line(750, 1000, 900, 1000);

// Inside Decoration

line(750, 1000, 870, 940);


line(720, 940, 900, 1000);

// 1st Base

line(720, 940, 870, 940);

// Inside Decoration

line(720, 940, 840, 880);

line(870, 940, 690, 880);

// 2nd Base

line(690, 880, 840, 880);

// Inside Decoration

line(690, 880, 810, 820);

line(840, 880, 660, 820);

// 3rd Base

line(660, 820, 810, 820);

// Left Tangent

line(750, 1000, 660, 820);

// Right Tangent

line(900, 1000, 810, 820);

// Rectangles For Decoration

rectangle(390, 800, 810, 820);


rectangle(380, 780, 820, 800);

rectangle(390, 760, 810, 780);

// Triangle Decoration

while (a <= 790) {

line(a, 820, a + 10, 800);

line(a + 10, 800, a + 20, 820);

a = a + 20;

// Vertical Line Decoration

while (b <= 810) {

line(b, 760, b, 780);

b = b + 20;

// Left Side

// Upper Base

line(410, 760, 530, 760);

// Inside Decoration

line(410, 760, 560, 700);

line(530, 760, 440, 700);

// 1st Base

line(440, 700, 560, 700);


// Inside Decoration

line(440, 700, 590, 640);

line(560, 700, 470, 640);

// 2nd base

line(470, 640, 590, 640);

// Left Tangent

line(410, 760, 470, 640);

// Right Tangent

line(540, 760, 590, 640);

// Right Side

// Upper Base

line(670, 760, 790, 760);

// Inside Decoration

line(670, 760, 760, 700);

line(790, 760, 640, 700);

// 1st Base

line(640, 700, 760, 700);

// Inside Decoration

line(640, 700, 730, 640);

line(760, 700, 610, 640);


// 2nd Base

line(610, 640, 730, 640);

// Left Tangent

line(670, 760, 610, 640);

// Right Tangent

line(790, 760, 730, 640);

// Joining Line

line(470, 640, 730, 640);

// Rectangle For Decoration

rectangle(460, 620, 740, 640);

rectangle(470, 600, 730, 620);

// Redeclaring Variable

b = 470;

// Vertical Line Decoration

while (b <= 730) {

line(b, 600, b, 620);

b = b + 10;

// Redeclaring Variable
a = 600;

b = 500;

// Middle Line

line(600, 600, 600, 140);

// Upper Most Decoration

while (b >= 240) {

if (b == c)

break;

else {

line(b, a, c, a);

line(b, a, c - 10, a - 40);

line(b + 10, a - 40, c, a);

a = a - 40;

b = b + 10;

c = c - 10;

// Tangent Lines

line(500, 600, 590, 240);

line(700, 600, 610, 240);

rectangle(590, 200, 610, 240);

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>

using namespace std;

int main(){

initwindow(800, 800);

// head

rectangle(300, 300, 400, 400);

// eyes

circle(330, 330, 10);

circle(370, 330, 10);

// mouth

ellipse(350, 380, 0, 360, 30, 10);

// body

rectangle(260, 400, 440, 600);

// legs

rectangle(285, 600, 330, 700);

rectangle(370, 600, 415, 700);

// arms
setlinestyle(0, 1, 12);

line(260, 500, 120, 480);

line(440, 500, 580, 480);

getch();

return 0;

Output:

35. Write a program to Draw the TajMahal.

Code // C program to implement


// the above approach

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

// Declaring functions used

// in this program

void taj_body();

void left_minars();

void right_minars();

// Driver Code

int main()

initwindow(1400, 800);

// Calling taj_body() function

taj_body();

// Calling left_minars() function

left_minars();

// Calling right_minars() function

right_minars();

getch();

return 0;
}

void taj_body()

// Main Base

rectangle(10, 650, 1350, 680);

rectangle(550, 300, 800, 650);

rectangle(540, 290, 810, 650);

// Left Door Line

line(600, 650, 600, 400);

// Right Door Line

line(750, 650, 750, 400);

line(600, 400, 675, 390);

line(675, 390, 750, 400);

arc(675, 240, 330, 210, 155);

// Main Finial

line(655, 85, 675, 30);

line(675, 30, 695, 87);

// Left Side

line(540, 315, 445, 315);

arc(490, 315, 0, 180, 45);


// Left Finial

line(480, 270, 490, 250);

line(490, 250, 500, 270);

// Left Vertical Line

line(445, 315, 445, 355);

// Lower Left Join

line(445, 355, 540, 355);

// Again Lower Left Join

line(445, 375, 540, 375);

// Left Tangent

line(445, 355, 375, 370);

// Lower Left Tangent

line(445, 375, 375, 390);

// Left Last Vertical

line(375, 370, 375, 650);

// Middle Left Rectangle

rectangle(450, 390, 530, 650);

// Left Rectangle Divider

line(450, 520, 530, 520);


// Left Rectangle Side Structure

line(440, 650, 440, 390);

line(385, 650, 385, 405);

line(440, 390, 385, 405);

// Divider

line(440, 520, 385, 520);

// Creating Left Lower Spike

line(460, 650, 460, 585);

line(520, 650, 520, 585);

line(460, 585, 490, 555);

line(520, 585, 490, 555);

// Creating Left Upper Spike

line(460, 520, 460, 455);

line(520, 520, 520, 455);

line(460, 455, 490, 425);

line(520, 455, 490, 425);

// Right Side

line(810, 315, 905, 315);

arc(860, 315, 0, 180, 45);

// Right Finial

line(850, 270, 860, 250);


line(860, 250, 870, 270);

// Right Vertical Line

line(905, 315, 905, 355);

line(905, 355, 810, 355);

// Lower Right Join

line(905, 355, 810, 355);

// Again Right Left Join

line(905, 375, 810, 375);

// Right Tangent

line(905, 355, 975, 370);

// Lower Right Tangent

line(905, 375, 975, 390);

// Right Last Vertical

line(975, 370, 975, 650);

// Middle Right Rectangle

rectangle(820, 390, 900, 650);

// Right Rectangle Divider

line(820, 520, 900, 520);


// Right Rectangle Side Structure

line(910, 650, 910, 390);

line(965, 650, 965, 405);

line(910, 390, 965, 405);

// Divider

line(910, 520, 965, 520);

// Creating Right Lower Spike

line(830, 650, 830, 585);

line(890, 650, 890, 585);

line(830, 585, 860, 555);

line(890, 585, 860, 555);

// Creating Right Upper Spike

line(830, 520, 830, 455);

line(890, 520, 890, 455);

line(830, 455, 860, 425);

line(890, 455, 860, 425);

void left_minars()

// 1st Left Minar

// 1st Step

line(20, 650, 30, 500);

line(120, 650, 110, 500);


line(110, 500, 30, 500);

// 2nd Step

line(30, 500, 20, 480);

line(110, 500, 120, 480);

line(120, 480, 20, 480);

line(30, 480, 40, 330);

line(110, 480, 100, 330);

line(40, 330, 100, 330);

line(40, 330, 30, 310);

line(100, 330, 110, 310);

line(110, 310, 30, 310);

// 3rd Step

line(40, 310, 50, 160);

line(100, 310, 90, 160);

line(50, 160, 90, 160);

line(50, 160, 40, 140);

line(90, 160, 100, 140);

line(40, 140, 100, 140);

line(40, 140, 60, 120);

line(100, 140, 80, 120);

line(60, 120, 80, 120);

line(60, 120, 70, 100);

line(70, 100, 80, 120);

// 2nd Left Minar


// 1st Step

line(170, 650, 180, 575);

line(270, 650, 260, 575);

line(180, 575, 260, 575);

// 2nd Step

line(180, 575, 170, 555);

line(260, 575, 270, 555);

line(170, 555, 270, 555);

line(180, 555, 190, 480);

line(260, 555, 250, 480);

line(190, 480, 250, 480);

// 3rd Step

line(190, 480, 180, 460);

line(250, 480, 260, 460);

line(180, 460, 260, 460);

line(190, 460, 200, 385);

line(250, 460, 240, 385);

line(200, 385, 240, 385);

line(200, 385, 190, 365);

line(240, 385, 250, 365);

line(190, 365, 250, 365);

line(190, 365, 210, 345);

line(250, 365, 230, 345);

line(210, 345, 230, 345);

line(210, 345, 220, 325);


line(220, 325, 230, 345);

void right_minars()

// 1st Right Minar

// 1st Step

line(1340, 650, 1330, 500);

line(1240, 650, 1250, 500);

line(1330, 500, 1250, 500);

// 2nd Step

line(1330, 500, 1340, 480);

line(1250, 500, 1240, 480);

line(1240, 480, 1340, 480);

line(1330, 480, 1320, 330);

line(1250, 480, 1260, 330);

line(1320, 330, 1260, 330);

line(1320, 330, 1330, 310);

line(1260, 330, 1250, 310);

line(1250, 310, 1330, 310);

// 3rd Step

line(1320, 310, 1310, 160);

line(1260, 310, 1270, 160);

line(1310, 160, 1270, 160);

line(1310, 160, 1320, 140);


line(1270, 160, 1260, 140);

line(1320, 140, 1260, 140);

line(1320, 140, 1300, 120);

line(1260, 140, 1280, 120);

line(1300, 120, 1280, 120);

line(1280, 120, 1290, 100);

line(1290, 100, 1300, 120);

// 2nd Right Minar

// 1st Step

line(1090, 650, 1100, 575);

line(1190, 650, 1180, 575);

line(1100, 575, 1180, 575);

// 2nd Step

line(1100, 575, 1090, 555);

line(1180, 575, 1190, 555);

line(1090, 555, 1190, 555);

line(1100, 555, 1110, 480);

line(1180, 555, 1170, 480);

line(1110, 480, 1170, 480);

// 3rd Step

line(1110, 480, 1100, 460);

line(1170, 480, 1180, 460);

line(1180, 460, 1100, 460);

line(1110, 460, 1120, 385);


line(1170, 460, 1160, 385);

line(1120, 385, 1160, 385);

line(1110, 365, 1120, 385);

line(1160, 385, 1170, 365);

line(1110, 365, 1170, 365);

line(1110, 365, 1130, 345);

line(1170, 365, 1150, 345);

line(1130, 345, 1150, 345);

line(1130, 345, 1140, 325);

line(1140, 325, 1150, 345);

}:

Output:

36. Write a program of the Sunrise Animation.

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>

using namespace std;

float round(float a);

int main(){

int x1, y1, x2, y2, steps, k;

float xincr, yincr, x, y, dx, dy;

cout << "Enter x1, y1\n";

cin >> x1 >> y1;

cout << "Enter x, y2\n";

cin >> x2 >> y2;

initwindow(400, 400);

dx = x2- x1;

dy = y2 - y1;

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

steps=abs(dx);
}

else{

steps=abs(dy);

xincr = dx / steps;

yincr = dy / steps;

x = x1;

y = y1;

for (k = 1; k <= steps; k++){

delay(100); //for seeing the line drawing process slowly.

x += xincr;

y += yincr;

putpixel(round(x), round(y), WHITE);

outtextxy(200, 20, "DDA"); // for printing text at desired screen location.

outtextxy(x1+5, y1-5, "(x1,y1)");

outtextxy(x2+5, y2+5, "(x2,y2)");

getch();

return 0;
}

float round(float a)

int b=a+0.5;

return b;

Output:

2. Plot a Straight line using the Midpoint Algorithm.

Code:

#include<iostream>
#include <graphics.h>

using namespace std;

// midPoint function for line generation

void midPoint(int X1, int Y1, int X2, int Y2)

// calculate dx & dy

int dx = X2 - X1;

int dy = Y2 - Y1;

if(dy<=dx){

// initial value of decision parameter d

int d = dy - (dx/2);

int x = X1, y = Y1;

// Plot initial given point

putpixel(x, y, WHITE);

delay(10);

cout << x << "," << y << "\n";

// iterate through value of X

while (x < X2)

{
x++;

// E or East is chosen

if (d < 0)

d = d + dy;

// NE or North East is chosen

else

d += (dy - dx);

y++;

// Plot intermediate points

putpixel(x, y, WHITE);

delay(10);

cout << x << "," << y << "\n";

else if(dx<dy){

// initial value of decision parameter d

int d = dx - (dy/2);

int x = X1, y = Y1;


// Plot initial given point

putpixel(x, y, WHITE);

delay(10);

cout << x << "," << y << "\n";

// iterate through value of X

while (y < Y2)

y++;

// E or East is chosen

if (d < 0)

d = d + dx;

// NE or North East is chosen

// NE or North East is chosen

else

d += (dx - dy);

x++;

// Plot intermediate points

putpixel(x, y, WHITE);

delay(10);

cout << x << "," << y << "\n";


}

int main()

initwindow(800, 800);

int X1 = 100, Y1 = 100, X2 = 100, Y2 = 355;

midPoint(X1, Y1, X2, Y2);

getch();

return 0;

Output:

3. Plot a Straight line using Bresenham's Algorithm.

Code:

#include <iostream>
#include <graphics.h>

using namespace std;

void drawline(int x0, int y0, int x1, int y1)

int dx, dy, p, x, y;

dx = x1 - x0;

dy = y1 - y0;

x = x0;

y = y0;

p = 2*dy - dx;

while (x < x1){

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

int x0, y0, x1, y1;

cout << "Enter co-ordinates of first point: ";

cin >> x0 >> y0;

cout << "Enter co-ordinates of second point: ";

cin >> x1 >> y1;

drawline(x0, y0, x1, y1);

getch();

return 0;

4. Plot a Circle using Bresenham’s Algorithm.

Code:

#include <iostream>

#include <graphics.h>

// Function to put pixels at subsequence points

void drawCircle(int xc, int yc, int x, int y)

putpixel(xc+x, yc+y, RED);

putpixel(xc-x, yc+y, RED);

putpixel(xc+x, yc-y, RED);

putpixel(xc-x, yc-y, RED);

putpixel(xc+y, yc+x, RED);

putpixel(xc-y, yc+x, RED);

putpixel(xc+y, yc-x, RED);


putpixel(xc-y, yc-x, RED);

// Function for circle-generation using Bresenham's algorithm

void circleBres(int xc, int yc, int r)

int x = 0, y = r;

int d = 3 - 2 * r;

drawCircle(xc, yc, x, y);

while (y >= x)

// for each pixel we will draw all eight pixels

x++;

// check for decision parameter and correspondingly update d, x, y

if (d > 0)

y--;

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

else{

d = d + 4 * x + 6;

drawCircle(xc, yc, x, y);


delay(50);

int main(){

int xc = 150, yc = 150, r = 50;

initwindow(300, 300);

circleBres(xc, yc, r); // function call

getch();

return 0;

Output:

5. Plot a Circle using Midpoint Circle Algorithm.

Code #include <graphics.h>

#include <math.h>
#include <iostream>

using namespace std;

void draw_circle(int, int, int);

void symmetry(int, int, int, int);

int main(){

initwindow(500, 500);

int xc, yc, R;

cout << "Enter the center of the circle:\n";

cout << "Xc = ";

cin >> xc;

cout << "Yc = ";

cin >> yc;

cout << "Enter the radius of the circle : ";

cin >> R;

draw_circle(xc, yc, R);

getch();

return 0;

}
void draw_circle(int xc, int yc, int rad){

int x = 0;

int y = rad;

int p = 1 - rad;

symmetry(x, y, xc, yc);

for (x=0;y>x;x++){

if(p < 0)

p += 2*x + 3;

else{

p += 2 * (x-y) + 5;

y--;

symmetry(x, y, xc, yc);

delay(1);

void symmetry(int x, int y, int xc, int yc){

putpixel(xc+x,yc-y,GREEN); //For pixel (x,y)

delay(1);
putpixel(xc+y,yc-x, GREEN); //For pixel (y,x)

delay(1);

putpixel(xc+y,yc+x, GREEN); //For pixel (y,-x)

delay(1);

putpixel(xc+x,yc+y, GREEN); //For pixel (x,-y)

delay(1);

putpixel(xc-x,yc+y, GREEN); //For pixel (-x,-y)

delay(1);

putpixel(xc-y,yc+x, GREEN); //For pixel (-y,-x)

delay(1);

putpixel(xc-y,yc-x, GREEN); //For pixel (-y,x)

delay(1);

putpixel(xc-x,yc-y, GREEN); //For pixel (-x,y)

delay(1);

}:

Output:
6. Plot an Ellipse using Midpoint Algorithm.
Code:

#include <iostream>

#include <graphics.h>

#include <math.h>

using namespace std;

void disp();

float x, y;

int xc, yc;

int main(){

int rx, ry;

float p1, p2;

initwindow(400, 400);

cout << "Enter the center point :";

cin >> xc >> yc;

cout <<"Enter the value for Rx and Ry :";

cin >> rx >> ry;

x = 0;

y = ry;
disp();

p1 = (ry*ry) - (rx*rx*ry) + (rx*rx)/4;

while ((2.0 * ry * ry * x) <= ( 2.0 * rx * rx * y)){

x++;

if(p1<=0)

p1=p1+(2.0*ry*ry*x)+(ry*ry);

else{

y--;

p1 = p1 + (2.0*ry*ry*x) - (2.0*rx*rx*y) + (ry*ry);

disp();

x =- x;

disp();

x =- x;

x = rx;

y = 0;

disp();

p2 = (rx*rx) + 2.0 * (ry*ry*rx) + (ry*ry)/4;


while((2.0*ry*ry*x)>(2.0*rx*rx*y))

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

putpixel(xc+x, yc+y, 10);

putpixel(xc-x, yc+y, 10);


putpixel(xc+x, yc-y, 10);

putpixel(xc-x, yc-y, 10);

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

Rotation in both directions clockwise or counterclockwise w.r.t to origin

Scaling uniform and nonuniform

Shearing in X-direction alone, Y-direction alone, in Both directions

Code:

#include <graphics.h>

#include <iostream>

#include <cmath>

using namespace std;

void matMul33(double mat1[3][3], double mat2[3][3]) {

double temp[3][3] = {};

int i, j, k;

// storing the values of the second matrix in a temporary matrix

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

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

temp[i][j] = mat2[i][j];

mat2[i][j] = 0;

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

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

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

mat2[i][j] += mat1[i][k] * temp[k][j];


}

void matMul34(double mat1[3][3], double mat2[3][4]) {

double temp[3][4] = {};

int i, j, k;

// storing the values of the second matrix in a temporary matrix

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

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

temp[i][j] = mat2[i][j];

mat2[i][j] = 0;

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

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

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

mat2[i][j] += mat1[i][k] * temp[k][j];

double degrees_to_radian(double deg)


{

return deg * M_PI / 180.0;

void draw_object(double obj[3][4]){

setcolor(RED);

line(obj[0][0], obj[1][0], obj[0][1], obj[1][1]);

setcolor(GREEN);

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

setcolor(BLUE);

line(obj[0][2], obj[1][2], obj[0][3], obj[1][3]);

setcolor(WHITE);

line(obj[0][3], obj[1][3], obj[0][0], obj[1][0]);

void translate(double obj[3][4], double tx, double ty){

double step_x = tx/100;

double step_y = ty/100;

for (int i = 0; i < 100; i++){

for (int k = 0; k < 4; k++){

obj[0][k] += step_x;

obj[1][k] += step_y;

delay(10);

cleardevice();
draw_object(obj);

void rotate(double obj[3][4], double deg){

double step, tx, ty, SIN, COS, cx, cy, sx, sy, I1, K1, I2, K2;

char rot = 'p';

// setting the rotation point to the origin

tx = 0;

ty = 0;

if (deg < 0){

rot = 'n';

deg = abs(fmod(deg, 360.0));

step = deg / 100;

deg = 0.0;

for (int i = 0; i < 100; i++){

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

// drawing translated object

draw_object(obj);
}

void scale(double obj[3][4], double sx=1.0, double sy=1.0){

double step_x = 0, step_y = 0;

double temp_x = sx * (fabs(obj[1][3] - obj[1][0]))/100;

double temp_y = sy * (fabs(obj[0][1] - obj[0][0]))/100;

double temp[3][4];

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

for (int j = 0; j < 4; j++) {

temp[i][j] = obj[i][j];

for (int i = 0; i < 100; i++){

step_x += temp_x;

step_y += temp_y;

obj[0][1] = temp[0][0] + step_x;

obj[0][2] = temp[0][0] + step_x;

obj[1][2] = temp[1][0] + step_y;

obj[1][3] = temp[1][0] + step_y;

delay(10);

cleardevice();
draw_object(obj);

void shear(double obj[3][4], double shx=0, double shy=0){

double tempX = shx / 100;

double tempY = shy / 100;

shx = 0;

shy = 0;

double temp[3][4];

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

for (int j = 0; j < 4; j++) {

temp[i][j] = obj[i][j];

for (int i = 0; i < 100; i++){

shx += tempX;

shy += tempY;

obj[0][3] += shx * temp[1][3]/100;

obj[0][2] += shx * temp[1][2]/100;


obj[1][2] += shy * temp[0][2]/100;

obj[1][1] += shy * temp[0][1]/100;

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

double tx, ty, sx, sy, shx, shy, deg = 0;

char rot = 'p';

int option = 1, sh_opt;

// matricies

double trans_mat[3][3] = {{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};

double neg_trans_mat[3][3] = {{1, 0, -tx}, {0, 1, -ty}, {0, 0, 1}};

// drawing object

draw_object(obj);

cout << "\n\n\nEnter the type of transformation you want to apply:\n";

while (1){
cout << "\n1. Translation \n2. Rotation \n3. Scaling \n4. Shearing \n5. EXIT\n";

cin >> option;

switch (option){

case 1: cout << "\nEnter translation factor x and y: ";

cin >> tx >> ty;

translate(obj, tx, ty);

break;

case 2: cout << "\nEnter the angle of rotation: ";

cin >> deg;

rotate(obj, deg);

break;

case 3: cout << "\nEnter scaling factor x and y: ";

cin >> sx >> sy;

scale(obj, sx, sy);

break;

case 4: cout << "\nEnter the shearing axis:" << endl;

cout << "1. x-axis \n2. y-axis \n3. Both x and y axes\n";

cin >> sh_opt;

switch (sh_opt){

case 1: cout << "\nEnter shearing factor x: ";

cin >> shx;


shear(obj, shx);

break;

case 2: cout << "\nEnter shearing factor y: ";

cin >> shy;

shear(obj, shy);

break;

case 3: cout << "\nEnter shearing factor x and y: ";

cin >> shx >> shy;

shear(obj, shx, shy);

break;

default: shear(obj);

break;

case 5: exit(0);

default: cout << "Enter a valid option";

getch();

return 0;

Output:
8. Draw an object i.e Triangle and perform the following Reflection on it.
Reflection along X-axis

Reflection along Y-axis

Reflection along y = x line

Reflection along y = -x line

Code: #include <graphics.h>

#include <iostream>

#include <cmath>

using namespace std;

int ORIGIN = 400;

void draw_grid(int o){

setcolor(WHITE);

line(0, o, 800, o);

line(o, 0, o, 800);

line(0, 800, 800, 0);

line(0, 0, 800, 800);

void matMul33(double mat1[3][3], double mat2[3][3]) {

double temp[3][3] = {};

int i, j, k;

// storing the values of the second matrix in a temporary matrix

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

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


temp[i][j] = mat2[i][j];

mat2[i][j] = 0;

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

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

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

mat2[i][j] += mat1[i][k] * temp[k][j];

double degrees_to_radian(double deg)

return deg * M_PI / 180.0;

void draw_object(double obj[3][3]){

setcolor(RED);

line(obj[0][0], obj[1][0], obj[0][1], obj[1][1]);

setcolor(GREEN);

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

setcolor(BLUE);

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

}
void translate(double obj[3][3], double tx, double ty){

double step_x = tx/100;

double step_y = ty/100;

for (int i = 0; i < 100; i++){

for (int k = 0; k < 4; k++){

obj[0][k] += step_x;

obj[1][k] += step_y;

// delay(10);

// cleardevice();

// draw_object(obj);

void rotate(double obj[3][3], double deg){

double step, tx, ty, SIN, COS, cx, cy, sx, sy, I1, K1, I2, K2;

char rot = 'p';

// setting the rotation point to the origin

tx = 400;

ty = 400;

if (deg < 0){

rot = 'n';
}

deg = abs(fmod(deg, 360.0));

step = deg / 100;

deg = 0.0;

for (int i = 0; i < 100; i++){

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

// drawing translated object

draw_object(obj);

void reflect(double obj[3][3], string axis="x"){

if (axis == "x"){

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

obj[1][i] = -obj[1][i];

obj[1][i] += 2*ORIGIN;

cleardevice();

draw_grid(ORIGIN);

draw_object(obj);

}
else if (axis == "y"){

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

obj[0][i] = -obj[0][i];

obj[0][i] += 2*ORIGIN;

cleardevice();

draw_grid(ORIGIN);

draw_object(obj);

void reflectyx(double obj[3][3]){

rotate(obj, 45);

reflect(obj, "x");

rotate(obj, -45);

void reflectxy(double obj[3][3]){

rotate(obj, 45);

reflect(obj, "y");

rotate(obj, -45);

int main(){
initwindow(800, 800);

draw_grid(ORIGIN);

int option = 1;

string axis = "yx";

double obj[3][3] = {{180+ORIGIN, 260+ORIGIN, 220+ORIGIN}, {ORIGIN-(-150), ORIGIN-(-150), ORIGIN-(-


200)}, {1, 1, 1}};

draw_object(obj);

cout << "\nEnter the reflection axis\n";

while (1){

cout << "\n1. x-axis \n2. y-axis \n3. y=x line \n4. x=y line \n5. EXIT\n";

cin >> option;

switch (option){

case 1: reflect(obj, "x");

break;

case 2: reflect(obj, "y");

break;

case 3: reflectyx(obj);

break;

case 4: reflectxy(obj);
break;

case 5: exit(0);

default: cout << "Enter a valid option";

getch();

return 0;

Output:
9. Implement cohen-sutherland algorithm for Line clipping

Code:

#include<iostream>

#include<cmath>

#include<graphics.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(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);

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:

You might also like