You are on page 1of 32

INDEX

No. Experiment Name Date of Date of Page


Experiment Submission
01. Write a program to
implement DDA (Digital
2-6
Differential Analyzer) line
drawing algorithm.
02. Write a program to
implement Bresenham's line 7-10
drawing algorithm.
03. Write a program to
implement Bresenham's circle 11-14
drawing algorithm.
04. Write a program to
implement Midpoint Circle 15-18
drawing algorithm.
05. Write a program to
implement Polygon 19-22
(Rectangle) filling algorithm.
06. Write a program to
implement Cohen-Sutherland 23-28
line clipping algorithm.
07. Write a program to
implement 2D
29-32
Transformations (Translation,
Rotation) algorithm.

Course Teacher Chairman


Department of CSE Department of CSE
=2=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 01

Experiment Title: Write a program to implement DDA (Digital Differential


Analyzer) line drawing algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=3=

Objective: This objective of the program is to implement DDA line


algorithm. 

Theory: The digital differential analyzer (DDA) is an incremental scan-


conversation method. Such an approach is characterized by
performing calculations at each step using results from the
preceding step. Suppose at step i we have calculated (xi, yi) to
be a point on the line. Since the next point (xi+1, yi+1) should
satisfy ∆y/∆x=m where ∆y=yi+1-yi and ∆x=xi+1-xi we have 

yi+1=yi + m∆x 
or xi+1=xi + ∆y/m 
Algorithm:
double m = (double) (y2 - y1) / (double) (x2 - x1);

double x = x1;
double y = y1;
for (int i = min(x1, x2); i <= max(x1, x2); i++) {

drawPoint(x, y);
printf("%.1f, %.1f\n", x, y);

if (m == 1) {
x = x + 1;
y = y + 1;
}
else if (m > 1) {
x = x + (double)1.0 / m;
y = y + 1;
}
else if (m < 1) {
x = x + 1;
y = y + m;
}
}
=4=

Source Code:

#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

void drawPoint(int x, int y)


{
glPointSize(5.0);
glBegin(GL_POINTS);
glColor3f( 1.0 , 1.0 , 0.0 ) ;
glVertex2f(x, y);
glEnd();
}

/*line drawing using DDA algorithm*/

void drawLines(int x1, int y1, int x2, int y2)

double m = (double) (y2 - y1) / (double) (x2 - x1);

double x = x1;
double y = y1;
for (int i = min(x1, x2); i <= max(x1, x2); i++) {

drawPoint(x, y);
printf("%.1f, %.1f\n", x, y);

if (m == 1) {
x = x + 1;
y = y + 1;
}
else if (m > 1) {
x = x + (double)1.0 / m;
y = y + 1;
}
else if (m < 1) {
=5=

x = x + 1;
y = y + m;
}
}
}

static void display(void)


{
glPushMatrix();
drawLines(0, 0, 8, 4);
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("DDA");
glClearColor(0,0,0,0);
//glOrtho(-200, 200, -200, 200, -1, 1);

glutDisplayFunc(display);

glutMainLoop();

return EXIT_SUCCESS;
}
=6=

Sample output:

Discussion: 

This program is to implement DDA line algorithm. 


=7=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 02

Experiment Title: Write a program to implement Bresenham's line drawing


algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=8=

Objective: This objective of the program is to implement Bresenham’s


line algorithm.

Theory: Bresenham’s line algorithm is a highly efficient incremental


method for scan-converting lines. We want to scan-convert
the line where 0<m<1. We start with pixel P’ 1(x’1, y’1), then
select  subsequent pixels as we work our way to the right, one
pixel position at a time in the horizontal  direction towards
P’2(x’2, y’2). 

Algorithm:
while (x1 < x2) {

x1++;

if (p >= 0){
y1++;
p = p + 2 * dy - 2 * dx;
}
else{
p=p+2*dy;
}
drawPoint(x1, y1);
printf("%d, %d\n", x1, y1);
}

Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void drawPoint(int x, int y)
{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f( 1.0 , 1.0 , 0.0 ) ;
glVertex2f(x, y);
glEnd();
}
=9=

/*line drawing using Bresenham*/

void drawLinesManual(int x1, int y1, int x2, int y2)


{
int dx = (x2- x1);
int dy = (y2- y1);

int p = 2 * dy - dx;

//int x = x1;
//int y = y1;

while (x1 < x2) {

x1++;

if (p >= 0){
y1++;
p = p + 2 * dy - 2 * dx;
}
else{
p=p+2*dy;
}
drawPoint(x1, y1);
printf("%d, %d\n", x1, y1);
}
}

static void display(void)


{
glPushMatrix();
drawLinesManual(0, 0, 10, 8);
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
=10=

glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham's Algorithm");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);

glutDisplayFunc(display);

glutMainLoop();

return EXIT_SUCCESS;
}

Sample Output:

Discussion:

This program is to implement Bresenham’s line algorithm. 


=11=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 03

Experiment Title: Write a program to implement Bresenham's circle


drawing algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=12=

Objective: This objective of the program is to implement Bresenham’s


circle algorithm.
Theory: Bresenham’s circle algorithm allows scan-converting a circle
using Bresenham’s  algorithm works as follows. If the eight-
way symmetry of a circle is used to generate a circle, points 
will only have to be generated through a 45° angle. And, if
points are generated from 90°t 45°, moves  will be made only
in the +x and –y directions. 

Algorithm:
int x =0;
int y =r;
int d = 3-2*r;
drawCirclePixel(x,y);
while(y>x)
{
x++;
//update d,x,y
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else{
d=d+4*x+6;
}
drawCirclePixel(x,y);
printf("%d,%d\n",x,y);
}
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
void drawPixel(int x, int y)
{
glPointSize(5.0);
glBegin(GL_POINTS);
=13=

glColor3f( 1.0 , 1.0 , 0.0 ) ;


glVertex2f(x, y);
glEnd();
}
/*line drawing using bresenham_circle algorithm*/
void drawCirclePixel(int x, int y)
{
//for each pixel we will draw all eight pixels
drawPixel(+x, +y);
drawPixel(-x, +y);
drawPixel(+x, -y);
drawPixel(-x, -y);
drawPixel(+y, +x);
drawPixel(-y, +x);
drawPixel(+y, -x);
drawPixel(-y, -x);
}
/*circle drawing using Bresenham*/

void drawCircle(int r)
{
int x =0;
int y =r;
int d = 3-2*r;
drawCirclePixel(x,y);
while(y>x)
{
x++;
//update d,x,y
if(d>0)
{
y--;
d=d+4*(x-y)+10;
}
else{
d=d+4*x+6;
}
drawCirclePixel(x,y);
printf("%d,%d\n",x,y);
=14=

}
}
static void display (void)
{
glPushMatrix();
drawCircle(10);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Bresenham_circle");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);

glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
Sample Output:

Discussion:

This program is to implement Bresenham’s circle algorithm.


=15=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 04

Experiment Title: Write a program to implement Midpoint Circle drawing


algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=16=

Objective: This objective of the program is to implement midpoint ellipse


drawing algorithm. 
Theory: The ellipse, like the circle, shows symmetry. In the case of an
ellipse, however, symmetry is four-rather than eight-way.
There are two methods of mathematically defining a ellipse.
The polynomial method of defining an ellipse is given by the
expression 
(x-h)2/a2 + (y-k)2/b2 = 1 
Algorithm:

for(x=1;x<=x2;x++) { 
y = b * sqrt(1 - (pow(x,2)/pow(a,2)));  

Source Code:

#include<windows.h>  
#include<iostream.h>  
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<math.h>  
#include<GL/gl.h>  
#include<GL/glu.h>  
#include<GL/glut.h>  
const int screenWidth = 640*2;  
const int screenHeight = 480*2;  
GLdouble A,B,C,D;  
void myInit(void)  {  
glClearColor(1.0, 1.0, 1.0, 0.0);  
glColor3f(0.0f, 0.0f, 0.0f);  
glPointSize(4.0);  
glMatrixMode(GL_PROJECTION);  
glLoadIdentity();  
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);  
A = screenWidth/4.0;  
B= 0.0;  
C=D=screenHeight/2.0;  
gluOrtho2D(0.0, 640.0, 0.0, 480.0);  }  
=17=

void myDisplay(void)  {  


glClear(GL_COLOR_BUFFER_BIT);  
glPointSize(2.0);  
GLint h = 300, k = 300, x, x2, y, i = 1, a = 150, b = 25; x2 = a;  
for(x=1;x<=x2;x++) { 

y = b * sqrt(1 - (pow(x,2)/pow(a,2)));  
glBegin(GL_POINTS); 

{  
glVertex2i(x+h,y+k);  
glVertex2i(-x+h,-y+k); 
glVertex2i(-x+h,y+k); 
glVertex2i(x+h,-y+k); 


glEnd(); 

glFlush();  

int main(int argc, char** argv) 

 glutInit(&argc, argv);  

 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  

 glutInitWindowSize(640,480);  

 glutInitWindowPosition(100,150);  

 glutCreateWindow("My First Window");  

 glutDisplayFunc(myDisplay); 

 myInit(); 

 glutMainLoop();  

}
=18=

 
Sample Output: 

  
Discussion:

This program is to implement ellipse drawing algorithm.


=19=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 05

Experiment Title: Write a program to implement Polygon (Rectangle) filling


algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=20=

Objective: This objective of the program to implement Polygon


(Rectangle) filling algorithm.

Theory: A rectangle whose sides are parallel to the coordinate axes


may be constructed if the locations of two vertices. The
remaining corner points are then derived. Once the vertices
are known, the 4 sets of coordinates are sent to the line
routine & the rectangle is scan-converted. In the case of the
rectangle, lines would be drawn as follows: line (x 1, y1) to
(x1,y1); line (x1,y2) to (x2,y2); line (x2,y2) to (x2,y1); line (x2,y1) to
(x1,y1).

Algorithm:
for (int y = bl_y; y <= tl_y; y++){
for (int x = bl_x; x <=br_x; x++) {
drawPoint(x, y);
}
}
Source Code:
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
void drawPoint(int x, int y)
{
glLineWidth(1.0);
glBegin(GL_POINTS);
glColor3f( 1.0 , 1.0 , 0.0 ) ;
glVertex2f(x, y);
glEnd();
}
/*
b-bottom
t--top
l-left
r-right
*/
=21=

/*Polygon (Rectangle) filling algorithm*/


void drawRectangle(int bl_x, int bl_y, int br_x, int br_y, int tr_x, int tr_y, int
tl_x, int tl_y)

{
for (int y = bl_y; y <= tl_y; y++){
for (int x = bl_x; x <=br_x; x++) {
drawPoint(x, y);
}
}
}

static void display(void)


{
glPushMatrix();
drawRectangle(0, 0, 70, 0, 70, 50, 0, 50);
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Polygon (Rectangle) filling algorithm");
glClearColor(0,0,0,0);
glOrtho(-200, 200, -200, 200, -1, 1);

glutDisplayFunc(display);

glutMainLoop();

return EXIT_SUCCESS;
}
=22=

Sample Output:

  
Discussion:

This program is to implement polygon (rectangle) filling algorithm.


=23=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 06

Experiment Title: Write a program to implement Cohen-Sutherland line


clipping algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=24=

Objective: This objective of the program is to implement Cohen-


Suntherland line clipping  algorithm. 
Theory: In this algorithm we divide the line clipping process two
phases: (1) identify those lines  which intersect the clipping
window & so need to be clipped & (2) perform the clipping. All
lines fall  into one of the following clipping categories:  
1. Visible- both endpoints of the line lie within the
window.
2. Not visible- the line definitely lies outside the window.
This will occur if the line from (x 1,y1) to  (x2,y2) satisfies
any one of the following four inequalities:  
x1,x2 > xmax y1,y2 > ymax 
x1,x2 < xmin y1,y2 < ymin  
3. Clipping candidate- the line is in neither category
1 nor 2. 

Source Code: 

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<iterator>  
#include <windows.h>  
#include <gl/glut.h>  
using namespace std;  
#define REP(i,n) for(i=0; i<(n); i++)  
#define FOR(i,a,b) for(i=(a); i<=(b); i++)  #define WIDTH 640  
#define HEIGHT 480  #define Wxmin 0  
#define Wxmax 200  
#define Wymin 0  
#define Wymax 200  struct wind {  double x[4],y[4];  }W;  
void reshape(int width, int height) {  
glViewport(0,0,width, height); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
=25=

glOrtho(-WIDTH/2, WIDTH/2+1, -HEIGHT/2, HEIGHT/2+1,-


1000,1000);

void drawPixel(int x, int y, int op) { 


if(op == 0) glVertex2i(x,y); 
else if(op == 3) glVertex2i(-x,y); 
else if(op == 4) glVertex2i(-x,-y); 
else if(op == 7) glVertex2i(x,-y); 
else if(op == 1) glVertex2i(y,x); 
else if(op == 2) glVertex2i(-y,x); 
else if(op == 5) glVertex2i(-y, -x); 
else if(op == 6) glVertex2i(y, -x);  }  void drawLine(int x0,int
y0,int x1,int y1,int value) { 
int dx = x1 - x0; 
int dy = y1 - y0; 
int dinitial = 2*dy - dx; 
int dE = 2*dy; 
int dNE = 2*(dy - dx); 
int x = x0; 
int y = y0; 
drawPixel(x,y,value); 
while ( x < x1 ) { 
if( dinitial < 0 ) { 
x ++; 
dinitial += dE; 

else { 
x++; 
y++; 
dinitial += dNE; 

drawPixel(x,y,value); 
} }  void drawSlop(int x0, int y0, int x1, int y1) { 
int dx = x1 - x0; 
int dy = y1- y0; 
if(abs(dx)>=abs(dy)){ 
if((x1>=x0)&&(y1>=y0)) 
=26=

drawLine(x0,y0,x1,y1,0); 
else if((x1<x0)&&(y1>=y0)) 
drawLine(-x0,y0,-x1,y1,3); 
else if((x1<x0) && (y1<y0)) 
drawLine(-x0,-y0,-x1,-y1,4);
15 
else 
drawLine(x0,-y0,x1,-y1,7); } 

else { 
if((x1>=x0)&&(y1>=y0)) 
drawLine(y0,x0,y1,x1,1); 
else if((x1<x0)&&(y1>=y0)) 
drawLine(y0,-x0,y1,-x1,2); 
else if((x1<x0)&&(y1<y0)) 
drawLine(-y0,-x0,-y1,-x1,5); 
else if((x1>=x0)&&(y1<=y0)) 
drawLine(-y0,x0,-y1,x1,6); } 
}  void CohenSutherlandLineClipAndDraw(double x0,double
y0,double x1,double y1,double  xmin,double xmax,double
ymin,double ymax) 

outcode outcode0,outcode1,outcodeOut; 
bool accept = false,done = false; 
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax); 
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax); 
do{ 
if(!(outcode0|outcode1)) 
accept = true; done = true;  
else if(outcode0&outcode1) 
done = true; 
else { 
double x,y; 
outcodeOut = outcode0?outcode0:outcode1; 
if(outcodeOut&TOP){ 
x = x0 + (x1-x0)*(ymax-y0)/(y1-y0); 
y = ymax; 

=27=

else if(outcodeOut&BOTTOM){ 
x = x0 + (x1-x0)*(ymin-y0)/(y1-y0); 
y = ymin; 

else if(outcodeOut&RIGHT){ 
y = y0 + (y1-y0)*(xmax-x0)/(x1-x0); 
x = xmax; 

else { 
y = y0 + (y1-y0)*(xmin-x0)/(x1-x0); 
x = xmin; 

if(outcodeOut==outcode0) { 
x0 = x; y0= y; 
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax); } 
else { 
x1 = x; y1 = y; 
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax); } } 
}while(done==false); 
if(accept) { 
drawSlop(x0,y0,x1,y1); 


CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.
x[1],W.y[0],W.y[2]); } 
REP(i,2) 

j = (i + 2)%4; 
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.
x[1],W.y[0],W.y[2]);  } 

void display(void)  

glColor4f(1.0,0.0,0.0,1.0); 
angle = 0; 
getPoint(); 
getPos(); 
setW(); 
while(true) 
{
=28=

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
glBegin(GL_POINTS); 
resetW(); 
glColor4f(3.0,5.0,1.0,1.0); 
drawW(); 
glColor4f(0.0,0.0,0.0,0.0); 
drawTetra(); 
glColor4f(1.0,0.0,0.0,1.0); 
drawTetraC(); 
glEnd(); 
glutSwapBuffers(); 


int main(int argc, char **argv) 

glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_DOUBLE); 
glutInitWindowPosition(-1,-1); 
glutInitWindowSize(WIDTH, HEIGHT); 
glutCreateWindow("My Window"); 
glutReshapeFunc(reshape); 
glutDisplayFunc(display); 
glutMainLoop(); 

Sample Output:

Discussion: 

This program is to implement Cohen-Suntherland line clipping  algorithm. 


=29=

MOHAMMADPUR KENDRIYA COLLEGE, DHAKA


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE– 325
Experiment No: 07

Experiment Title: Write a program to implement 2D Transformations


(Translation, Rotation) algorithm.

Date of Experiment:

Date of Submission:

SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature

SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
=30=

Objective: This objective of the program to implement 2D


Transformations (Translation, Rotation) algorithm.
Theory: In this algorithm we divide the line clipping process two
phases: (1) identify those lines  which intersect the clipping
window & so need to be clipped & (2) perform the clipping. All
lines fall  into one of the following clipping categories:  
1. Visible- both endpoints of the line lie within the
window.
2. Not visible- the line definitely lies outside the window.
This will occur if the line from (x 1,y1) to  (x2,y2) satisfies
any one of the following four inequalities:  
x1,x2 > xmax y1,y2 > ymax 
x1,x2 < xmin y1,y2 < ymin  
3. Clipping candidate- the line is in neither category
1 nor 2. 
Source Code: 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void 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,"c:\tc\bg:");
            printf("\t Program for basic transactions");
            printf("\n\t Enter the points of triangle");
            setcolor(1);
            scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
            line(x1,y1,x2,y2);
            line(x2,y2,x3,y3);
            line(x3,y3,x1,y1);
            getch();
            printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
            printf("Enter your choice:");
            scanf("%d",&c);
            switch(c)
            {
                        case 1:
                                    printf("\n Enter the translation factor");
                                    scanf("%d%d",&xt,&yt);
=31=

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

                        case 2:
                                    printf("\n Enter the angle of rotation");
                                    scanf("%d",&r);
                                    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);
                                    getch();

                        case 3:
                                    printf("\n Enter the scalling factor");
                                    scanf("%d%d",&sx,&sy);
                                    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);
                                    getch();

                        case 4:
                                    break;
                        default:
                                    printf("Enter the correct choice");
                                    }
                                    closegraph();
                                    }
=32=

Sample Output:

Discussion: 

This program is to implement 2D Transformations (Translation, Rotation)


algorithm.

You might also like