0% found this document useful (0 votes)
28 views42 pages

CG LAB Practical - Final Record - New

The document outlines practical exercises for a Computer Graphics course at Karpagam Academy of Higher Education, including algorithms for drawing lines, circles, character generation, and polygon clipping. Each exercise includes an aim, algorithm steps, coding examples in C, and results confirming successful execution. The document serves as a practical record for students in the III B.Sc. Computer Technology program.

Uploaded by

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

CG LAB Practical - Final Record - New

The document outlines practical exercises for a Computer Graphics course at Karpagam Academy of Higher Education, including algorithms for drawing lines, circles, character generation, and polygon clipping. Each exercise includes an aim, algorithm steps, coding examples in C, and results confirming successful execution. The document serves as a practical record for students in the III B.Sc. Computer Technology program.

Uploaded by

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

KARPAGAM ACADEMY OF HIGHER EDUCATION

(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.

DEPARTMENT OF COMPUTER TECHNOLOGY


COMPUTER GRAPHICS – PRACTICAL
III B.Sc COMPUTER TECHNOLOGY

NAME :

REG.NO:
KARPAGAM ACADEMY OF HIGHER EDUCATION
(Deemed to be University)
(Established Under section 3 of UGC act 1956)
Accredited with A+ Grade by NAAC in the Second Cycle
Coimbatore-641021.

DEPARTMENT OF COMPUTER TECHNOLOGY

BONAFIDE CERTIFICATE

REGISTER NUMBER:

This is to certify that this is a bonafide record work done by


_______________________________of III B.Sc. (Computer Technology) during the
academic year 2024- 2025 and submitted for the practical examination in
COMPUTER GRAPHICS – PRACTICAL (21CTU612A)held on
______________.

Staff-in-charge Head of the Department

Internal Examiner External Examiner


INDEX

STAFF
PAGENO
S.NO DATE NAME OF THE EXPERIMENT SIGNATURE

1. Program to draw a line using DDA algorithm

2. Program to draw a line using Bresenham’s


algorithm.

3. Program to draw a circle using Bresenham's


algorithm.

4. Program to implement the Character


generation algorithm.

5. Program to implement the Polygon clipping


algorithm.

6. Program to implement the Text clipping


algorithm.

7. Program to implement the line Clipping


algorithm

8. Program to implement the 2D Translation, 2D


Rotation and 2D scaling.

9. Program to implement the 3D Translation, 3D


Rotation and 3D scaling.

10. Program to implement the Shearing and


Reflection of an object.
Ex.No. : 1
Date :
DDA LINE DRAWING ALGORITHM

Aim:

To draw a straight line using DDA algorithm

Algorithm:

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2-x1

Step5: Calculate dy = y2-y1

Step6: If ABS (dx) > ABS (dy)

Then step = abs (dx)

Else

Step7: xinc=dx/step

yinc=dy/step

assign x = x1

assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc

y = y + yinc

Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until x = x2

Step11: End Algorithm.


Coding:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(WHITE);
printf("Enter coordinates for 1st point of line:");
scanf("%d%d",&x0,&y0);
printf("Enter coordinates for 2nd point of line:");
scanf("%d%d",&x1,&y1);
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
Output:

Result:

Thus the program to draw the line is successfully coded, compiled and then executed successfully.
Ex.No. : 2
Date :
BRESENHAM'S LINE DRAWING ALGORITHM

Aim :

To draw a line using Bresenham's algorithm in C programming

Algorithm:

Step1: Start Algorithm

Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy

Step3: Enter value of x1,y1,x2,y2

Where x1,y1are coordinates of starting point

And x2,y2 are coordinates of Ending point

Step4: Calculate dx = x2-x1

Calculate dy = y2-y1

Calculate i1=2*dy

Calculate i2=2*(dy-dx)

Calculate d=i1-dx

Step5: Consider (x, y) as starting point and xendas maximum possible value of x.

If dx < 0

Then x = x2

y = y2

xend=x1

If dx > 0

Then x = x1

y = y1

xend=x2

Step6: Generate point at (x,y)coordinates.

Step7: Check if whole line is generated.

If x > = xend

Stop.

Step8: Calculate co-ordinates of the next pixel


If d < 0

Then d = d + i1

If d ≥ 0

Then d = d + i2

Increment y = y + 1

Step9: Increment x = x + 1

Step10: Draw a point of latest (x, y) coordinates

Step11: Go to step 7

Step12: End of Algorithm

Coding:

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

Output :

Result:

Thus the program to draw the line is successfully coded, compiled and then executed successfully.
Ex.No. : 3
Date :
BRESENHAM'S CIRCLE DRAWING ALGORITHM

Aim:

To draw a circle using bresenham’S algorithm for circle using C program.

Algorithm:

Step1: Start Algorithm

Step2: Declare p, q, x, y, r, d variables

p, q are coordinates of the center of the circle

r is the radius of the circle

Step3: Enter the value of r

Step4: Calculate d = 3 - 2r

Step5: Initialize x=0

&nbsy= r

Step6: Check if the whole circle is scan converted

If x > = y

Stop

Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p, q). Current active
pixel is (x, y).

putpixel (x+p, y+q)


putpixel (y+p, x+q)
putpixel (-y+p, x+q)
putpixel (-x+p, y+q)
putpixel (-x+p, -y+q)
putpixel (-y+p, -x+q)
putpixel (y+p, -x+q)
putpixel (x+p, -y-q)
Step8: Find location of next pixels to be scanned
If d < 0

then d = d + 4x + 6

increment x = x + 1

If d ≥ 0

then d = d + 4 (x - y) + 10

increment x = x + 1
decrement y = y - 1

Step9: Go to step 6

Step10: Stop Algorithm

Coding:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void EightWaySymmetricPlot(int xc,intyc,intx,int y)
{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}
void BresenhamCircle(int xc,intyc,int r)
{
int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);
getch();
closegraph();
return 0;
}

Output:

Result:

Thus the program to draw the circle is successfully coded, compiled and then executed successfully.
Ex.No. : 4
Date :
CHARACTER GENERATION ALGORITHM

Aim:

To generate a character in C program

Algorithm:

Step 1: Start the algorithm

Step 2: Initialize the required variables and graphics

Step 3: Inside the loop

a. print the pixels

b. repeat the step a till the loop ends

Step 4: The character 'A' will be generated after the loop ends

Step 5: Stop the algorithm

Coding:

//***PROGRAM FOR CHARACTER GENERATION***//


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int a[7][7]={{0,0,0,0,0,0,0},{0,0,0,0,0,0,0,},{0,0,1,1,1,1},{0,1,0,0,1,0,0},{0,1,0,0,1,0,0},
{0,0,1,1,1,1}};
int i,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
putpixel(200+i,200+j,5*a[i][j]);
}
}
getch();
closegraph();
}
Output:

(Zoomed)

Result:

Thus the program to draw the character is successfully coded, compiled and then executed
successfully.
Ex.No. : 5
Date :
POLYGON CLIPPING USING SUTHERLAND–HODGMAN ALGORITHM

Aim:

To perform polygon clipping using Sutherland–Hodgman Algorithm in C program

Algorithm:

Step 1: Start the Algorithm

Step 2: Feed the input for polygon and input area

Step 3: Consider each edge e of clipping Area and do following:

a) Clip given polygon against e.

Step 4: Check the four possible cases for any given edge of given polygon against current clipping edge e.

a. Both vertices are inside

b. First vertex is outside while second one is inside

c. First vertex is inside while second one is outside

d. Both vertices are outside

Step 5: Print the clipped output values

Step 6: Stop the Algorithm

Coding:

// C++ program for implementing Sutherland–Hodgman


// algorithm for polygon clipping
#include<iostream>
using namespace std;
const int MAX_POINTS = 20;

// Returns x-value of point of intersection of two


// lines
int x_intersect(int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int num = (x1*y2 - y1*x2) * (x3-x4) -
(x1-x2) * (x3*y4 - y3*x4);
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
return num/den;
}
// Returns y-value of point of intersection of
// two lines
int y_intersect(int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int num = (x1*y2 - y1*x2) * (y3-y4) -
(y1-y2) * (x3*y4 - y3*x4);
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
return num/den;
}
// This functions clips all the edges w.r.t one clip
// edge of clipping area
void clip(int poly_points[][2], int &poly_size,
int x1, int y1, int x2, int y2)
{
int new_points[MAX_POINTS][2], new_poly_size = 0;
// (ix,iy),(kx,ky) are the co-ordinate values of
// the points
for (int i = 0; i<poly_size; i++)
{
// i and k form a line in polygon
int k = (i+1) % poly_size;
int ix = poly_points[i][0], iy = poly_points[i][1];
int kx = poly_points[k][0], ky = poly_points[k][1];
// Calculating position of first point
// w.r.t. clipper line
int i_pos = (x2-x1) * (iy-y1) - (y2-y1) * (ix-x1);
// Calculating position of second point
// w.r.t. clipper line
int k_pos = (x2-x1) * (ky-y1) - (y2-y1) * (kx-x1);
// Case 1 : When both points are inside
if (i_pos<0 &&k_pos< 0)
{
//Only second point is added
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}
// Case 2: When only first point is outside
else if (i_pos>= 0 &&k_pos< 0)
{
// Point of intersection with edge
// and the second point is added
new_points[new_poly_size][0] = x_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_points[new_poly_size][1] = y_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_poly_size++;

new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}
// Case 3: When only second point is outside
else if (i_pos<0 &&k_pos>= 0)
{
//Only point of intersection with edge is added
new_points[new_poly_size][0] = x_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_points[new_poly_size][1] = y_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_poly_size++;
}
// Case 4: When both points are outside
else
{
//No points are added
}
}
// Copying new points into original array
// and changing the no. of vertices
poly_size = new_poly_size;
for (int i = 0; i<poly_size; i++)
{
poly_points[i][0] = new_points[i][0];
poly_points[i][1] = new_points[i][1];
}
}

// Implements Sutherland–Hodgman algorithm


void suthHodgClip(int poly_points[][2], int poly_size,
int clipper_points[][2], int clipper_size)
{
//i and k are two consecutive indexes
for (int i=0; i<clipper_size; i++)
{
int k = (i+1) % clipper_size;
// We pass the current array of vertices, it's size
// and the end points of the selected clipper line
clip(poly_points, poly_size, clipper_points[i][0],
clipper_points[i][1], clipper_points[k][0],
clipper_points[k][1]);
}
// Printing vertices of clipped polygon
for (int i=0; i<poly_size; i++)
cout<< '(' <<poly_points[i][0] <<
", " <<poly_points[i][1] << ") ";
}
//Driver code
int main()
{
// Defining polygon vertices in clockwise order
int poly_size = 3;
int poly_points[20][2] = {{100,150}, {200,250},
{300,200}};
// Defining clipper polygon vertices in clockwise order
// 1st Example with square clipper
int clipper_size = 4;
int clipper_points[][2] = {{150,150}, {150,200},
{200,200}, {200,150} };
// 2nd Example with triangle clipper
/*int clipper_size = 3;
int clipper_points[][2] = {{100,300}, {300,300},
{200,100}};*/
//Calling the clipping function
suthHodgClip(poly_points, poly_size, clipper_points,
clipper_size);
return 0;
}

Output:
(150, 162) (150, 200) (200, 200) (200, 174)

Result:

Thus the program for polygon clipping is successfully coded, compiled and then executed
successfully.
Ex.No. : 6
Date :
TEXT CLIPPING ALGORITHM

Aim:

To perform the text clipping operation using C program

Algorithm:

Step 1: Start the algorithm

Step 2: Initialize the graphics and view points

Step 3: Chose any of the below clipping type

a. All or none string clipping

b. All or none character clipping

c. Text Clipping

Step 4: Do the necessary steps to clip the unwanted portion of the text

Step 5: Print the clipped text

Step 6: Stop the Algorithm

Coding:

//text clipping

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(val)((int)(val+.5))
int maxx, maxy, miny, minx;
void main()
{
int gd = DETECT, gm;
void clipping(int xa, int ya, int xb, int y);
int xa, xb, ya, yb;
printf("Enter the window coordination");
//code - works
scanf("%d%d%d%d", &minx, &maxy, &maxx, &miny);
printf("Enter the two string");
scanf("%s%s”, &xa, &ya);
initgraph(&gd, &gm, "");
rectangle(minx, miny, maxx, maxy);
line(xa, ya, xb, yb);
getch();
closegraph();
}
void clipping(int xa, int ya, int xb, int yb)
{
int Dx = xb - xa, Dy = yb - ya, steps, k;
int visible1 = 0, visible2 = 0;
float xin, yin, x = xa, y = ya;
if (abs(Dx) > abs(Dy))
steps = abs(Dx);
else// NO
steps = abs(Dy);
xin = Dx / (float) steps;
yin = Dy / (float) steps;
putpixel(Round(x), Round(y), 2);
for (k = 0; k < steps; k++) {
x += xin;
y += yin;
if ((y >miny&& y <maxx)) {
visible1 = 1;
putpixel(Round(x), Round(y), 2);
} else
visible2 = 1;
}//works
if (visible1 == 0)
outtextxy(20, 200, "completely visible");
if (visible1 == 1 && visible2 == 1)
outtextxy(20, 20, "partialy visible");

}
Output:

Result:

Thus the program to clip the text is succesfully coded, compiled and then executed successfully.
Ex.No. : 7
Date :
LINE CLIPPING ALGORITHM

Aim :

To perform the line clipping algorithm using Cohen Sutherland in C program.

Algorithm:

Step 1: Start the algorithm

Step 2: Initialize the required variables and graphics

Step 3: Get the inputs for clipping area and line position

Step 4: Check for the below possibilities

a. Completely invisible

b. Completely visible

c. Partially visible

Step 5: Clip according to the above said possibility

Step 6: Print the clipped line

Step 6: Stop the program

Coding:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(val)((int)(val+.5))
int maxx, maxy, miny, minx;
void main()
{
int gd = DETECT, gm;
void clipping(int xa, int ya, int xb, int y);
int xa, xb, ya, yb;
printf("Enter the window coordination");
scanf("%d%d%d%d", &minx, &maxy, &maxx, &miny);
printf("Enter the two end points for the line");
scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
initgraph(&gd, &gm, "");
rectangle(minx, miny, maxx, maxy);
line(xa, ya, xb, yb);
getch();
closegraph();
}
void clipping(int xa, int ya, int xb, int yb)
{
int Dx = xb - xa, Dy = yb - ya, steps, k;
int visible1 = 0, visible2 = 0;
float xin, yin, x = xa, y = ya;
if (abs(Dx) > abs(Dy))
steps = abs(Dx);
else
steps = abs(Dy);
xin = Dx / (float) steps;
yin = Dy / (float) steps;
putpixel(Round(x), Round(y), 2);
for (k = 0; k < steps; k++) {
x += xin;
y += yin;
if ((y >miny&& y <maxx)) {
visible1 = 1;
putpixel(Round(x), Round(y), 2);
} else
visible2 = 1;
}
if (visible1 == 0)
outtextxy(20, 200, "completely visible");
if (visible1 == 1 && visible2 == 1)
outtextxy(20, 20, "partialy visible");
if (visible1 == 1 && visible2 == 0)
outtextxy(20, 20, "completely visible");
}

Output:

Before clipping the line


After Clipping the line

Result:

Thus the program to clip the line is successfully coded, compiled and then executed successfully.
Ex.No. : 8
Date :
2-D BASIC TRANSFORMATIONS

Aim:

To perform 2-D basic transformations such as translation, rotation and scaling using C program

Algorithm:

Step 1: Start the Algorithm

Step 2: Initialize necessary variables and graphics

Step 3: In translation transformation, ask for a choice, as below

a. line , b. rectangle and c. circle

Step 4: In rotation transformation, triangle object is used

Step 5: In scaling transformation, line is used

Step 6. Do the necessary steps to perform translation, rotation and scaline

Step 7: Stop the algorithm

Coding:

//***PROGRAM FOR TRANSLATION*****//

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void circlemid(int,int,int);
void circlepoint(int,int,int,int);
int r,x1,x2,y1,y2,x3,y3,x4,y4;
int ch;
int x,y;
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
{
cleardevice();
do
{
clrscr();
printf("TRANSLATION\n");
printf("PRESS 1 FOR LINE\n");
printf("PRESS 2 FOR RECTANGALE\n");
printf("PRESS 3 FOR CIRCLE\n");
printf("PRESS ANY KEY TO EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x1,&y1);
printf("ENTER THE ENDPOINT");
scanf("%d%d",&x2,&y2);
setcolor(RED);
line(x1,y1,x2,y2);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x,&y);
setcolor(RED);
x3 = x1+x;
x4 = x2+x;
y3 = y1+y;
y4 = y2+y;
line(x3,y3,x4,y4);
getch();
break;
case 2:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x1,&y1);
printf("ENTER THE ENDPOINT");
scanf("%d%d",&x2,&y2);
setcolor(RED);
rectangle(x1,y1,x2,y2);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x,&y);
setcolor(RED);
x3 = x1+x;
x4 = x2+x;
y3 = y1+y;
y4 = y2+y;
rectangle(x3,y3,x4,y4);
getch();
break;
case 3:
printf("ENTER STARING POINT\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle");
scanf("%d",&r);
setcolor(RED);
circlemid(x,y,r);
printf("ENTER TRANSLATION FACTOR");
scanf("%d%d",&x1,&y1);
x=x+x1;
y=y+y1;
circlemid(x,y,r);
setcolor(RED);
getch();
break;
default:
exit(0);
}
}
while(ch!=0);
getch();
closegraph();
}
}
void circlemid(int xc,intyc,intrd)
{
int x =0;
int y =rd;
int p = 1-rd;
while(x<y)
{
x++;
if(p<0)
p = p+2*x+1;
else
{
y--;
p = p+2*(x-y)+1;
}
circlepoint(xc,yc,x,y);
delay(200);
}
}
void circlepoint(int xc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-y,yc-x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-x,yc-y,1);
}

//Program for Rotation


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
triangle(a1,b1,a2,b2,a3,b3);
}

//Program for scaling


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,x,y,ch;
char p[10];
int i,t;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(BLUE);
outtextxy(480,400,"x");
outtextxy(150,90,"y");
line(150,100,150,400);
line(150,400,470,400);
outtextxy(140,405,"0");
for(i=0,t=25;i<12;i++)
{
itoa(i+1,p,10);
outtextxy(150+t,410,p);
line(150+t,400,150+t,405);
itoa(i+1,p,10);
outtextxy(130,400-t,p);
line(150,400-t,145,400-t);
t+=25;
}
setcolor(5);
line(250,200,350,200);
line(250,200,300,300);
line(300,300,350,200);
x=(250+350+300)/3;
y=(200+200+300)/3;
putpixel(x,y,10);
printf("Enter your choice");
printf("\npress 1: fixed point Traslation");
printf("\npress 2: fixed point Scaling");
printf("\nchoice=");
scanf("%d",&ch);
switch(ch)
{
case 1: setcolor(RED);
setlinestyle(2,0,2);
line(150,467,200,367);
line(100,367,150,467);
line(100,367,200,367);
getch();
setbkcolor(RED);
setcolor(YELLOW);
line(200,350,150-67,400);
line(200,350,200,450);
line(200,450,150-67,400);
setcolor(YELLOW);
getch();
setbkcolor(RED);
line(300-67,233,350,183);
line(350,183,350,283);
line(350,283,300-67,233);
break;
case 2: setcolor(RED);
setlinestyle(2,0,2);
line(150,467,200,367);
line(100,367,150,467);
line(100,367,200,367);
getch();
setcolor(YELLOW);
line(150,447,180,377);
line(180,377,120,377);
line(120,377,150,447);
setbkcolor(RED);
getch();
setcolor(YELLOW);
line(270,210,330,210);
line(270,210,300,280);
line(300,280,330,210);
getch();
break;
default : exit(0);
break;
}
getch();
}
Output:

Result:

Thus the program to perform the basic operations on 2-D object are successfully coded, compiled
and then executed successfully.
Ex.No. : 9
Date :
3-D BASIC TRANSFORMATIONS

Aim:

To perform 3-D basic transformations such as translation, rotation and scaling using C program

Algorithm:

Step 1: Start the Algorithm

Step 2: Initialize necessary variables and graphics

Step 3: Feed the coordinates according to the object needed

Step 4: Draw the object

Step 5. Do the necessary steps to perform translation, rotation and scaline

Step 6: Draw the object after the operation

Step 7: Stop the algorithm

Coding:

/*3D Translation Program In C Programming*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

int x1, x2, y1, y2, mx, my, depth;

void draw();

void trans();

void main()

int gd = DETECT, gm, c;

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

printf("\n\t\t3D Transmission\n\n");

printf("\nEnter 1st top value(x1,y1):");

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


printf("Enter right bottom value(x2,y2):");

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

depth = (x2 - x1) / 4;

mx = (x1 + x2) / 2;

my = (y1 + y2) / 2;

draw();

getch();

cleardevice();

trans();

getch();

void draw() {

bar3d(x1, y1, x2, y2, depth, 1);

void trans() {

int a1, a2, b1, b2, dep, x, y;

printf("\n Enter the TtransitionCo ordinates:");

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

a1 = x1 + x;

a2 = x2 + x;

b1 = y1 + y;

b2 = y2 + y;

dep = (a2 - a1) / 4;

bar3d(a1, b1, a2, b2, dep, 1);

setcolor(5);

draw();

/*3D Rotation Program In C*/

#include<stdio.h>
#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

int x1, x2, y1, y2, mx, my, depth;

void draw();

void rotate();

void main()

int gd = DETECT, gm, c;

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

printf("\n3D Transformation Rotating\n\n");

printf("\nEnter 1st top value(x1,y1):");

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

printf("Enter right bottom value(x2,y2):");

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

depth = (x2 - x1) / 4;

mx = (x1 + x2) / 2;

my = (y1 + y2) / 2;

draw();

getch();

cleardevice();

rotate();

getch();

void draw() {

bar3d(x1, y1, x2, y2, depth, 1);

void rotate()
{

float t;

int a1, b1, a2, b2, dep;

printf("Enter the angle to rotate=");

scanf("%f", &t);

t = t * (3.14 / 180);

a1 = mx + (x1 - mx) * cos(t)-(y1 - my) * sin(t);

a2 = mx + (x2 - mx) * cos(t)-(y2 - my) * sin(t);

b1 = my + (x1 - mx) * sin(t)-(y1 - my) * cos(t);

b2 = my + (x2 - mx) * sin(t)-(y2 - my) * cos(t);

if (a2 > a1)

dep = (a2 - a1) / 4;

else

dep = (a1 - a2) / 4;

bar3d(a1, b1, a2, b2, dep, 1);

setcolor(5);

//draw();

/*3D Scaling Example Program In C*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

int x1, x2, y1, y2, mx, my, depth;

void draw();

void scale();

void main() {

int gd = DETECT, gm, c;


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

printf("\n\t\t3D Transformation Scalling\n\n");

printf("\nEnter 1st top value(x1,y1):");

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

printf("Enter right bottom value(x2,y2):");

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

depth = (x2 - x1) / 4;

mx = (x1 + x2) / 2;

my = (y1 + y2) / 2;

draw();

getch();

cleardevice();

scale();

getch();

void draw() {

bar3d(x1, y1, x2, y2, depth, 1);

void scale() {

int x, y, a1, a2, b1, b2, dep;

printf("\n\n Enter scalling co-ordinates:");

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

a1 = mx + (x1 - mx) * x;

a2 = mx + (x2 - mx) * x;

b1 = my + (y1 - my) * y;

b2 = my + (y2 - my) * y;

dep = (a2 - a1) / 4;

bar3d(a1, b1, a2, b2, dep, 1);

setcolor(5);
draw();

Output:

Result:

Thus the program to perform the basic operations on 3-D object are successfully coded, compiled
and then executed successfully.
Ex.No. : 10
Date :
SHEARING AND REFLECTION OF AN OBJECT

Aim:

To perform shearing and refelction of an object using CPP program

Algorithmfor Shearing

Step 1: Start the Algorithm

Step 2: Initialize necessary variables and graphics

Step 3: In shearing transformation, ask for a choice, as below

a. line , b. rectangle and c. triangle

Step 6. Do the necessary steps to perform shearing operation on the chosen object

Step 7: Stop the algorithm

Coding:

//*******PROGRAM FOR SHEARING******// CPP program

//Line Rectangle and Triangle

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
int x1,y1,x2,y2,x,y,x3,y3,x4,y4,ch;
void main()
{
int gd = DETECT,gm,errorcode;
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode = graphresult();
if(errorcode!=grOk)
{
printf("Graphics error:%s\n",grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
exit(1);
}
do
{
clrscr();
cout<<" #############MAIN-MENU###############\n";
cout<<" SHEARING\n";
cout<<" 1.LINE\n";
cout<<" 2.RECTANGLE\n";
cout<<" 3.TRIANGLE\n";
cout<<"enter your choice:0 for exit:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the value of line coordinates:";
cin>>x1>>y1>>x2>>y2;
cout<<"enter the value of shearing for x-axis:";
cin>>x;
cout<<"enter the value of shearing for y-axis:";
cin>>y;
cleardevice();
setcolor(4);
line(x1,y1,x2,y2);
cout<<"now hit a key to see shear in x_axis:";
getch();
setcolor(1);
line(x1,y1,x2*x,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(58);
line(x1,y1,x2,y2*y);
getch();
break;
case 2:
cout<<"enter the top left coordinates:";
cin>>x1>>y1;
cout<<"enter the bottom right coordinates:";
cin>>x2>>y2;
cout<<"enter the value of shearing coordinates for x-shear:";
cin>>x;
cout<<"enter the value of shearing coordinates for y-shear:";
cin>>y;
cleardevice();
setcolor(5);
rectangle(x1,y1,x2,y2);
cout<<"now hit a key to see shear in x_axis:";
getch();
setcolor(1);
rectangle(x1,y1,x2+x*y2,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(58);
rectangle(x1,y1,x2,y2+y*x2);
getch();
break;
case 3:
cout<<"enter the coordinates of triangle:\n";
cin>>x1>>y1>>x2>>y2;
cin>>x3>>y3;
cout<<"enter the value of shearing coordinates for x-shear:";
cin>>x;
cout<<"enter the value of shearing coordinates for y-shear:";
cin>>y;
cleardevice();
setcolor(5);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
cout<<"\nnow hit a key to see shear in x_axis:";
getch();
setcolor(10);
line(x1,y1,x2*x,y2);
line(x1,y1,x3*x,y3);
line(x3*x,y3,x2*x,y2);
cout<<"\nnow hit a key to see shear in y_axis:";
getch();
setcolor(1);
line(x1,y1,x2,y2*y);
line(x1,y1,x3,y3*y);
line(x3,y3*y,x2,y2*y);
getch();
break;
case0:break;
default:cout<<"invalid choice";break;
}}while(ch!=0);
closegraph();
getch();
}
Output:

Algorithm for Reflection:

Step 1: Start the algorithm.

Step 2: Draw a line in graphics to act as a Y-axis, by passing along 4 values as parameters of the line()
function as line(getmaxx()/2, 0, getmaxx()/2, getmaxy()).

Step 3: Draw a line in graphics to act as X-axis by passing along 4 values as parameters of the line() function
as line(0, getmaxy()/2, getmaxx(), getmaxy()/2).

Step 4: Draw an object using the line() function with parameters from set variables.

Step 5: Perform reflection and draw the object along origin using Step 3 of the technique and color it red to
distinguish it from other objects.

Step 6: Perform reflection and draw the object along the X-axis using Step 1 of the technique and color it
cyan to distinguish it from other objects.
Step 7: Perform reflection and draw the object along the Y-axis using Step 2 of the technique and color it
green to distinguish it from other objects.

Step 8: Stop the algorithm.

Coding for reflection:

// C program for the above approach


#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
{
// Initialize the drivers
int gm, gd = DETECT, ax, x1 = 100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;
// Add in your BGI folder path
// like below initgraph(&gd, &gm,
// "C:\\TURBOC3\\BGI");
initgraph(&gd, &gm, "");
cleardevice();
// Draw the graph
line(getmaxx() / 2, 0, getmaxx() / 2,
getmaxy());
line(0, getmaxy() / 2, getmaxx(),
getmaxy() / 2);
// Object initially at 2nd quadrant
printf("Before Reflection Object"
" in 2nd Quadrant");
// Set the color
setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
// After reflection
printf("\nAfter Reflection");
// Reflection along origin i.e.,
// in 4th quadrant
setcolor(4);
line(getmaxx() - x1, getmaxy() - y1,
getmaxx() - x2, getmaxy() - y2);
line(getmaxx() - x2, getmaxy() - y2,
getmaxx() - x3, getmaxy() - y3);
line(getmaxx() - x3, getmaxy() - y3,
getmaxx() - x1, getmaxy() - y1);
// Reflection along x-axis i.e.,
// in 1st quadrant
setcolor(3);
line(getmaxx() - x1, y1,
getmaxx() - x2, y2);
line(getmaxx() - x2, y2,
getmaxx() - x3, y3);
line(getmaxx() - x3, y3,
getmaxx() - x1, y1);
// Reflection along y-axis i.e.,
// in 3rd quadrant
setcolor(2);
line(x1, getmaxy() - y1, x2,
getmaxy() - y2);
line(x2, getmaxy() - y2, x3,
getmaxy() - y3);
line(x3, getmaxy() - y3, x1,
getmaxy() - y1);
getch();
// Close the graphics
closegraph();
}

Output

Result:

Thus the program to perform the shearing and reflection operations on 2-D object are successfully
coded, compiled and then executed successfully.

You might also like