You are on page 1of 43

Contents

1.Implementation of Line Drawing Algorithms............................................................................................2


DDA algorithm......................................................................................................................................2
Bresenham's Line Drawing Algorithm................................................................................................3
2.Implementation of Circle Drawing Algorithms.........................................................................................5
Bresenham Circle Drawing Algorithm:...............................................................................................5
Mid-point Circle Drawing Algorithm:.................................................................................................6
3.Programs on 2D and 3D Transformations: Translation, Rotation, Scaling and Shearing of an Object......8
2D Transformation:...............................................................................................................................8
4.Write a program to implement Cohen Sutherland Line Clipping, Sutherland Hodgeman polygon
clipping......................................................................................................................................................11
Cohen Sutherland Line Clipping:......................................................................................................11
Sutherland Hodgeman polygon clipping:..........................................................................................13
5. Write a program to apply composite scaling and rotation to 2D-shapes...............................................18
Composite 2d transformation:............................................................................................................18
6. Write a program to translation and rotation to 3D shapes....................................................................19
3D translation and rotation:...............................................................................................................19
7.Write a program to create empty fill, solid fill, line fill, wide dot fill, close dot fill and user fill using
ellipse attribute.........................................................................................................................................22
8. Create a Bouncing Ball using key frame animation and path animation...............................................24
9. Write a Program to show animation of a ball moving in a helical path.................................................25
10. Write a program to show ticking clock using graphics.........................................................................27
11. Create and rotate a triangle about the origin and a fixed point..........................................................30
12.Draw a color cube and spin it using OpenGL transformation matrices.................................................32
13. Develop Programs for making Simple animation using transformations like rotation scaling and
translation.................................................................................................................................................34
Circle Moving from left to right and vice versa................................................................................34
Man object moving:.............................................................................................................................35
Man Walking:......................................................................................................................................37
Wind mill Rotation..............................................................................................................................38
Simple animation of a football goal:...................................................................................................40
1.Implementation of Line Drawing Algorithms.
DDA algorithm

#include "graphics.h"

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main() {
int gdriver = DETECT, gmode;
int x1, y1, x2, y2, step, xn, yn, dx, dy;
initgraph( & gdriver, & gmode, NULL);
printf("Enter the starting coordinates\n");
scanf("%d%d", & x1, & y1);
printf("Enter the end coordinates\n");
scanf("%d%d", & x2, & y2);
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy)) step = abs(dx);

else step = abs(dy);


xn = dx / step;
yn = dy / step;
for (int i = 0; i <= step; i++) {
putpixel(x1, y1, WHITE);
delay(100);
x1 = x1 + xn;
y1 = y1 + yn;
}
getch();
closegraph();
return 0;
}

Output:
Bresenham's Line Drawing 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) {


delay(50);
if (p >= 0) {
putpixel(x, y, WHITE);
y = y + 1;
p = p + 2 * dy - 2 * dx;
} else {
putpixel(x, y, WHITE);
p = p + 2 * dy;
}
x = x + 1;
}
}

int main() {
int gdriver = DETECT, gmode, error, x0, y0, x1, y1;
initgraph( & gdriver, & gmode, "c:\\turboc3\\bgi");

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


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

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


scanf("%d%d", & x1, & y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}

Output:
2.Implementation of Circle Drawing Algorithms.
Bresenham Circle Drawing Algorithm:
#include <stdio.h>

#include <dos.h>

#include <graphics.h>

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


putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
}

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

x++;

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 = 300, yc = 200, r2 = 100;
int gd = DETECT, gm;
initgraph( & gd, & gm, ""); // initialize graph
circleBres(xc, yc, r2);
getch(); // function call
return 0;
}

Output:
Mid-point Circle Drawing Algorithm:

#include<stdio.h>

#include<graphics.h>

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


int x = radius;
int y = 0;
int err = 0;

while (x >= y) {
delay(50);
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) {
x -= 1;
err -= 2 * x + 1;
}
}
}

int main() {
int gdriver = DETECT, gmode, error, x, y, r;
initgraph( & gdriver, & gmode, "c:\\turboc3\\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:
3.Programs on 2D and 3D Transformations: Translation, Rotation,
Scaling and Shearing of an Object
2D Transformation:

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

using namespace std;


int main() {
int gd = DETECT, gm, s;
initgraph( & gd, & gm, (char * )
"");
cout << "1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing
" << endl;
cout << "Selection:";
cin >> s;
switch (s) {
case 1: {
int x1 = 200, y1 = 150, x2 = 300, y2 = 250;
int tx = 50, ty = 50;
cout << "Rectangle before translation" << endl;
setcolor(3);
rectangle(x1, y1, x2, y2);
setcolor(4);
cout << "Rectangle after translation" << endl;
rectangle(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
getch();
break;
}
case 2: {
long x1 = 200, y1 = 200, x2 = 300, y2 = 300;
double a;
cout << "Rectangle with rotation" << endl;
setcolor(3);
rectangle(x1, y1, x2, y2);
cout << "Angle of rotation:";
cin >> a;
a = (a * 3.14) / 180;
long xr = x1 + ((x2 - x1) * cos(a) - (y2 - y1) * sin(a));
long yr = y1 + ((x2 - x1) * sin(a) + (y2 - y1) * cos(a));
setcolor(2);
rectangle(x1, y1, xr, yr);
getch();
break;
}
case 3: {
int x1 = 30, y1 = 30, x2 = 70, y2 = 70, y = 2, x = 2;
cout << "Before scaling" << endl;
setcolor(3);
rectangle(x1, y1, x2, y2);
cout << "After scaling" << endl;
setcolor(10);
rectangle(x1 * x, y1 * y, x2 * x, y2 * y);
getch();
break;
}
case 4: {
int x1 = 200, y1 = 300, x2 = 500, y2 = 300, x3 = 350, y3 = 400;
cout << "triangle before reflection" << endl;
setcolor(3);
line(x1, y1, x2, y2);
line(x1, y1, x3, y3);
line(x2, y2, x3, y3);
cout << "triangle after reflection" << endl;
setcolor(5);
line(x1, -y1 + 500, x2, -y2 + 500);
line(x1, -y1 + 500, x3, -y3 + 500);
line(x2, -y2 + 500, x3, -y3 + 500);
getch();
break;
}
case 5: {
int x1 = 50, y1 = 100, x2 = 150, y2 = 100, x3 = 50, y3 = 200, x4 = 150,
y4 = 200, shx = 2;
cout << "Before shearing of rectangle" << endl;
setcolor(3);
line(x1, y1, x2, y2);
line(x1, y1, x3, y3);
line(x3, y3, x4, y4);
line(x2, y2, x4, y4);
cout << "After shearing of rectangle" << endl;
x1 = x1 + shx * y1;
x2 = x2 + shx * y2;
x3 = x3 + shx * y3;
x4 = x4 + shx * y4;
setcolor(13);
line(x1, y1, x2, y2);
line(x1, y1, x3, y3);
line(x3, y3, x4, y4);
line(x2, y2, x4, y4);
getch();
}
default: {
cout << "Invalid Selection" << endl;
break;
}
}
closegraph();
return 0;
}
Output:
Translation of rectangle:

Rotation of Rectangle:

Scaling of rectangle:
Shearing of Rectangle:

4.Write a program to implement Cohen Sutherland Line Clipping,


Sutherland Hodgeman polygon clipping.
Cohen Sutherland Line Clipping:

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
using namespace std;

static int LEFT = 1, RIGHT = 2, BOTTOM = 4, TOP = 8, xmin, ymin, xmax, ymax;

int getcode(int x, int y) {


int code = 0;
if (y > ymax) code = TOP;
if (y < ymin) code = BOTTOM;
if (x < xmin) code = LEFT;
if (x > xmax) code = RIGHT;
return code;

int main() {
int gd = DETECT, gm;
initgraph( & gd, & gm, (char * )
"");
setcolor(WHITE);
cout << "Enter window minimum and maximum values";
cin >> xmin >> ymin >> xmax >> ymax;
rectangle(xmin, ymin, xmax, ymax);
int x1, y1, x2, y2;
cout << "Enter the endpoint of the line: ";
cin >> x1 >> y1 >> x2 >> y2;
line(x1, y1, x2, y2);
getch();
int outcode1 = getcode(x1, y1), outcode2 = getcode(x2, y2);
int accept = 0;
while (1) {
float m = (float)(y2 - y1) / (x2 - x1);
if (outcode1 == 0 && outcode2 == 0) {
accept = 1;
break;
} else if ((outcode1 & outcode2) != 0) {
break;
} else {
int x, y;
int temp;
if (outcode1 == 0)
temp = outcode2;
else
temp = outcode1;
if (temp & TOP) {
x = x1 + (ymax - y1) / m;
y = ymax;
} else if (temp & BOTTOM) {
x = x1 + (ymin - y1) / m;
y = ymin;
} else if (temp & LEFT) {
x = xmin;
y = y1 + m * (xmin - x1);
} else if (temp & RIGHT) {
x = xmax;
y = y1 + m * (xmax - x1);
}
if (temp == outcode1) {
x1 = x;
y1 = y;
outcode1 = getcode(x1, y1);
} else {
x2 = x;
y2 = y;
outcode2 = getcode(x2, y2);
}

}
}
cout << "After Clipping";
if (accept)
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
setcolor(RED);
line(x1, y1, x2, y2);
getch();
closegraph();
}

Output

Before Clipping and after Clipping

Sutherland Hodgeman polygon clipping:

#include<bits/stdc++.h>
#include<graphics.h>
#define round(a)((int)(a+0.5))
using namespace std;
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipleft(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void cliptop(float x1, float y1, float x2, float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<= ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipright(float x1, float y1, float x2, float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1<=xmax && x2<= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipbottom(float x1, float y1, float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin&& y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2 <ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
int main()
{
int gd=DETECT,gm,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
cout<<"coordinates of rectanglular clip window :\nxmin,ymin:";
cin>>xmin>>ymin;
cout<<"xmax,ymax:";
cin>>xmax>>ymax;
cout<<"\n\n polygon to be clipped:\nnumber of sildes:\n";
cin>>n;
cout<<"enter the coordinates:\n";
for(i=0;i<2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gd,&gm,"");
setcolor(YELLOW);
rectangle(xmin,ymax,ymax,ymin);
setcolor(WHITE);
outtextxy(50,50,"Unclipped Polygon");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipleft(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
cliptop(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipright(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipbottom(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(YELLOW);
rectangle(xmin,ymax,xmax,ymin);
outtextxy(50,50,"Clipped Polygon");
getch();
closegraph();
}

Output:
5. Write a program to apply composite scaling and rotation to 2D-
shapes.
Composite 2d transformation:
#include<iostream>

#include<graphics.h>

#include<math.h>

int main() {
int gd, gm, n, i, xa[10], ya[10], op, tx, ty, xa1[10], ya1[10], theta,
xf, yf, rx, ry,
sx, sy, shx, shy, xref, yref;
char d;
gd = DETECT;
initgraph( & gd, & gm, "");
printf("enter the no of points");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("enter the coordinates");
scanf("%d%d", & xa[i], & ya[i]);
}
do {
printf("menu");
printf("\n1.rotation\n2.scaling\n3.exit");
scanf("%d", & op);
switch (op) {
case 1:
printf("enter the rotation angle");
scanf("%d", & theta);
theta = (theta * 3.14) / 180;
printf("enter the reference points");
scanf("%d%d", & xf, & yf);
for (i = 0; i < 3; i++) {
xa1[i] = xf + (xa[i] - xf) * cos(theta) - (ya[i] - yf) *
sin(theta);
ya1[i] = yf + (xa[i] - xf) * sin(theta) - (ya[i] - yf) *
cos(theta);
}
cleardevice();
printf("before rotation");
for (i = 0; i < 3; i++) {
line(xa[i], ya[i], xa[(i + 1) % n], ya[(i + 1) % n]);
}
printf("after rotation");
for (i = 0; i < 3; i++) {
line(xa1[i], ya1[i], xa1[(i + 1) % n], ya1[(i + 1) % n]);
}
getch();
cleardevice();
break;
case 2:
printf("enter the scaling factor x and y");
scanf("%d%d", & sx, & sy);
printf("enter the reference point x and y");
scanf("%d%d", & rx, & ry);
for (i = 0; i < 3; i++) {
xa1[i] = xa[i] * sx + rx * (1 - sx);
ya1[i] = ya[i] * sy + ry * (1 - sy);
}
cleardevice();
printf("before scaling");
for (i = 0; i < 3; i++) {
line(xa[i], ya[i], xa[(i + 1) % n], ya[(i + 1) % n]);
}
printf("after scaling");

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


line(xa1[i], ya1[i], xa1[(i + 1) % n], ya1[(i + 1) % n]);
}
getch();
cleardevice();
break;

case 3:
exit(0);
break;
}
} while (op != 3);
}

6. Write a program to translation and rotation to 3D shapes.


3D translation and rotation:
#include <bits/stdc++.h>

#include <graphics.h>

using namespace std;

int main() {
int gd, gm, x, y, z, ang, x1, x2, y1, y2;
detectgraph( & gd, & gm);
initgraph( & gd, & gm, (char * )
"");
setfillstyle(3, 25);

int left = 100, top = 100,


right = 200, bottom = 200,
depth = 20, topflag = 1;

// Orignal 3D object
bar3d(left, top, right, bottom, depth, topflag);

//Enter the Translation vector


x = 100;
y = 50;
bar3d(left + x, top + y, right + x, bottom + y, depth, topflag);

// Enter the Scaling Factor


x = 2;
y = 2;
z = 2;
bar3d(x * left, y * top, x * right, y * bottom, depth * z, topflag);

// Enter the Rotation angle


ang = 45;
x1 = 100 * cos(ang * 3.14 / 180) - 20 * sin(ang * 3.14 / 180);
y1 = 100 * sin(ang * 3.14 / 180) + 20 * sin(ang * 3.14 / 180);
x2 = 60 * cos(ang * 3.14 / 180) - 90 * sin(ang * 3.14 / 180);
y2 = 60 * sin(ang * 3.14 / 180) + 90 * sin(ang * 3.14 / 180);
// After rotating about z-axis;
bar3d(left + x1, top - y1, right + x2, bottom - y2, depth, topflag);
delay(100);
// After rotating about x-axis
bar3d(left + 100, top - x1, right + 60, bottom - x2, depth, topflag);
// After rotating about y-axis
bar3d(left + x1, top, right + x2, bottom, depth, topflag);

getchar();
closegraph();
return 0;
}

Output:
7.Write a program to create empty fill, solid fill, line fill, wide dot
fill, close dot fill and user fill using ellipse attribute.
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

/*
EMPTY FILL 0
SOLID FILL 1
LINE FILL 2
WIDE DOT FILL 10
CLOSE DOR FILL 11
USER FILL 12
*/

int main() {
int gd = DETECT, gm;
initgraph( & gd, & gm, (char * )
"");

int xc = 100; // x radius


int yc = 50; // y radius
int x = 300, y = 300; // center

int FILL_STYLE = 0;

while (true) {
cout << "chose fill style: " << endl;
cout << "\t0.EMPTY_FILL\n\t1.SOLID_FILL\n\t2.LINE_FILL\n\
t10.WIDE_DOT_FILL\n\t11.CLOSE_DOT_FILL\n\t12.USER_FILL\n\t6.EXIT" << endl;
cin >> FILL_STYLE;
if (FILL_STYLE == 6) break;

setfillstyle(FILL_STYLE, 1);
ellipse(x, y, 0, 360, xc, yc);
fillellipse(x, y, xc, yc);

delay(1000);
getch();
cleardevice();
}
closegraph();
}

Output
8. Create a Bouncing Ball using key frame animation and path
animation.
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

int main() {
int gd = DETECT, gm;
int i, x, y, flag = 0;
initgraph(&gd, &gm, (char *)"");

/* get mid positions in x and y-axis */


x = getmaxx() / 2;
y = 30;

while (!kbhit())
{
if (y >= getmaxy() - 30 || y <= 30)
flag = !flag;
/* draws the gray board */
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);

/* delay for 20 milli seconds */


delay(15);

/* clears screen */
cleardevice();
if (flag)
{
y = y + 5;
}
else
{
y = y - 5;
}
}

getch();
closegraph();
return 0;
}

Output
9. Write a Program to show animation of a ball moving in a helical
path.
#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 gd = DETECT, gm, 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(&gd, &gm, "");
/* mid positions at x and y-axis */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* manipulating radius of all 9 planets */
planet[7] = 20;
planet[8]=planet[7]+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[7] = 200, yrad[7] = 20;
xrad[8] = xrad[7] + 30;
yrad[8] = yrad[7] + 15;

/* positions of planets on their corresponding orbits */


planetMotion(xrad[8], yrad[8], midx, midy, x[8], y[8]);
while (!kbhit()) {
/* drawing 9 orbits */
setcolor(WHITE);
for (i = 8; i < 9; i++) {
ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);

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

Output:
10. Write a program to show ticking clock using graphics.
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

void rotate( int figure[], int edges, double angle, int cx, int cy ) {
double x, y;
angle = -1 * (angle*3.14/180);
double cos_a = cos(angle);
double sin_a = sin(angle);
for (int i=0; i < edges; i++) {
x = figure[2*i] - cx;
y = figure[2*i+1] - cy;
figure[2*i] = floor( (x * cos_a) - (y * sin_a) + cx +
0.5 );
figure[2*i+1] = floor( (x * sin_a)+(y * cos_a) + cy +
0.5 );
}
}
void drawClock(int,int,int);

int main() {
int second_hand[4],minute_hand[4], hour_hand[4], edges = 2 ;
double angle;
int cx=300, cy=200;
int gd = DETECT, gm;
initgraph( &gd, &gm, (char*)"");
int max_y = getmaxy();

cleardevice();
angle = -6;
// Set the initial position of the second, minute and the hour
hands.
second_hand[0] = cx ;
second_hand[1] = max_y - cy;
second_hand[2] = cx;
second_hand[3] = max_y - 320;
hour_hand[0] = cx;
hour_hand[1] = max_y - cy;
hour_hand[2] = cx + 90;
hour_hand[3] = max_y - 200;
minute_hand[0] = cx;
minute_hand[1] = max_y - cy;
minute_hand[2] = cx;
minute_hand[3] = max_y - 310;
cleardevice();
setbkcolor(WHITE);
// Draw the clock
drawClock(cx,max_y - cy,150);
setlinestyle(SOLID_FILL,0,1);
// Draw the minute and the hour hand
drawpoly(2,minute_hand);
drawpoly(2,hour_hand);
int i=0;
while(!kbhit()) {
setcolor(RED);
drawpoly(2,second_hand);
setcolor(GREEN);
drawpoly(2,minute_hand);
setcolor(BLUE);
drawpoly(2,hour_hand);
delay(1000);
// set delay(10) to tick the clock fast
setcolor(0);
drawpoly(2,second_hand);
rotate(second_hand,edges,angle,cx,max_y - cy);
i++;
// Reset the second hand and move the minute hand
// when the second hand has moved 60 times.
if(i%60 == 0) {
second_hand[0] = cx ;
second_hand[1] = max_y - cy;
second_hand[2] = cx;
second_hand[3] = max_y - 320;
drawpoly(2,minute_hand);
rotate(minute_hand,edges,angle,cx,max_y - cy);
}
// Move the minute hand
// when the second hand has moved 720 (60*12) times.
if(i%720 == 0) {
i = 0;
drawpoly(2,hour_hand);
rotate(hour_hand,edges,angle,cx,max_y - cy);
}
}
getch();
}
// Function to draw the clock
void drawClock(int cx, int cy, int r) {
setcolor(WHITE);
setlinestyle(SOLID_FILL,0,3);
circle(cx,cy,r);
int max_y = getmaxy();
int center[2] = {
cx, max_y - 340
}
;
for (int i=0; i<60; i++) {
if(i%5 == 0) {
circle(center[0],center[1],2);
} else {
circle(center[0],center[1],1);
}
rotate(center,1,-6,cx,cy);
}
}

Output:
11. Create and rotate a triangle about the origin and a fixed point.
#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=0; // don't rotate initially
float rotate_angle=0; // initial angle
float translate_x=0,translate_y=0; // initial translation
void draw_pixel(float x1, float y1)
{
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(x1,y1); // plot a single point
glEnd();
}
void triangle(int x, int y)
{
glColor3f(1,0,0);
glBegin(GL_POLYGON); // drawing a Triangle
glVertex2f(x,y);
glVertex2f(x+400,y+300);
glVertex2f(x+300,y+0);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,1,1); // mark origin point as white dot
draw_pixel(0,0); // plot origin - white colour
if (where_to_rotate == 1) // rotate around origin
{
translate_x = 0; // no translation for rotation around origin
translate_y = 0;
rotate_angle += 1; // the amount of rotation angle
}
if (where_to_rotate == 2) // rotate around Fixed Point
{
translate_x = x; // SET the translation to wherever the customer says
translate_y = y;
rotate_angle += 1; // the amount of rotation angle
glColor3f(0,0,1); // mark the customer coordinate as blue dot
draw_pixel(x,y); // plot the customer coordinate - blue colour
}
glTranslatef(translate_x, translate_y, 0); // ACTUAL translation +ve
glRotatef(rotate_angle, 0, 0, 1); // rotate
glTranslatef(-translate_x, -translate_y, 0); // ACTUAL translation -ve
triangle(translate_x,translate_y); // what to rotate? – TRIANGLE boss
glutPostRedisplay(); // call display function again and again
glutSwapBuffers(); // show the output
}
void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}
void rotateMenu (int option)
{
if(option==1)
where_to_rotate=1; // rotate around origin
if(option==2)
where_to_rotate=2; // rotate around customer's coordinates
if(option==3)
where_to_rotate=3; // stop rotation
}
int main(int argc, char **argv)
{
printf( "Enter Fixed Points (x,y) for Rotation: \n");
scanf("%d %d", &x, &y); // getting the user's coordinates to rotate
glutInit(&argc, argv); // initialize the graphics system
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // SINGLE also works
glutInitWindowSize(800, 800); // 800 by 800 size..you can change it
glutInitWindowPosition(0, 0); // where do you wanna see your window
glutCreateWindow("Create and Rotate Triangle"); // title
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutCreateMenu(rotateMenu); // menu items
glutAddMenuEntry("Rotate around ORIGIN",1);
glutAddMenuEntry("Rotate around FIXED POINT",2);

glutAddMenuEntry("Stop Rotation",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop(); // run forever
}
Output:

Rotate around Origin Rotate Around Fixed point


12.Draw a color cube and spin it using OpenGL transformation
matrices.
#include<stdlib.h>
#include<GL/glut.h>
GLfloat vertices[] = { -1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1
};
GLfloat colors[] = { 0, 0, 0, // white color
1, 0, 0, // red color .. so on for eight faces of cube
1, 1, 0,
0, 1, 0,
0, 0, 1,
1, 0, 1,
1, 1, 1,
0, 1, 1
};
GLubyte cubeIndices[] = {0, 3, 2, 1,
2, 3, 7, 6,
0, 4, 7, 3,
1, 2, 6, 5,
4, 5, 6, 7,
0, 1, 5, 4
};
static GLfloat theta[]= {0, 0, 0}; // initial angles
static GLint axis=2; // let us assume the right mouse button has been
clicked initially
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef (theta[0], 1, 0, 0); // first angle rotation via x axis
glRotatef (theta[1], 0, 1, 0); // second angle rotation via y axis
glRotatef (theta[2], 0, 0, 1); // third angle rotation via z axis
glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,cubeIndices); // draw the
cube
glutSwapBuffers(); // show the output
}
void spinCube()
{
theta[axis] += 2; // rotate every 2 degrees
if (theta[axis] > 360) // it the rotation angle crosses 360 degrees, make
it 0 degree
theta[axis] -= 360;
glutPostRedisplay(); // call display again
}
void mouse(int btn, int state, int x, int y)
{
if (btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis=0; // x axis rotation
if (btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis=1; // y axis rotation
if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
axis=2; // z axis rotation
}
void myReshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho (-2, 2, -2*(GLfloat)h/(GLfloat)w, 2*(GLfloat)h / (GLfloat)w, -10,
10);
else
glOrtho (-2*(GLfloat)w/(GLfloat)h, 2*(GLfloat)w / (GLfloat)h, -2, 2, -10,
10);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Spin a color cube");
glutReshapeFunc(myReshape); // calls myReshape whenever we change the
window size
glutDisplayFunc(display); // call display function
glutIdleFunc(spinCube); // whenever we are idle, calls spinCube function

glutMouseFunc(mouse); // calls mouse function whenever we interact with


mouse
glEnable(GL_DEPTH_TEST); // enables depth – for 3D
glEnableClientState(GL_COLOR_ARRAY); // enables colour and vertex
properties
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices); //
glVertexPointer(size,type,stride,pointer)
glColorPointer(3, GL_FLOAT, 0, colors); //
glColorPointer(size,type,stride,pointer)
glColor3f(1, 1, 1);
glutMainLoop();
}

Output:
13. Develop Programs for making Simple animation using
transformations like rotation scaling and translation.
Circle Moving from left to right and vice versa
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

int main() {
int gd = DETECT, gm;
int i, x, y, flag = 0;
initgraph(&gd, &gm, (char *)"");

/* get mid positions in x and y-axis */


y = 200;
x = getmaxx() /5;

while (!kbhit())
{
if (x >= getmaxy() - 100 || x <= 100)
flag = !flag;
/* draws the gray board */
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);

/* delay for 20 milli seconds */


delay(100);

/* clears screen */
cleardevice();
if (flag)
{
x = x + 5;
}
else
{
x = x - 5;
}
}

getch();
closegraph();
return 0;
}

Output:
Man object moving:
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
int main()
{
int gd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht =
0;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
setcolor(BROWN);
line(0, 201, 600, 201);
cont:
while (!kbhit()) {
setcolor(4);
line(x, 100, x, 150);
line(x+20, 100, x+20, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
setcolor(0);
delay(50);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
line(x + 50, 100, x + 50, 200);
x++;
l--;
if (l == -15)
l = 15;
if (ht == 1)
h++;
else
h--;
if (h == 15)
ht = 0;
else if (h == -15)
ht = 1;
}
if (getch() == ' ') {
while (!kbhit());
getch();
goto cont;
}
}

output
Man Walking:
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
int main()
{
int gd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht =
0;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
setcolor(BROWN);
line(0, 201, 600, 201);
cont:
while (!kbhit()) {
setcolor(4);

circle(x - 20, 115, 15);


line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
/* hands*/
line(x - 20, 140, x - 20 + h, 160);
line(x - 20, 140, x - 20 - h, 160);
setcolor(0);
delay(50);

circle(x - 20, 115, 15);


line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);

line(x - 20, 140, x - 20 + h, 160);


line(x - 20, 140, x - 20 - h, 160);
line(x + 50, 100, x + 50, 200);
x++;
l--;
if (l == -15)
l = 15;
if (ht == 1)
h++;
else
h--;
if (h == 15)
ht = 0;
else if (h == -15)
ht = 1;
}
if (getch() == ' ') {
while (!kbhit());
getch();
goto cont;
}
}

Output:

Wind mill Rotation


#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wind(float x[7],float y[7]);
int main()
{
int gd=DETECT,gm;
float x[7],y[7],maxx,maxy,xw1,yw1,xw2,yw2;
float theta=30;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
maxx=getmaxx();
maxy=getmaxy();
x[0]=maxx/2;
y[0]=maxy/2;
x[1]=y[4]=x[2]=-90;
y[6]=y[5]=y[1]=60;
y[2]=35;
y[3]=-100;
x[4]=20;
x[3]=0;
x[5]=90;
x[6]=65;
theta=theta*22/7/180;
while(kbhit()==0)
{
wind(x,y);
xw1=cos(theta)*x[1]+sin(theta)*y[1];
yw1=-sin(theta)*x[1]+cos(theta)*y[1];
xw2=cos(theta)*x[2]+sin(theta)*y[2];
yw2=-sin(theta)*x[2]+cos(theta)*y[2];
x[1]=xw1;
y[1]=yw1;
x[2]=xw2;
y[2]=yw2;
xw1=cos(theta)*x[3]+sin(theta)*y[3];
yw1=-sin(theta)*x[3]+cos(theta)*y[3];
xw2=cos(theta)*x[4]+sin(theta)*y[4];
yw2=-sin(theta)*x[4]+cos(theta)*y[4];
x[3]=xw1;
y[3]=yw1;
x[4]=xw2;
y[4]=yw2;
xw1=cos(theta)*x[5]+sin(theta)*y[5];
yw1=-sin(theta)*x[5]+cos(theta)*y[5];
xw2=cos(theta)*x[6]+sin(theta)*y[6];
yw2=-sin(theta)*x[6]+cos(theta)*y[6];
x[5]=xw1;
y[5]=yw1;
x[6]=xw2;
y[6]=yw2;
delay(50);
cleardevice();
}
closegraph();
}
void wind(float x[7],float y[7])
{
cleardevice();
line(x[0],y[0],x[0]-50,y[0]+200);
line(x[0],y[0],x[0]+50,y[0]+200);
line(x[0]-60,y[0]+200,x[0]+60,y[0]+200);
line(x[0],y[0],x[0]+x[1],y[0]-y[1]);
line(x[0],y[0],x[0]+x[2],y[0]-y[2]);
line(x[0]+x[1],y[0]-y[1],x[0]+x[2],y[0]-y[2]);
line(x[0],y[0],x[0]+x[3],y[0]-y[3]);
line(x[0],y[0],x[0]+x[4],y[0]-y[4]);
line(x[0]+x[3],y[0]-y[3],x[0]+x[4],y[0]-y[4]);
line(x[0],y[0],x[0]+x[5],y[0]-y[5]);
line(x[0],y[0],x[0]+x[6],y[0]-y[6]);
line(x[0]+x[5],y[0]-y[5],x[0]+x[6],y[0]-y[6]);
}

Output:
Simple animation of a football goal:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int x=0,gd=DETECT,gm,points[]={0,220,1600,220,1600,900,0,900,0,220};
float y=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(MAGENTA);
settextstyle(6,HORIZ_DIR,6);
outtextxy(200,250,"Hi");
delay(1000);
cleardevice();
settextstyle(7,VERT_DIR,1);
outtextxy(200,50,"GET READY FOR ANIMATION");
delay(1000);
cleardevice();
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(5,points);
setcolor(WHITE);
circle(100,100,25);
delay(1000);
line(100,125,100,185);
delay(1000);
line(100,135,125,170);
delay(1000);
line(100,135,75,170);
delay(1000);
line(100,185,125,220);
delay(1000);
line(100,185,75,220);
delay(1000);
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,210-y,10,10);
for(x=0;x<50;x++)
{
setcolor(WHITE);
line(100,185,75+x,220-y);
delay(100);
setcolor(BLACK);
line(100,185,75+x,220-y);
y=y+0.25;
}
setcolor(WHITE);
line(100,185,125,220);
line(100,185,75,220);
for(x=0,y=0;y<100;x++)
{
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,210-y,10,10);
delay(10);
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(5,points);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(135+x,210-y,10,10);
y=y+0.5;
}
for(;x<490;x++)
{
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,y,10,10);
delay(10);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(135+x,y,10,10);
y=y+0.25;
}
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,y,10,10);
delay(2000);
cleardevice();
setbkcolor(CYAN);
settextstyle(3,HORIZ_DIR,10);
outtextxy(200,80,"GOAL");
getch();
closegraph();
}

Output:

You might also like