0% found this document useful (0 votes)
65 views52 pages

Sector - 16 C, Dwarka, Delhi, India: Sirifort Institute of Management Studies

Here is a program to rotate a line using 2D rotation matrix: #include <graphics.h> #include <conio.h> #include <math.h> void rotate(float x1, float y1, float angle) { float x2, y2, x, y; x2 = x1; y2 = y1; x = x2*cos(angle) - y2*sin(angle); y = x2*sin(angle) + y2*cos(angle); putpixel(x, y, RED); } int main() { int gd = DETECT, gm; float

Uploaded by

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

Sector - 16 C, Dwarka, Delhi, India: Sirifort Institute of Management Studies

Here is a program to rotate a line using 2D rotation matrix: #include <graphics.h> #include <conio.h> #include <math.h> void rotate(float x1, float y1, float angle) { float x2, y2, x, y; x2 = x1; y2 = y1; x = x2*cos(angle) - y2*sin(angle); y = x2*sin(angle) + y2*cos(angle); putpixel(x, y, RED); } int main() { int gd = DETECT, gm; float

Uploaded by

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

CG Practical File

Guru Gobind Singh Indraprastha University


Sector -16 C, Dwarka, Delhi, India

Practical- CG Lab
BCA-351
By
Name: Mehak
Enrollment No: 02016702018

Under the guidance:

Dr. Meenaksshi Yadav

Sirifort Institute of Management Studies


Plot No.8, Institutional Area Rohini Sector-25, New Delhi 110085

Mehak
(02016702018)
CG Practical File

CONTENTS PAGE

S.no Particulars Dated Signature


.
1. Write a program to draw the basic
shapes
(line,circle,rectangle,ellipse,sector,polyg
on,arc).
2. Write a program to set the background
color.
3 Write a program to draw a human face.
4 Write a program to generate circles on
the screen randomly.
5 Write a program to generate circles with
random radius and random pixel on the
screen .
6 Write a program to print a line using
DDA algorithm.
7 Write a program to print a line using
BRESENHAM ’s algorithm.

8 Write a program to create a circle


through mid-point algorithm.
9 Write a program to draw an ellipse by
using x2/a2+y2/b2=1.
10 Write a program to translate a point .
11 Write a program to rotate a line
12 Write a program to 2D transformation.
13 Write a program to create a rainbow.
14 Write a program to draw a moving car.
15 Write a program to design concentric
circles

Mehak
(02016702018)
CG Practical File

Write a program to design traffic light


16 simulator

17 Write a program to draw animation using


increasing circles filled with different
colors and patterns.
18 Write a program to draw Beezier curve
19 Write a program to draw a house
20 Write a program to draw Solar system.

Mehak
(02016702018)
CG Practical File

QUE 1. Write a program to draw the basic shapes


(line,circle,rectangle,ellipse,sector,polygon,arc).
#include<graphics.h>

#include<conio.h>

main()

int gd = DETECT,gm;

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

setbkcolor(GREEN);

printf("\t\t\t\n\nLINE");

line(50,40,190,40);

printf("\t\t\n\n\n\nRECTANGLE");

rectangle(125,115,215,165);

printf("\t\t\t\n\n\n\n\n\n\nARC");

arc(120,200,180,0,30);

printf("\t\n\n\n\nCIRCLE");

circle(120,270,30);

printf("\t\n\n\n\nELLIPSE");

ellipse(120,350,0,360,30,20);

printf("\t\n\n\n\nSECTOR");

sector(120,450,0,150,50,65);

getch();

closegraph();

return 0;

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 2. Write a program to set the background color.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{ int gd=DETECT, gm;

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

setbkcolor(RED);

getch();

closegraph();

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 3. Write a program to draw a human face.


#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <stdio.h>

int main()

int gr = DETECT, gm;

initgraph(&gr, &gm, "C:\\TC\\BGI");

setcolor(YELLOW);

circle(300, 100, 40);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(300, 100, YELLOW);

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

fillellipse(310, 85, 2, 6);

fillellipse(290, 85, 2, 6);

ellipse(300, 100, 205, 335, 20, 9);

ellipse(300, 100, 205, 335, 20, 10);

ellipse(300, 100, 205, 335, 20, 11);

getch();

closegraph();

return 0;

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 4. Write a program to generate circles on the screen


randomly.
#include<stdio.h>

#include<conio.h>

#include"graphics.h"

#include"stdlib.h"

void main()

int gd=DETECT,gm,i=0,x,xx,y,yy,r;

//Initializes the graphics system

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

x=getmaxx();

y=getmaxy();

while(!kbhit())

i++;

// setfillstyle(random(i),random(3));

circle(xx=random(x),yy=random(y),random(300));

setfillstyle(random(i),random(300));

floodfill(xx,yy,getmaxcolor());

delay(200);

getch();

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 5. Write a program to generate circles with random radius


and random pixel on the screen .

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

int main()

int gd = DETECT, gm;

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

circle(250, 200, 50);

circle(180,100,60);

circle(300,300,70);

getch();

closegraph();

return 0;

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 6. Write a program to print a line using DDA algorithm.


#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <dos.h>

void main( )

float x,y,x1,y1,x2,y2,dx,dy,step;

int i,gd=DETECT,gm;

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

printf("Enter the value of x1 and y1 : ");

scanf("%f%f",&x1,&y1);

printf("Enter the value of x2 and y2: ");

scanf("%f%f",&x2,&y2);

dx=abs(x2-x1);

dy=abs(y2-y1);

if(dx>=dy)

step=dx;

else

step=dy;

dx=dx/step;

dy=dy/step;

x=x1;

y=y1;

Mehak
(02016702018)
CG Practical File

i=1;

while(i<=step)

putpixel(x,y,5);

x=x+dx;

y=y+dy;

i=i+1;

delay(100);

closegraph();

getch();

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 7. Write a program to print a line using BRESENHAM ’s


algorithm.
#include<stdio.h>

#include<graphics.h>

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

y=y+1;

p=p+2*dy-2*dx;

else

putpixel(x,y,7);

p=p+2*dy;}

x=x+1;

Mehak
(02016702018)
CG Practical File

int main()

int gdriver=DETECT, gmode, error, x0, y0, x1, y1;

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter co-ordinates of first point: ");

scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");

scanf("%d%d", &x1, &y1);

drawline(x0, y0, x1, y1);

getch();

return 0;

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 8. Write a program to create a circle through mid-point


algorithm.
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void drawcircle(int x0, int y0, int radius)

{ int x = radius;

int y = 0;

int err = 0;

while (x >= y)

{ putpixel(x0 + x, y0 + y, 7);

putpixel(x0 + y, y0 + x, 7);

putpixel(x0 - y, y0 + x, 7);

putpixel(x0 - x, y0 + y, 7);

putpixel(x0 - x, y0 - y, 7);

putpixel(x0 - y, y0 - x, 7);

putpixel(x0 + y, y0 - x, 7);

putpixel(x0 + x, y0 - y, 7);

if (err <= 0)

y += 1;

err += 2*y + 1;

if (err > 0)

Mehak
(02016702018)
CG Practical File

x -= 1;

err -= 2*x + 1;

} } }

int main()

{ int gdriver=DETECT, gmode, error, x, y, r;

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter radius of circle: ");

scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");

scanf("%d%d", &x, &y);

drawcircle(x, y, r);

getch();

return 0;

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 10. Write a program to translate a point .

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void translatePoint(int p[],int t[]);

int main()

int P[2] = {300, 400}; // coordinates of point

int T[] = {60, 60}; // translation factor

translatePoint (P, T);

return 0;

void translatePoint ( int P[], int T[])

int gd = DETECT, gm, errorcode;

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

printf("Original cordinates : %d,%d" ,P[0],P[1]);

putpixel (P[0], P[1], 1);

// calculating translated coordinates

P[0] = P[0] + T[0];

P[1] = P[1] + T[1];

printf("\n Translated cordinates : %d,%d" ,P[0],P[1]);

putpixel (P[0], P[1], 3);

Mehak
(02016702018)
CG Practical File

getch();

closegraph();

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 11. Write a program to rotate a line.


#include<stdio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

int x1,y1,x2,y2 ;

float b1,b2;

float t,deg;

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

printf("Enter the coordinates of Line \n");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

setcolor(6);

line(x1,y1,x2,y2);

getch();

//cleardevice();

printf("Enter the angle of rotation: ");

scanf("%f",&deg);

t=(22*deg)/(180*7);

b1=abs((x2*cos(t))-(y2*sin(t)));

b2=abs((x2*sin(t))+(y2*cos(t)));

line(x1,y1,b1,b2);

getch();

closegraph();

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 12. Write a program for 2D transformation.


#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void translate();

void scale();

void rotate();

void main()

int ch;

int gd=DETECT,gm;

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

setcolor(6);

outtextxy (100,88,"Object.");

rectangle(100,150,150,100);

printf("---MENU---");

printf("\n 1)Translate\n 2)Scale\n 3)Rotate");

printf("\nEnter your choice: ");

scanf("%d",&ch);

cleardevice();

switch(ch)

case 1: translate();

break;

Mehak
(02016702018)
CG Practical File

case 2: scale();

break;

case 3: rotate();

break;

default: printf("you have enterd wrong choice");

break;

getch();

closegraph();

void translate()

int tx,ty;

setcolor(2);

outtextxy(240,10,"TRANSLATION");

outtextxy(238,20,"------------");

printf("\nEnter tx: ");

scanf("%d",&tx);

printf("\nEnter ty: ");

scanf("%d",&ty);

cleardevice();

rectangle(100,150,150,100);

printf("\nAfter Translation");

rectangle(100+tx,150+ty,150+tx,100+ty);

Mehak
(02016702018)
CG Practical File

void scale()

int sx,sy;

setcolor(2);

outtextxy(240,10,"SCALING");

outtextxy(238,20,"--------");

printf("\nEnter sx: ");

scanf("%d",&sx);

printf("\nEnter sy: ");

scanf("%d",&sy);

cleardevice();

rectangle(100,150,150,100);

printf("\nAfter Scaling");

rectangle(100*sx,150*sy,150*sx,100*sy);

void rotate()

{ float theta;

int x1,x2,x3,x4;

int y1,y2,y3,y4;

int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;

int refx,refy;

printf("\nEnter the angle for rotation: ");

scanf("%f",&theta);

theta=theta*(3.14/180);

cleardevice();

setcolor(2);

Mehak
(02016702018)
CG Practical File

outtextxy(240,10,"ROTATE");

outtextxy(238,20,"-------");

refx=100;

refy=100;

x1=100;

y1=100;

x2=150;

y2=100;

x3=150;

y3=150;

x4=100;

y4=150;

ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);

ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);

ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);

ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);

ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);

ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);

rectangle(100,150,150,100);

line(ax1,ay1,ax2,ay2);

line(ax2,ay2,ax3,ay3);

line(ax3,ay3,ax4,ay4);

line(ax4,ay4,ax1,ay1);

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 13. Write a program to create a rainbow.


#include<stdio.h>

#include<graphics.h>

#include<dos.h>

// function for making of rainbow

void rainbow()

// auto detection

int gdriver = DETECT,gmode;

int x, y, i;

initgraph(&gdriver,&gmode,"C:\\Tc\\BGI");

x = getmaxx() / 2;//finding centre x-ordinate of screen

y = getmaxy() / 2;//finding centre y-ordinate of screen

for (i=30; i<200; i++)

// delay function under dos.h for holding the

// function for some time

delay(100);

// selecting color for making of rainbow

setcolor(i/10);

// making of arc with fixed centre and increasing radius

Mehak
(02016702018)
CG Practical File

arc(x, y, 0, 180, i-10);

// driver program

int main()

rainbow();

return 0; }

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 14. Write a program to draw a moving car.


#include <graphics.h>

#include <stdio.h>

void draw_moving_car(void) {

int i, j = 0, gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Tc\\BGI");

for (i = 0; i <= 420; i = i + 10) {

// Set color of car as red

setcolor(RED);

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

Mehak
(02016702018)
CG Practical File

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

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

Mehak
(02016702018)
CG Practical File

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

closegraph();

// Driver code

int main()

draw_moving_car();

return 0;

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 15. Write a program to design concentric circles.


#include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main(){

int gd = DETECT,gm;

int x ,y;

initgraph(&gd, &gm, "C:\\TC\\BGI");

/* Initialize center of circle with center of screen */

x = getmaxx()/2;

y = getmaxy()/2;

outtextxy(240, 50, "Concentric Circles");

/* Draw circles on screen */

setcolor(RED);

circle(x, y, 30);

setcolor(GREEN);

circle(x, y, 50);

setcolor(YELLOW);

circle(x, y, 70);

setcolor(BLUE);

circle(x, y, 90);

Mehak
(02016702018)
CG Practical File

getch();

closegraph();

return 0;

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 16. Write a program to design traffic light simulator.

#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<stdlib.h>

main()

int gd=DETECT,gm,midx,midy;

initgraph(&gd,&gm,"C:\\TC\\BGI");

midx=getmaxx()/2;

midy=getmaxy()/2;

setcolor(RED);

settextstyle(SCRIPT_FONT,HORIZ_DIR,3);

settextjustify(CENTER_TEXT,CENTER_TEXT);

outtextxy(midx,midy-10,"Traffic Light Simulation");

outtextxy(midx,midy+10,"Press any key to start");

getch();

cleardevice();

setcolor(WHITE);

settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

rectangle(midx-30,midy-80,midx+30,midy+80);

circle(midx,midy-50,22);

setfillstyle(SOLID_FILL,RED);

floodfill(midx,midy-50,WHITE);

Mehak
(02016702018)
CG Practical File

setcolor(BLUE);

outtextxy(midx,midy-50,"STOP");

delay(2000);

graphdefaults();

cleardevice();

setcolor(WHITE);

rectangle(midx-30,midy-80,midx+30,midy+80);

circle(midx,midy,20);

setfillstyle(SOLID_FILL,YELLOW);

floodfill(midx,midy,WHITE);

setcolor(BLUE);

outtextxy(midx-18,midy-3,"READY");

delay(2000);

cleardevice();

setcolor(WHITE);

rectangle(midx-30,midy-80,midx+30,midy+80);

circle(midx,midy+50,22);

setfillstyle(SOLID_FILL,GREEN);

floodfill(midx,midy+50,WHITE);

setcolor(BLUE);

outtextxy(midx-7,midy+48,"GO");

setcolor(RED);

settextstyle(SCRIPT_FONT,HORIZ_DIR,4);

outtextxy(midx-150,midy+100,"Press any key to exit...");

getch();

closegraph();

Mehak
(02016702018)
CG Practical File

return 0;

OUTPUT:

Mehak
(02016702018)
CG Practical File

Mehak
(02016702018)
CG Practical File

QUE 17. Write a program to draw animation using increasing


circles filled with different colors and patterns.
#include<graphics.h>

#include<conio.h>

void main()

int gd=DETECT, gm, i, x, y;

initgraph(&gd, &gm, "C:\\TC\\BGI");

x=getmaxx()/3;

y=getmaxx()/3;

setbkcolor(WHITE);

setcolor(BLUE);

for(i=1;i<=8;i++)

setfillstyle(i,i);

delay(20);

circle(x, y, i*20);

floodfill(x-2+i*20,y,BLUE);

getch();

closegraph();

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 18. Write a program to draw Bezier curve.


#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

#include <math.h>

void bezier (int x[4], int y[4])

int gd = DETECT, gm;

int i;

double t;

initgraph (&gd, &gm, "C:\\tc\\bgi");

for (t = 0.0; t < 1.0; t += 0.0005)

double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +

3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];

double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +

3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];

putpixel (xt, yt, WHITE);

Mehak
(02016702018)
CG Practical File

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

putpixel (x[i], y[i], YELLOW);

getch();

closegraph();

return;

void main()

int x[4], y[4];

int i;

printf ("Enter the x- and y-coordinates of the four control points.\n");

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

scanf ("%d%d", &x[i], &y[i]);

bezier (x, y);

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 19. Write a program to draw a house.


#include<graphics.h>

#include<conio.h>

int main(){

int gd = DETECT,gm;

initgraph(&gd, &gm, "c:\\TC\\BGI");

/* Draw Hut */

setcolor(WHITE);

rectangle(150,180,250,300);

rectangle(250,180,420,300);

rectangle(180,250,220,300);

line(200,100,150,180);

line(200,100,250,180);

line(200,100,370,100);

line(370,100,420,180);

/* Fill colours */

setfillstyle(SOLID_FILL, BROWN);

floodfill(152, 182, WHITE);

floodfill(252, 182, WHITE);

setfillstyle(SLASH_FILL, BLUE);

floodfill(182, 252, WHITE);

setfillstyle(HATCH_FILL, GREEN);

floodfill(200, 105, WHITE);

Mehak
(02016702018)
CG Practical File

floodfill(210, 105, WHITE);

getch();

closegraph();

return 0;

OUTPUT:

Mehak
(02016702018)
CG Practical File

QUE 20. Write a program to draw Solar System.


#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

#include <math.h>

/* manipulates the position of planets on the orbit */

void planetMotion(int xrad, int yrad, int midx, int midy, int x[60], int y[60]) {

int i, j = 0;

/* positions of planets in their corresponding orbits */

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

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

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

return;

int main() {

/* request auto detection */

int gdriver = DETECT, gmode, err;

int i = 0, midx, midy;

int xrad[9], yrad[9], x[9][60], y[9][60];

int pos[9], planet[9], tmp;

/* initialize graphic mode */

initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

err = graphresult();

Mehak
(02016702018)
CG Practical File

if (err != grOk) {

/* error occurred */

printf("Graphics Error: %s",

grapherrormsg(err));

return 0;

/* mid positions at x and y-axis */

midx = getmaxx() / 2;

midy = getmaxy() / 2;

/* manipulating radius of all 9 planets */

planet[0] = 7;

for (i = 1; i < 9; i++) {

planet[i] = planet[i - 1] + 1;

/* offset position for the planets on their corresponding orbit */

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

pos[i] = i * 6;

/* orbits for all 9 planets */

xrad[0] = 60, yrad[0] = 30;

for (i = 1; i < 9; i++) {

xrad[i] = xrad[i - 1] + 30;

yrad[i] = yrad[i - 1] + 15;

Mehak
(02016702018)
CG Practical File

/* positions of planets on their corresponding orbits */

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

planetMotion(xrad[i], yrad[i], midx, midy, x[i], y[i]);

while (!kbhit()) {

/* drawing 9 orbits */

setcolor(WHITE);

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

ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);

/* sun at the mid of the solar system */

setcolor(YELLOW);

setfillstyle(SOLID_FILL, YELLOW);

circle(midx, midy, 20);

floodfill(midx, midy, YELLOW);

/* mercury in first orbit */

setcolor(CYAN);

setfillstyle(SOLID_FILL, CYAN);

pieslice(x[0][pos[0]], y[0][pos[0]], 0, 360, planet[0]);

/* venus in second orbit */

setcolor(GREEN);

setfillstyle(SOLID_FILL, GREEN);

pieslice(x[1][pos[1]], y[1][pos[1]], 0, 360, planet[1]);

/* earth in third orbit */

setcolor(BLUE);

Mehak
(02016702018)
CG Practical File

setfillstyle(SOLID_FILL, BLUE);

pieslice(x[2][pos[2]], y[2][pos[2]], 0, 360, planet[2]);

/* mars in fourth orbit */

setcolor(RED);

setfillstyle(SOLID_FILL, RED);

pieslice(x[3][pos[3]], y[3][pos[3]], 0, 360, planet[3]);

/* jupiter in fifth orbit */

setcolor(BROWN);

setfillstyle(SOLID_FILL, BROWN);

pieslice(x[4][pos[4]], y[4][pos[4]], 0, 360, planet[4]);

/* saturn in sixth orbit */

setcolor(LIGHTGRAY);

setfillstyle(SOLID_FILL, LIGHTGRAY);

pieslice(x[5][pos[5]], y[5][pos[5]], 0, 360, planet[5]);

/* uranus in sevth orbit */

setcolor(BROWN);

setfillstyle(SOLID_FILL, BROWN);

pieslice(x[6][pos[6]], y[6][pos[6]], 0, 360, planet[6]);

/* neptune in eigth orbit */

setcolor(LIGHTBLUE);

setfillstyle(SOLID_FILL, LIGHTBLUE);

Mehak
(02016702018)
CG Practical File

pieslice(x[7][pos[7]], y[7][pos[7]], 0, 360, planet[7]);

/* pluto in ninth orbit */

setcolor(LIGHTRED);

setfillstyle(SOLID_FILL, LIGHTRED);

pieslice(x[8][pos[8]], y[8][pos[8]], 0, 360, planet[8]);

/* checking for one complete rotation */

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

if (pos[i] <= 0) {

pos[i] = 59;

} else {

pos[i] = pos[i] - 1;

/* sleep for 100 milliseconds */

delay(100);

/* clears graphic screen */

cleardevice();

/* deallocate memory allocated for graphic screen */

closegraph();

return 0;

Mehak
(02016702018)
CG Practical File

OUTPUT:

Mehak
(02016702018)

You might also like