0% found this document useful (0 votes)
9 views78 pages

CG File

Uploaded by

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

CG File

Uploaded by

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

INDEX

S.No. Topic Page


No.

1. Write a program to implement Bresenham’s line drawing


algorithm.
3-8

2. Write a program to implement mid-point circle drawing


algorithm.
9-13

3. Write a program to clip a line using Cohen and 14-22


Sutherland line clipping algorithm.

4. Write a program to clip a polygon using Sutherland 23-29


Hodgeman algorithm.

5. Write a program to apply various 2D transformations on 30-42


a 2D object (use homogenous Coordinates).

6. Write a program to apply various 3D transformations on


a 3D object and then apply parallel and perspective
43-64
projection on it.

7. Write a program to draw Hermite /Bezier curve. 65-69

8. Dda line algorithm. 70-75

9. Mid point line algorithm. 76-79

1
1. Write a program to implement Bresenham’s line drawing algorithm.

Program:

#include<iostream>

#include<graphics.h>

#include<math.h>

using namespace std;

float x_mid, y_mid;

int lineBres(int xa, int ya, int xb, int yb)

int dx = abs(xa-xb);

int dy = abs(ya-yb);

int p = 2*dy-dx;

int two_Dy = 2*dy, two_DyDx= 2*(dy-dx);

int x,y,xEnd;

if(xa>xb)

x = xb;

y = yb;

xEnd = xa;

2
}

else

x = xa;

y = ya;

xEnd = xb;

putpixel(round(x)+x_mid, y_mid-round(y), YELLOW);

while(x<xEnd)

x++;

if(p<0)

p += two_Dy;

else

y++;

p += two_DyDx;

putpixel(round(x)+x_mid, y_mid-round(y), YELLOW);

return 0;

3
int main()

//DETECT is a macro defined in "graphics.h" header file

int gd = DETECT, gm;

initgraph(&gd,&gm,""); //initgraph initializes the graphics system by


loading a graphics driver from disk.

//getmaxx() and getmaxy() returns the maximum (screen-relative) x and y


value for the current graphics driver and mode

float X = getmaxx(), Y = getmaxy();

x_mid = X/2;

y_mid = Y/2;

int x1, y1, x2, y2;

cout<<"\n Enter coordinate x1: ";

cin>>x1;

cout<<"\n Enter coordinate x2: ";

cin>>x2;

cout<<"\n Enter coordinate y1: ";

cin>>y1;

cout<<"\n Enter coordinate y2: ";

cin>>y2;

cout<<"\n Graph using Bresenhem algorithm: "<<endl;


4
//line(x1,y1.x2,y2) draws a line from x1,y1,x2,y2

line(x_mid, 0, x_mid, Y);

line(0, y_mid, X, y_mid);

//displays a text string in the viewport at the given distance (x,y) using the
current justification settings and the current font, direction and size

outtextxy(x_mid-40, y_mid+10, "(0,0)");

outtextxy(x_mid-60, y_mid-230, "Y-axis");

outtextxy(x_mid-70, y_mid+220, "Y-axis");

outtextxy(x_mid-305, y_mid+10, "X-axis");

outtextxy(x_mid+260, y_mid+10, "X-axis");

setcolor(GREEN);

lineBres(x1, y1, x2, y2);

getch();

//deallocates all the memory allocated by the graphics system, then restores
the screen to the mode it was in before you called initgraph.

closegraph();

return 0;

5
Output:

6
2. Write a program to implement mid-point circle drawing algorithm.

7
Program:

#include<iostream>

#include<graphics.h>

#include<math.h>

using namespace std;

float x_mid, y_mid;

// Function to plot points of circle in symmetry.

void circlePlotPoints(int xCenter, int yCenter, int x, int y)

putpixel(xCenter + x + x_mid, y_mid - yCenter + y, YELLOW);

putpixel(xCenter - x + x_mid, y_mid - yCenter + y, YELLOW);

putpixel(xCenter + x + x_mid, y_mid - yCenter - y, YELLOW);

putpixel(xCenter - x + x_mid, y_mid - yCenter - y, YELLOW);

putpixel(xCenter + y + x_mid, y_mid - yCenter + x, YELLOW);

putpixel(xCenter - y + x_mid, y_mid - yCenter + x, YELLOW);

putpixel(xCenter + y + x_mid, y_mid - yCenter - x, YELLOW);

putpixel(xCenter - y + x_mid, y_mid - yCenter - x, YELLOW);

// Function to implement Mid-point circle drawing algorithm.


8
void circleMidpoint(int xCenter, int yCenter, int radius)

int x = 0;

int y = radius;

int p = 1 - radius;

// Plotting first set of points.

circlePlotPoints(xCenter, yCenter, x, y);

while (x < y)

x++;

if (p < 0)

p += 2 * x + 1;

else

y--;

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

circlePlotPoints(xCenter, yCenter, x, y);

// Driver Code

9
int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)"");

float X = getmaxx(), Y = getmaxy();

x_mid = X / 2;

y_mid = Y / 2;

int r;

int xc, yc;

cout << "\n Enter coordinate x-center: ";

cin >> xc;

cout << "\n Enter coordinate y-center: ";

cin >> yc;

cout << "\n Enter radius: ";

cin >> r;

cout << "\n Circle using Mid-point Circle Algorithm:" << endl;

line(x_mid, 0, x_mid, Y);

line(0, y_mid, X, y_mid);

outtextxy(x_mid - 40, y_mid + 10, "(0,0)");

setcolor(GREEN);

10
outtextxy(x_mid - 60, y_mid - 230, "Y - axis");

outtextxy(x_mid - 70, y_mid + 220, "- Y - axis");

outtextxy(x_mid - 305, y_mid + 10, "- X - axis");

outtextxy(x_mid + 260, y_mid + 10, "X - axis");

circleMidpoint(xc, yc, r);

getch();

closegraph();

return 0;

Output:

11
12
3. Write a program to clip a line using Cohen and Sutherland line
clipping algorithm.

Program:

//clip a line using cohen and sutherland algo

#include <iostream>

#include<graphics.h>

#include<math.h>

using namespace std;

float x_mid, y_mid;

// Defining region codes

const int TOP = 1; // 0001

const int BOTTOM = 2; // 0010

const int RIGHT = 4; // 0100

const int LEFT = 8; // 1000

// Defining x_max, y_max and x_min, y_min for clipping rectangle.

const int x_max = 300;

const int y_max = 300;

const int x_min = 80;

const int y_min = 80;

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

int ComputeOutCode(double x, double y)

// Point initialized as being inside the clipping window.

int code = 0;

if (y > y_max)

code |= TOP;

else if (y < y_min)

code |= BOTTOM;

if (x > x_max)

code |= RIGHT;

else if (x < x_min)

code |= LEFT;

return code;

// Implementing Cohen-Sutherland algorithm.

void CohenSutherlandLineClipAndDraw(double x1, double y1, double x2, double


y2)

14
// Initialize line as outside the clipping window.

bool accept = false, done = false;

// Compute region codes for P1, P2.

int code1 = ComputeOutCode(x1, y1);

int code2 = ComputeOutCode(x2, y2);

do

if (!(code1 | code2))

// Trivial accept and exit.

accept = true;

done = true;

break;

else if (code1 & code2)

// If both endpoints are outside clipping window, so trivial reject.

break;

else

15
/* Failed both tests, so calculate the line segment to clip:

from an outside point to an intersection with clip edge.

*/

double x, y;

int code_out;

// At least one endpoint is outside the clip rectangle, pick it.

code_out =(code1 != 0)? code1 : code2;

// Now, find intersection point.

// Using formulas: y = y1 + slope * (x - x1), x = x1 + (1 / slope) * (y - y1).

if (code_out & TOP)

// Point is above the clipping window.

x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);

y = y_max;

else if (code_out & BOTTOM)

// Point is below the clipping window.

x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);

y = y_min;

16
else if (code_out & RIGHT)

// Point is to the right of clipping window.

y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);

x = x_max;

else if (code_out & LEFT)

// Point is to the left of clipping window.

y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);

x = x_min;

// Now we move outside point to intersection point to clip.

if (code_out == code1)

x1 = x;

y1 = y;

code1 = ComputeOutCode(x1, y1);

else

x2 = x;

17
y2 = y;

code2 = ComputeOutCode(x2, y2);

} while(done == false);

if (accept)

// Drawing the clipped line.

cout << "\n Line accepted from (" << x1 << ", " << y1 << ") to (" <<
x2 << ", " << y2 << ")" << endl;

setcolor(YELLOW);

line(x1, y1, x2, y2);

else

cout << "\n Line rejected" << endl;

// Driver code

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)"");

18
float X = getmaxx(), Y = getmaxy();

float x_mid = X / 2;

float y_mid = Y / 2;

setcolor(GREEN);

outtextxy(30, 30, "Cohen-Sutherland Line Clipping Algorithm:");

// Drawing Window using Lines

setcolor(RED);

line(x_min, y_min, x_max, y_min);

line(x_max, y_min, x_max, y_max);

line(x_max, y_max, x_min, y_max);

line(x_min, y_max, x_min, y_min);

setcolor(BLUE);

cout << "\n ----- LINE 1 -----";

cout << "\n P1 = (260, 320), P2 = (320, 200)";

setcolor(WHITE);

outtextxy(310, 250, "LINE 1");

setcolor(BLUE);

line(260, 320, 320, 200);

CohenSutherlandLineClipAndDraw(260, 320, 320, 200);

cout << "\n ----- LINE 2 -----";

cout << "\n P1 = (100, 100), P2 = (150, 150)";

setcolor(WHITE);

19
outtextxy(135, 110, "LINE 2");

setcolor(BLUE);

line(100, 100, 150, 150);

CohenSutherlandLineClipAndDraw(100, 100, 150, 150);

cout << "\n ----- LINE 3 -----";

cout << "\n P1 = (200, 200), P2 = (200, 240)";

setcolor(WHITE);

outtextxy(150, 230, "LINE 3");

setcolor(BLUE);

line(200, 200, 200, 400);

CohenSutherlandLineClipAndDraw(200, 200, 200, 400);

cout << "\n ----- LINE 4 -----";

cout << "\n P1 = (350, 150), P2 = (450, 250)";

setcolor(WHITE);

outtextxy(410, 180, "LINE 4");

setcolor(BLUE);

line(350, 150, 450, 250);

CohenSutherlandLineClipAndDraw(350, 150, 450, 250);

getch();

closegraph();

return 0;

20
Output:

21
4. Write a program to clip a polygon using Sutherland Hodgeman
algorithm.

Program:

//clip a polygon using sutherland hodgeman

#include<iostream>

#include<graphics.h>

using namespace std;

struct vertex

float x;

float y;

};

vertex sp[40], cw[20];

int n_sp, n_cw;

void draw_poly(vertex vlist[], int n)

for(int i=0; i<n; i++)

line(vlist[i].x, vlist[i].y, vlist[(i+1)%n].x, vlist[(i+1)%n].y);

22
int in_out(float x, float y, int x1, int y1, int x2, int y2)

float p = (y-y1)*(x2-x1) - (x-x1)*(y2-y1);

if (p<0)

return 0; //for out

return 1; //for in

void intersection_line(float &x, float &y, float x1, float y1, float x2, float y2, int
xa, int ya, int xb, int yb)

if (x2==x1)

float m2 = (yb-ya) / (xb-xa);

x = x1;

y = ya - m2*(xa-x1);

else if (xb==xa)

float m1 = (y2-y1) / (x2-x1);

x = xa;

23
y = y1 + m1*(xa-x1);

else

float m1 = (y2-y1) / (x2-x1);

float m2 = (yb-ya) / (xb-xa);

x = (ya-y1 + m1*x1 - m2*xa) / (m1-m2);

y = (m1*m2*(xa-x1) + m2*y1 - m1*ya) / (m2-m1);

void edge_clip(int cw_x1, int cw_y1, int cw_x2, int cw_y2)

vertex temp[40];

int j=-1;

int p1, p2;

for(int i=0; i<n_sp; i++)

p1 = in_out(sp[i].x, sp[i].y, cw_x1, cw_y1, cw_x2, cw_y2);

p2 = in_out(sp[(i+1)%n_sp].x, sp[(i+1)%n_sp].y, cw_x1, cw_y1, cw_x2,


cw_y2);

if (p1==1 && p2==0)

24
j++;

intersection_line(temp[j].x, temp[j].y, sp[i].x, sp[i].y, sp[(i+1)%n_sp].x,


sp[(i+1)%n_sp].y, cw_x1, cw_y1, cw_x2, cw_y2);

else if (p1==0 && p2==1)

j++;

intersection_line(temp[j].x, temp[j].y, sp[i].x, sp[i].y, sp[(i+1)%n_sp].x,


sp[(i+1)%n_sp].y, cw_x1, cw_y1, cw_x2, cw_y2);

j++;

temp[j].x = sp[(i+1)%n_sp].x;

temp[j].y = sp[(i+1)%n_sp].y;

else if (p1==1 && p2==1)

j++;

temp[j].x = sp[(i+1)%n_sp].x;

temp[j].y = sp[(i+1)%n_sp].y;

else

25
}

for (int i=0; i<=j; i++)

sp[i].x = temp[i].x;

sp[i].y = temp[i].y;

n_sp = j+1;

int main()

int gd=DETECT, gm=0;

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

cout<<"Enter no. of vertices of clipping window"<<endl;

cin>>n_cw;

cout<<"Enter vertices (x,y) clockwise"<<endl;

for (int i=0; i<n_cw; i++)

cin>>cw[i].x>>cw[i].y;

draw_poly(cw, n_cw);

cout<<"Enter no. of vertices of subject polygon"<<endl;

cin>>n_sp;
26
cout<<"Enter vertices (x,y) clockwise"<<endl;

for (int i=0; i<n_sp; i++)

cin>>sp[i].x>>sp[i].y;

draw_poly(sp, n_sp);

char ch;

cout<<"Press a key to clip from an edge of clipping window"<<endl;

for(int i=0; i<n_cw; i++)

cin>>ch;

edge_clip(cw[i].x, cw[i].y, cw[(i+1)%n_cw].x, cw[(i+1)%n_cw].y);

cleardevice();

draw_poly(cw, n_cw);

draw_poly(sp, n_sp);

getch();

closegraph();

return 0;

27
Output:

28
5. Write a program to apply various 2D transformations on a 2D object
(use homogenous Coordinates).

i.Translation

Program:

#include<graphics.h>

#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

system("cls");

int gd = DETECT,gm;

initgraph(&gd, &gm, "c://turboc3//bgi");

int x1 = 100, y1 = 100, x2 = 80, y2 = 150, x3 = 120, y3 = 150, tx, ty;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

cout << "Enter translation value among x-axis: ";

cin >> tx;

cout << "Enter translation value among y-axis: ";

29
cin >> ty;

x1 += tx; x2 += tx; x3 += tx;

y1 += ty; y2 += ty; y3 += ty;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

getch();

return 0;

Output:

30
ii.Scaling

Program:

#include<iostream>

#include<conio.h>

#include<graphics.h>

using namespace std;

int main()

system("cls");

int gd = DETECT, gm;

31
initgraph(&gd, &gm, "c://turboc3//bgi");

int x1 = 100, y1 = 100, x2 = 80, y2 = 150, x3 = 120, y3 = 150, sx, sy;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

cout << "Enter scaling value among x-axis: ";

cin >> sx;

cout << "Enter scaling value among y-axis: ";

cin >> sy;

x1 *= sx; x2 *= sx; x3 *= sx;

y1 *= sy; y2 *= sy; y3 *= sy;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

getch();

return 0;

Output:

32
iii.Rotation
Program:

#include<iostream>

#include<conio.h>

#include<math.h>

#include<graphics.h>

using namespace std;

int main()

system("cls");

33
int gd = DETECT, gm;

initgraph(&gd, &gm, "c://turboc3//bgi");

int x1 = 200, y1 = 200, x2 = 180, y2 = 250, x3 = 220, y3 = 250;

float angle;

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

cout << "Enter the rotation angle: ";

cin >> angle;

angle = angle * 3.1428 / 180;

x1 = x1 * cos(angle) - y1 * sin(angle);

y1 = (x1 * sin(angle)) + y1 * cos(angle);

x2 = x2 * cos(angle) - y2 * sin(angle);

y2 = (x2 * sin(angle)) + y2 * cos(angle);

x3 = x3 * cos(angle) - y3 * sin(angle);

y3 = (x3 * sin(angle)) + y3 * cos(angle);

line(x1, y1, x2, y2);

line(x2, y2, x3, y3);

line(x3, y3, x1, y1);

getch();

return 0;

34
Output:

iv.Reflection

Program:

#include<iostream>

#include<conio.h>

#include<graphics.h>

using namespace std;

int main()

35
int gd = DETECT, gm;

initgraph(&gd, &gm, " ");

int x1, y1, x2, y2, X1, X2, Y1, Y2;

cout << "Enter the coordinates of the line\n";

cin >> x1 >> y1 >> x2 >> y2;

int m = getmaxx();

int n = getmaxy();

setcolor(6);

line(x1, y1, x2, y2);

outtextxy(x1 + 5, y1 + 10, "Original Object");

setcolor(5);

line(y1, x1, y2, x2);

outtextxy(y2, x2 + 10, "Reflection y=-x");

setcolor(4);

line((m/2), 0, (m/2), n);

line(0, (n/2), m, (n/2));

setcolor(3);

int c = (n/2) - y1;

int d = (n/2) - y2;

Y2 = y2 + (d*2);

Y1 = y1 + (c*2);

line(x2, Y2, x1, Y1);


36
outtextxy(x1, Y1 + 10, "Reflection along X-axis");

setcolor(9);

int a = (m/2) - x1;

int b = (m/2) - x2;

X1 = x1 + (a*2);

X2 = x2 + (b*2);

line(X1, y1, X2, y2);

outtextxy(X2 - 20, y2 + 10, "Reflection along Y-axis");

line(X1, Y1, X2, Y2);

outtextxy(X1, Y1 + 10, "Reflection y=x");

getch();

closegraph();

return 0;

Output:

37
v.Shearing

#include<iostream>

#include<graphics.h>

using namespace std;

void drawTriangle(int x1,int y1, int x2,int y2,int x3,int y3){

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

38
line(x3,y3,x1,y1);

void shear(int &x1,int &y1,int &x2,int &y2,int &x3,int &y3,int c,int d){

x1=x1+c*y1;

x2=x2+c*y2;

x3=x3+c*y3;

y1=y1+d*x1;

y2=y2+d*x2;

y3=y3+d*x3;

int main(){

int x1,y1,x2,y2,x3,y3,c,d;

int gd= DETECT,gm;

initgraph(&gd,&gm,(char*)"");

cout<<"enter first point"<<endl;

cin>>x1>>y1;

cout<<"enter second point"<<endl;

cin>>x2>>y2;

cout<<"enter third point"<<endl;

cin>>x3>>y3;

39
drawTriangle(x1,y1,x2,y2,x3,y3);

cout<<"Enter shearing factors"<<endl;

cin>>c>>d;

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

shear(x1,y1,x2,y2,x3,y3,c,d);

getch();

closegraph();

return 0;

Output:

40
41
6. Write a program to apply various 3D transformations on a 3D object
and then apply parallel and perspective projection on it.

Program:

#include<iostream>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#define pi 3.14285714

using namespace std;

class projections

double vertices[8][4];

double t_matrix[4][4];

double result[8][4];

public:

projections(){};

void get_vertices();

void display_cube();

void multiplication();

void copyback();

void orthographic();

42
void axonometric(double angle_x,double angle_y);

void cavalier(double angle);

void cabinet(double angle);

void single_point(double r);

void two_point(double r);

void three_point(double r);

};

void projections::get_vertices()

for(int i=0;i<8;i++)

cout<<"\nEnter vertex "<<i+1<<":";

cout<<"\nx : ";

cin>>vertices[i][0];

result[i][0]=vertices[i][0];

cout<<"y : ";

cin>>vertices[i][1];

result[i][1]=vertices[i][1];

cout<<"z : ";

cin>>vertices[i][2];

result[i][2]=vertices[i][2];

vertices[i][3]=result[i][3]=1;

}
43
}

void projections::display_cube()

int i=0;

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

line(result[i][0],result[i][1],result[i+1][0],result[i+1][1]);

line(result[i][0],result[i][1],result[0][0],result[0][1]);

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

line(result[i][0],result[i][1],result[i+1][0],result[i+1][1]);

line(result[i][0],result[i][1],result[4][0],result[4][1]);

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

line(result[i][0],result[i][1],result[i+4][0],result[i+4][1]);

void projections::copyback()

int i=0,j=0;

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

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

result[i][j]=vertices[i][j];

void projections::multiplication()

double r[8][4];
44
int i=0,j=0,k=0;

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

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

r[i][j]=0;

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

r[i][j]+=result[i][k]*t_matrix[k][j];

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

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

result[i][j]=r[i][j];

void projections::orthographic()

cleardevice();

clearviewport();

cout<<"\tORTHOGRAPHIC PROJECTION";

copyback();

t_matrix[0][0]=1;

t_matrix[0][1]=0;

t_matrix[0][2]=0;
45
t_matrix[0][3]=0;

t_matrix[1][0]=0;

t_matrix[1][1]=1;

t_matrix[1][2]=0;

t_matrix[1][3]=0;

t_matrix[2][0]=0;

t_matrix[2][1]=0;

t_matrix[2][2]=0;

t_matrix[2][3]=0;

t_matrix[3][0]=0;

t_matrix[3][1]=0;

t_matrix[3][2]=0;

t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

void projections::axonometric(double angle_x,double angle_y)

angle_x=((pi/180)*angle_x);

angle_y=((pi/180)*angle_y);
46
cleardevice();

clearviewport();

cout<<"\tAXONOMETRIC PROJECTION";

copyback();

t_matrix[0][0]=cos(angle_y);

t_matrix[0][1]=(sin(angle_y))*(sin(angle_x));

t_matrix[0][2]=0;

t_matrix[0][3]=0;

t_matrix[1][0]=0;

t_matrix[1][1]=cos(angle_x);

t_matrix[1][2]=0;

t_matrix[1][3]=0;

t_matrix[2][0]=sin(angle_y);

t_matrix[2][1]=(-1)*(cos(angle_y)*sin(angle_x));

t_matrix[2][2]=0;

t_matrix[2][3]=0;

t_matrix[3][0]=0;

t_matrix[3][1]=0;

t_matrix[3][2]=0;

t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();
47
delay(20);

getch();

void projections::cavalier(double angle)

angle=((pi/180)*angle);

cleardevice();

clearviewport();

cout<<"\tCAVALIER PROJECTION";

copyback();

t_matrix[0][0]=1;

t_matrix[0][1]=0;

t_matrix[0][2]=0;

t_matrix[0][3]=0;

t_matrix[1][0]=0;

t_matrix[1][1]=1;

t_matrix[1][2]=0;

t_matrix[1][3]=0;

t_matrix[2][0]=(-1)*1*cos(angle);

t_matrix[2][1]=(-1)*1*sin(angle);

t_matrix[2][2]=0;

t_matrix[2][3]=0;

t_matrix[3][0]=0;
48
t_matrix[3][1]=0;

t_matrix[3][2]=0;

t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

void projections::cabinet(double angle)

angle=((pi/180)*angle);

cleardevice();

clearviewport();

cout<<"\tCABINET PROJECTION";

copyback();

t_matrix[0][0]=1;

t_matrix[0][1]=0;

t_matrix[0][2]=0;

t_matrix[0][3]=0;

t_matrix[1][0]=0;

t_matrix[1][1]=1;

t_matrix[1][2]=0;
49
t_matrix[1][3]=0;

t_matrix[2][0]=(-1)*0.5*cos(angle);

t_matrix[2][1]=(-1)*0.5*sin(angle);

t_matrix[2][2]=0;

t_matrix[2][3]=0;

t_matrix[3][0]=0;

t_matrix[3][1]=0;

t_matrix[3][2]=0;

t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

void projections::single_point(double r)

double l=10,m=10,n=10;

cleardevice();

clearviewport();

cout<<"\tSINGLE POINT PRESPECTIVE PROJECTION";

r=(-1/r);

copyback();
50
t_matrix[0][0]=1;

t_matrix[0][1]=0;

t_matrix[0][2]=0;

t_matrix[0][3]=0;

t_matrix[1][0]=0;

t_matrix[1][1]=1;

t_matrix[1][2]=0;

t_matrix[1][3]=0;

t_matrix[2][0]=0;

t_matrix[2][1]=0;

t_matrix[2][2]=0;

t_matrix[2][3]=r;

t_matrix[3][0]=l;

t_matrix[3][1]=m;

t_matrix[3][2]=0;

t_matrix[3][3]=r*n+1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

void projections::two_point(double r)
51
{

double angle=45;

angle=(pi/180)*angle;

cleardevice();

clearviewport();

cout<<"\tTWO POINT PRESPECTIVE PROJECTION";

copyback();

r=(-1/r);

t_matrix[0][0]=cos(angle);

t_matrix[0][1]=0;

t_matrix[0][2]=(-1*sin(angle));

t_matrix[0][3]=sin(angle)/r;

t_matrix[1][0]=0;

t_matrix[1][1]=1;

t_matrix[1][2]=0;

t_matrix[1][3]=0;

t_matrix[2][0]=sin(angle);

t_matrix[2][1]=0;

t_matrix[2][2]=cos(angle);

t_matrix[2][3]=(-1*cos(angle))/r;

t_matrix[3][0]=0;

t_matrix[3][1]=0;

t_matrix[3][2]=0;
52
t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

void projections::three_point(double r)

double angle_y=45,angle_x=45;

angle_y=(pi/180)*angle_y;

angle_x=(pi/180)*angle_x;

cleardevice();

clearviewport();

cout<<"\tTHREE POINT PRESPECTIVE PROJECTION";

copyback();

r=(-1/r);

t_matrix[0][0]=cos(angle_y);

t_matrix[0][1]=sin(angle_y)*sin(angle_x);

t_matrix[0][2]=0;

t_matrix[0][3]=(sin(angle_y)*cos(angle_x))/r;

t_matrix[1][0]=0;

t_matrix[1][1]=cos(angle_x);
53
t_matrix[1][2]=0;

t_matrix[1][3]=(-1*sin(angle_x))/r;

t_matrix[2][0]=(sin(angle_y));

t_matrix[2][1]=(-1*cos(angle_y)*sin(angle_x));

t_matrix[2][2]=0;

t_matrix[2][3]=(-1*cos(angle_y)*cos(angle_x))/r;

t_matrix[3][0]=0;

t_matrix[3][1]=0;

t_matrix[3][2]=0;

t_matrix[3][3]=1;

multiplication();

setcolor(YELLOW);

display_cube();

delay(20);

getch();

int main()

system("cls");

int gd=DETECT,gm,choice;

projections t1;

char ch1,ch2,axis,axis1,axis2;

double angle_x,angle_y,angle,ratio,ratio1,ratio2,ratio3;
54
do

cout<<"\n\n\t........PROJECTIONS OF 3D OBJECTS........\n";

cout<<"\nEnter the details of a cube(i.e. 3D object)";

t1.get_vertices();

do

initgraph(&gd,&gm,(char*)"");

cout<<"\n\n...........MENU...........";

cout<<"\n1.Orthographic.";

cout<<"\n2.Axonometric.";

cout<<"\n3.Cavalier (Oblique type 1).";

cout<<"\n4.Cabinet (Oblique type 2)";

cout<<"\n5.Single-Point presepective.";

cout<<"\n6.Two-Point presepective.";

cout<<"\n7.Three-Point presepective.";

cout<<"\n............................";

cout<<"\n\nEnter your choice :: ";

cin>>choice;

switch(choice)

case 1:t1.orthographic();

break;
55
case 2:cout<<"\n\nFOR AXONOMETRIC PROJECTION";

cout<<"\nEnter the angle of rotation about :";

cout<<"\nx-axis : ";

cin>>angle_x;

cout<<"y-axis : ";

cin>>angle_y;

t1.axonometric(angle_x,angle_y);

break;

case 3:cout<<"\n\nFOR CAVALIER PROJECTION";

cout<<"\nEnter the angle of inclination : ";

cin>>angle;

t1.cavalier(angle);

break;

case 4:cout<<"\n\nFOR CABINET PROJECTION";

cout<<"\nEnter the angle of inclination : ";

cin>>angle;

t1.cabinet(angle);

break;

case 5:cout<<"\n\nFOR SINGLE POINT PRESPECTIVE PROJECTION";

cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";

cin>>ratio;

t1.single_point(ratio);

break;
56
case 6:cout<<"\n\nFOR TWO POINT PRESPECTIVE PROJECTION";

cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";

cin>>ratio;

t1.two_point(ratio);

break;

case 7:cout<<"\n\nFOR THREE POINT PRESPECTIVE PROJECTION";

cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";

cin>>ratio;

t1.three_point(ratio);

break;

default:cout<<"\n\n\t!!!INVALID CHOICE!!!";

getch();

closegraph();

cout<<"\nDo you want to try another projection(Y/N)?";

cin>>ch2;

}while(ch2=='y' || ch2=='Y');

cout<<"\nDo you want to try a cube with different dimensions(Y/N)?";

cin>>ch1;

}while(ch1=='y' || ch1=='Y');

getch();

return 0;

}
57
Output:

58
59
60
61
62
63
7. Write a program to draw Hermite /Bezier curve.

Program:

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

#include <math.h>

#include <conio.h>

#include <iostream>

using namespace std;

// Function to implement DDA line drawing algorithm.

int lineDDA(int xa, int ya, int xb, int yb)

int dx = xb - xa;

int dy = yb - ya;

int steps, k;

float x_inc, y_inc;

float x = xa, y = ya;

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

steps = abs(dx);

else
64
steps = abs(dy);

x_inc = dx / (float) steps;

y_inc = dy / (float) steps;

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

for (int k = 0; k < steps; k++)

x += x_inc;

y += y_inc;

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

return 0;

// Function to make a bezier curve with 4 control points.

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

int gd = DETECT, gm;

int i;

double t;

initgraph(&gd, &gm, "");

65
// Original curve

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

// New curve

lineDDA(x[0], y[0], x[1], y[1]);

lineDDA(x[1], y[1], x[2], y[2]);

lineDDA(x[2], y[2], x[3], y[3]);

int main()

int x[4], y[4];

int i;

66
cout << "\n Enter the X and Y coordinates of the four control points:" << endl;

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

cin>>x[i]>>y[i];

bezier(x, y);

getch();

closegraph();

return 0;

Output:

67
68
8. DDA line algorithm.

Program:

#include<iostream>

#include<graphics.h> //gm is graphics mode which is a computer display mode


that generates image using pixels.

#include<math.h>

using namespace std;

float x_mid, y_mid;

//function to implement DDA line drawing algorithm.

int algoDDA(int xa, int ya, int xb, int yb)

//xa, ya - initial x and y coordinates

//xb, yb - final x and y coordinates

int dx = xb-xa;

int dy = yb-ya;

int steps, k;

float x_inc, y_inc;

float x=xa, y=ya;

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

69
steps=abs(dx);

else

steps=abs(dy);

x_inc = dx/float(steps);

y_inc = dy/float(steps);

putpixel(round(x)+x_mid, y_mid-round(y), YELLOW);

for(int i=0; i<steps; i++)

x += x_inc;

y += y_inc;

putpixel(round(x)+x_mid, y_mid-round(y), YELLOW);

return 0;

int main()

//DETECT is a macro defined in "graphics.h" header file

int gd = DETECT, gm;

initgraph(&gd,&gm,""); //initgraph initializes the graphics system by


loading a graphics driver from disk.

70
//getmaxx() and getmaxy() returns the maximum (screen-relative) x and y
value for the current graphics driver and mode

float X = getmaxx(), Y = getmaxy();

x_mid = X/2;

y_mid = Y/2;

int x1, y1, x2, y2;

cout<<"\n Enter coordinate x1: ";

cin>>x1;

cout<<"\n Enter coordinate x2: ";

cin>>x2;

cout<<"\n Enter coordinate y1: ";

cin>>y1;

cout<<"\n Enter coordinate y2: ";

cin>>y2;

cout<<"\n Graph using DDA algorithm: "<<endl;

//line(x1,y1.x2,y2) draws a line from x1,y1,x2,y2

line(x_mid, 0, x_mid, Y);

line(0, y_mid, X, y_mid);

71
/*displays a text string in the viewport at the given distance (x,y)

using the current justification settings and the current font, direction and size
*/

outtextxy(x_mid-40, y_mid+10, "(0,0)");

outtextxy(x_mid-60, y_mid-230, "Y-axis");

outtextxy(x_mid-70, y_mid+220, "Y-axis");

outtextxy(x_mid-305, y_mid+10, "X-axis");

outtextxy(x_mid+260, y_mid+10, "X-axis");

setcolor(GREEN);

algoDDA(x1, y1, x2, y2);

getch();

/* deallocates all the memory allocated by the graphics system,

then restores the screen to the mode it was in before you called initgraph.*/

closegraph();

return 0;

Output:

72
73
74
9. Mid point line algorithm.

Program:

#include<iostream>

#include<graphics.h>

#include<stdlib.h>

using namespace std;

void midPoint(int x1,int y1,int x2,int y2){

int dx,dy;

dx=x2-x1;

dy=y2-y1;

if(dy<=dx){

int d = dy - (dx/2);

int x = x1,y=y1;

cout<<"("<<x<<','<<y<<")"<<'\n';

while(x<x2){

putpixel(x,y,6);

x++;

if(d<0)

d=d+dy;

else{
75
d += dy-dx;

y++;

cout<<"("<<x<<','<<y<<")"<<'\n';

else if(dx<dy){

int d= dx - (dy/2);

int x = x1,y=y1;

cout<<"("<<x<<','<<y<<")"<<'\n';

while(y<y2){

putpixel(x,y,6);

y++;

if(d<0){

d=d+dx;

else{

d += dx-dy;

x++;

cout<<"("<<x<<','<<y<<")"<<'\n';

76
}

int main(){

system("cls");

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)"");

int x1,x2,y1,y2;

cout<<"Enter coordinates in sequence x1 y1 x2 y2"<<endl;

cin>>x1>>y1>>x2>>y2;

midPoint(x1,y1,x2,y2);

getch();

return 0;

77
Output:

78

You might also like