0% found this document useful (0 votes)
79 views28 pages

ABC

The document contains code snippets demonstrating different computer graphics algorithms: 1. The first code snippet implements DDA line drawing algorithm to draw a line between two points by calculating the slope and incrementing x and y coordinates in a loop. 2. The second snippet uses Breshenham's line drawing algorithm to draw a line by calculating the decision parameter and incrementing x or y based on the parameter value. 3. The third snippet draws a circle using Breshenham's circle drawing algorithm by incrementing x and y in an octant fashion until they become equal. 4. The fourth snippet draws a circle using midpoint circle algorithm by calculating the decision parameter and incrementing/decrementing x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views28 pages

ABC

The document contains code snippets demonstrating different computer graphics algorithms: 1. The first code snippet implements DDA line drawing algorithm to draw a line between two points by calculating the slope and incrementing x and y coordinates in a loop. 2. The second snippet uses Breshenham's line drawing algorithm to draw a line by calculating the decision parameter and incrementing x or y based on the parameter value. 3. The third snippet draws a circle using Breshenham's circle drawing algorithm by incrementing x and y in an octant fashion until they become equal. 4. The fourth snippet draws a circle using midpoint circle algorithm by calculating the decision parameter and incrementing/decrementing x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 28

............................................DDA....................................

.......
#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;
//clrscr();
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=1;i<=step;i++){
putpixel(x1,y1,LIGHTBLUE);
delay(100);
x1=x1+xn;
y1=y1+yn;

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

.............................................breshenham_line_drawing...............
.........................
#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;
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\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;
}

............................................................breshenham_circle_drawi
ng...............................................

#include <stdio.h>
#include <dos.h>
#include <graphics.h>

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

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

..............................................midpoint
algo.........................................................

#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)
{
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;
}

........................................................2D_TRANSFORMATIONS.........
.....................................
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

int main(){
int gm;
int gd = DETECT;
int x1, x2, x3, y1, y2, y3, nx1, nx2, nx3, ny1, ny2, ny3, c;
int sx, sy, xt, yt, r;
float t;
initgraph(&gd, &gm, (char*)"");

setcolor(3);

// set three point of triangle


x1 = 200;
y1 = 200;
x2 = 400;
y2 = 400;
x3 = 400;
y3 = 50;

line(x1, y1, x2, y2);


line(x2, y2, x3, y3);
line(x3, y3, x1, y1);

// set the translation factor


xt = 1;
yt = 40;

nx1 = x1 + xt;
ny1 = y1 + yt;
nx2 = x2 + xt;
ny2 = y2 + yt;
nx3 = x3 + xt;
ny3 = y3 + yt;

line(nx1, ny1, nx2, ny2);


line(nx2, ny2, nx3, ny3);
line(nx3, ny3, nx1, ny1);
// set the angle of rotation
r = 45;
t = 3.14 * r / 180;
nx1 = abs(x1 * cos(t) - y1 * sin(t));
ny1 = abs(x1 * sin(t) + y1 * cos(t));
nx2 = abs(x2 * cos(t) - y2 * sin(t));
ny2 = abs(x2 * sin(t) + y2 * cos(t));
nx3 = abs(x3 * cos(t) - y3 * sin(t));
ny3 = abs(x3 * sin(t) + y3 * cos(t));
line(nx1, ny1, nx2, ny2);
line(nx2, ny2, nx3, ny3);
line(nx3, ny3, nx1, ny1);

// set the scalling factor


sx = 100;
sy = 100;
nx1 = x1 * sx;
ny1 = y2 * sy;
nx2 = x2 * sx;
ny2 = y2 * sy;
nx3 = x3 * sx;
ny3 = y3 * sy;
line(nx1, ny1, nx2, ny2);
line(nx2, ny2, nx3, ny3);
line(nx3, ny3, nx1, ny1);
getchar();
closegraph();
}

..............................................................3D_TRANSFORMATIONS...
..............................................

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

.............................................................sutherland_line_clipin
g...............................................

#include <iostream>
using namespace std;

const int INSIDE = 0; // 0000


const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000

const int x_max = 10;


const int y_max = 8;
const int x_min = 4;
const int y_min = 4;

// Function to compute region code for a point(x, y)


int computeCode(double x, double y)
{
// initialized as being inside
int code = INSIDE;

if (x < x_min) // to the left of rectangle


code |= LEFT;
else if (x > x_max) // to the right of rectangle
code |= RIGHT;
if (y < y_min) // below the rectangle
code |= BOTTOM;
else if (y > y_max) // above the rectangle
code |= TOP;

return code;
}

// Implementing Cohen-Sutherland algorithm


// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)
void cohenSutherlandClip(double x1, double y1,
double x2, double y2)
{
// Compute region codes for P1, P2
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);

// Initialize line as outside the rectangular window


bool accept = false;

while (true) {
if ((code1 == 0) && (code2 == 0)) {
// If both endpoints lie within rectangle
accept = true;
break;
}
else if (code1 & code2) {
// If both endpoints are outside rectangle,
// in same region
break;
}
else {
// Some segment of line lies within the
// rectangle
int code_out;
double x, y;

// At least one endpoint is outside the


// rectangle, pick it.
if (code1 != 0)
code_out = code1;
else
code_out = code2;

// Find intersection point;


// using formulas y = y1 + slope * (x - x1),
// x = x1 + (1 / slope) * (y - y1)
if (code_out & TOP) {
// point is above the clip rectangle
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM) {
// point is below the rectangle
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT) {
// point is to the right of rectangle
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT) {
// point is to the left of rectangle
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}

// Now intersection point x, y is found


// We replace point outside rectangle
// by intersection point
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
cout << "Line accepted from " << x1 << ", "
<< y1 << " to " << x2 << ", " << y2 << endl;

}
else
cout << "Line rejected" << endl;
}

// Driver code
int main()
{
// First Line segment
// P11 = (5, 5), P12 = (7, 7)
cohenSutherlandClip(5, 5, 7, 7);

// Second Line segment


// P21 = (7, 9), P22 = (11, 4)
cohenSutherlandClip(7, 9, 11, 4);

// Third Line segment


// P31 = (1, 5), P32 = (4, 1)
cohenSutherlandClip(1, 5, 4, 1);

return 0;
}

.............................................SUTHERLAND_POLYGON_CLIPPING...........
.....................................

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

int xmin, xmax, ymin, ymax;

class Coordinate{
public:
int X;
int Y;
Coordinate() {}
Coordinate(float x, float y) {
X = x;
Y = y;
}
};

Coordinate pointOfIntersection(Coordinate s1, Coordinate e1, Coordinate s2,


Coordinate e2)
{
float a1 = e1.Y - s1.Y;
float b1 = s1.X - e1.X;
float c1 = a1 * s1.X + b1 * s1.Y;

float a2 = e2.Y - s2.Y;


float b2 = s2.X - e2.X;
float c2 = a2 * s2.X + b2 * s2.Y;

float delta = a1 * b2 - a2 * b1;

return Coordinate((b2 * c1 - b1 * c2) / delta, (a1 * c2 - a2 * c1) / delta);


}

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

int i, j, choice=1;
cout<<"Enter co-ordinates for window ABCD:\n\n";
cout<<"xmin, xmax, ymin, ymax: ";
cin>>xmin>>xmax>>ymin>>ymax;
rectangle(xmin, ymin, xmax, ymax);

do{
int n;
cout<<"\n\nEnter number of vertices (max 10): ";
cin>>n;
Coordinate V[n];
cout<<"Enter co-ordinates of vertices\n";
int drawPolygon[n*2], newPolygon[100];
for(i=0, j=0; i<n; i++) {
cout<<"Enter x"<<i+1<<" and y"<<i+1<<": ";
cin>>V[i].X>>V[i].Y;
drawPolygon[j++] = V[i].X;
drawPolygon[j++] = V[i].Y;
}
drawPolygon[j++] = V[0].X;
drawPolygon[j] = V[0].Y;
setcolor(MAGENTA);
drawpoly(n+1, drawPolygon);

Coordinate top_left(xmin, ymin), top_right(xmax, ymin), bot_left(xmin, ymax),


bot_right(xmax, ymax);
int newN;

cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].X<xmin) {
if(V[(i+1)%n].X<xmin)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, bot_left);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].X<xmin) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, bot_left);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];

setcolor(RED);
cout<<"\nRed coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);

for(i=0, j=0; i<newN; i++) {


V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;

cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].X>xmax) {
if(V[(i+1)%n].X>xmax)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_right, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].X>xmax) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_right, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}
newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];

setcolor(GREEN);
cout<<"\nGreen coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);

for(i=0, j=0; i<newN; i++) {


V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;

cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].Y>ymax) {
if(V[(i+1)%n].Y>ymax)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], bot_left, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].Y>ymax) {
temp = pointOfIntersection(V[i], V[(i+1)%n], bot_left, bot_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}

newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];

setcolor(BLUE);
cout<<"\nBlue coordinates: ";
for(i=0; i<=j; i++)
cout<<newPolygon[i]<<" ";
drawpoly(newN+1, newPolygon);

for(i=0, j=0; i<newN; i++) {


V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;

cout<<endl;
for(i=0, j=0, newN=0; i<n; i++) {
Coordinate temp;
if(V[i].Y<ymin) {
if(V[(i+1)%n].Y<ymin)
cout<<"\nOut->Out";
else {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, top_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nOut->In";
}
}
else {
if(V[(i+1)%n].Y<ymin) {
temp = pointOfIntersection(V[i], V[(i+1)%n], top_left, top_right);
newN++;
newPolygon[j++] = temp.X;
newPolygon[j++] = temp.Y;
cout<<"\nIn->Out";
}
else {
newN++;
newPolygon[j++] = V[(i+1)%n].X;
newPolygon[j++] = V[(i+1)%n].Y;
cout<<"\nIn->In";
}
}
}

newPolygon[j++] = newPolygon[0];
newPolygon[j] = newPolygon[1];

for(i=0, j=0; i<newN; i++) {


V[i].X = newPolygon[j++];
V[i].Y = newPolygon[j++];
}
n = newN;

setcolor(YELLOW);
drawpoly(newN+1, newPolygon);

cout<<"\n\nContinue?\t1.Yes\t0.No:\t";
cin>>choice;
}while(choice!=0);
getch();
closegraph();
}

.......................................................2d_composite................
.............................................

#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<3;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");
scanf("%d%d",&sx,&sy);
printf("enter the reference point");
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);
}

.......................................................types of
fill.....................................................

#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\t13.EXIT" << endl;
cin >> FILL_STYLE;
if(FILL_STYLE == 13) break;

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

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

......................................................3d
shapes.......................................................

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

...................................................bouncing
ball................................................................

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

.........................................................ticking
clock..............................................................

#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(GREEN);
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);
}
}

..............................................rotate a
triangle.......................................................
#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
}

................................................color
cube...........................................................................

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

...........................................Circle moving from left to right and


vice versa..............................................

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
//for moving circle from left to right,the following loop works
for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50+i,50,50);
floodfill(52+i,52,3);
delay(20);
cleardevice();
}
//for moving circle from right to left, the following loop works
for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,50,50);
floodfill(i+2,52,3);
delay(20);
cleardevice();
}
//for moving circle from top to bottom,the following loop works
for(i=50;i<=getmaxy();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle from bottom to top,the following loop works
for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in diagonal direction,the following loop works
for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in reverse diagonal direction,the following loop works
for(i=getmaxx();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
getch();
}

................................................program for man object


moving:...........................................

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
//creation of man object by using circle,line.
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(50,50,30); // drawing head
floodfill(52,52,7);
setcolor(13);
line(50,80,50,200); //drawing body
line(50,110,20,140); //left hand
line(50,110,80,140); //right hand
line(50,200,20,230); //left leg
line(50,200,80,230); //right leg
// for loop for moving man
for(i=50;i<=getmaxx();i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing body
line(i,110,i-30,140); //left hand
line(i,110,i+30,140); //right hand
line(i,200,i-30,230); //left leg
line(i,200,i+30,230); //right leg
cleardevice();
delay(10);
}
//doing simple animation using translation
for(i=50;i<=getmaxx()/2;i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing body
line(i,110,i-30,140); //left hand
line(i,110,i+30,140); //right hand
line(i,200,i-30,230); //left leg
line(i,200,i+30,230); //right leg
cleardevice(); delay(10);
}
getch();
}

....................................................program for windmill


rotation:...................................................

#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wind(float x[7],float y[7]);
void 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]);
}

...................................................program for man


walking:...............................................

#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void 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);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
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);
setcolor(0);
delay(50);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
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;
}
}

..........................................program for simple animation of football


goal............................................

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void 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();
}

You might also like