0% found this document useful (0 votes)
19 views64 pages

CG Lab Manual

cg lab manual

Uploaded by

sayedzubia2211
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)
19 views64 pages

CG Lab Manual

cg lab manual

Uploaded by

sayedzubia2211
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

NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 R.

NO:72

Expt. No. 1

Title: Implement DDA Line Drawing algorithm (dotted/dashed/thick)

Performed On: ___________

Sign: __________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 1

Aim : Implement DDA Line Drawing algorithm


(dotted/dashed/thick)

Algorithm:
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also
declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC
folder.
4. Input the two line end-points and store the left end-points in
(x1,y1).
5. Load (x1,y1) into the frame buffer;that is,plot the first
[Link] x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do s=abs(dx).
8. Otherwise s= abs(dy).
9. Then xi=dx/s and yi=dy/s.
10. Start from k=0 and continuing till k<s,the points will be
i. x=x+xi.
ii. y=y+yi.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

11. Place pixels using putpixel at points (x,y) in specified


colour.
12. Close Graph.
13. Stop.

PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main() {
int x, y, x1, x2, y1, y2, k, dx, dy, s, xi, yi;
int gdriver = DETECT, gmode;
// Initialize graphics mode
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\bgi");
// Input the coordinates of the two points
printf("Enter first point (x1 y1): ");
scanf("%d%d", &x1, &y1);
printf("Enter second point (x2 y2): ");
scanf("%d%d", &x2, &y2);
// Initialize starting point
x = x1;
y = y1;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

putpixel(x, y, WHITE); // Plot the starting point


// Calculate differences and steps
dx = x2 - x1;
dy = y2 - y1;
if (abs(dx) > abs(dy))
s = abs(dx);
else
s = abs(dy);
// Calculate the increments for x and y
xi = dx / s;
yi = dy / s;
// Draw the line
for (k = 0; k <= s; k++) {
putpixel(x, y, WHITE); // Plot each point on the line
x = x + xi; // Move to the next point
y = y + yi;
}
// Wait for user input before closing the graphics window
getch();
closegraph();
}
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

OUTPUT:-

Conclusion: Implemented DDA Line Drawing algorithm


successfully
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 2

Title: Implement Bradenham’s Line


algorithm(dotted/dashed/thick)

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 2
Aim: Implement Bradenham’s Line
algorithm(dotted/dashed/thick)

Program :
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main() {
int x, y, x1, y1, x2, y2, p, dx, dy;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\turboc3\\BGI");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d", &x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d", &y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d", &x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d", &y2);
x = x1;
y = y1;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

dx = x2 - x1;
dy = y2 - y1;
putpixel(x, y, 2); // Starting point in color 2 (typically green)
p = 2 * dy - dx; // Initial decision parameter
while (x <= x2) {
putpixel(x, y, 7); // Plot pixel in color 7 (typically white)
if (p < 0) {
x = x + 1; // Move to the next x
p = p + 2 * dy; // Update decision parameter
} else {
x = x + 1; // Move to the next x
y = y + 1; // Move to the next y
p = p + 2 * (dy - dx); // Update decision parameter
}
}
getch(); // Wait for user input
closegraph(); // Close graphics mode
}

Output:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Conclusion: Implemented Bradenham’s Line


algorithm(dotted/dashed/thick) successfully.

Expt. No. 3

Title: Implement midpoint Circle algorithm

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 3

Aim: implement midpoint circle algorithm


Theroy:
Here are some key points about the midpoint circle algorithm:

- *Efficiency*: It uses integer arithmetic instead of floating-


point calculations, making it faster and more efficient.
- *Symmetry*: The algorithm leverages the symmetry of circles,
calculating points for one octant and mirroring them across the
remaining seven.
- *Incremental Calculation*: The next point on the circle is
determined using an incremental decision parameter,
minimizing complex calculations.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

- *Accuracy*: Ensures accurate and smooth rendering of circles


by carefully managing the placement of each pixel.
- *Applications*: Widely used in graphics programming for
drawing circles in games, design tools, and visual simulations.

Algorithm:
1. Start.
2. Declare variables x,y,p and also declare
gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC
folder.
4. Input the radius of the circle r.
5. Load x-0,y=r,initial decision parameter p=[Link] the first point
is (0,r).
6. Repeat Step 7 while (x<y) and increment x-value
simultaneously. 7. If (p>0),do p=p+2*(x-y)+1.
8. Otherwise p=p+2*x+1 and y is decremented simultaneously.
9. Then calculate the value of the function circlepoints() with
[Link] (x,y).
10. Place pixels using putpixel at points (x+300,y+300) in
specified colour in
circlepoints() function shifting the origin to 300 on both x-axis
and y-axis.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

11. Close Graph.


12. Stop.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int, int);
void main() {
int x, y, p, r;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\tc\\bgi");
clrscr();
printf("Enter the radius: ");
scanf("%d", &r);

x = 0;
y = r;
p = 1 - r;
while (x < y) {
x++;
if (p > 0) {
p = p + 2 * (x - y) + 1;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

y--;
} else {
p = p + 2 * x + 1;
}
circlepoints(x, y);
}

getch();
closegraph();
}
void circlepoints(int x, int y) {
putpixel(x + 300, y + 300, 8);
putpixel(x + 300, -y + 300, 8);
putpixel(-x + 300, y + 300, 8);
putpixel(-x + 300, -y + 300, 8);
putpixel(y + 300, x + 300, 8);
putpixel(y + 300, -x + 300, 8);
putpixel(-y + 300, x + 300, 8);
putpixel(-y + 300, -x + 300, 8);
}
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Output:

Conclusion:
The midpoint circle algorithm efficiently draws circles by
calculating points using decision parameters, minimizing
computation. This method plots symmetrical points around the
circle's center, ensuring smooth and accurate rendering in
graphics programming.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 4

Title: Implementation of midpoint Ellipse algorithm

Performed On: ___________

Sign:__
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 4
Aim: Implement midpoint ellipse algorithm using C Theroy:
Mid-point Ellipse algorithm is used to draw an ellipse in
computer graphics.
Midpoint ellipse algorithm plots(finds) points of an ellipse on
the first quadrant by dividing the quadrant.
Into two regions. Each point(x, y) is then projected into other
three quadrants (-x, y), (x, -y), (-x,-y) i.e. It uses 4-way
symmetry.
This is an incremental method for scan converting an ellipse that
is centered at the origin in standard position i.e., with the major
and minor axis parallel to coordinate system axis.
It is very similar to the midpoint circle algorithm. Because of the
four-way symmetry property we need to
Consider the entire elliptical curve in the first quadrant.
Algorithm:
Step 1: float p,x,y,xc,yc,a,b;
Step 2:int gd=DETECT,gm; initgraph;
Step 3: Enter coordinate of centre of ellipse as (xc,yc)
Step 4: Enter Major and Minor Axis of ellipse as (a=major and
b=minor)
Step 5: Calculate Decision parameter for Region 1:
P=p+2*b*b*x-2*a*b*b*y+b*b
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Step 6:
If(p<0)
{
X=x+1;
P=p+2*b*b*x+b*b;
}
Else
{
X=x+1;
Y=y-1;
P=p+2*b*b*x-2*a*a*y+b*b;
}
Step 7: Calculate Decision parameter for Region 2:
P=(b*b*(x+0.5)(x+0.5))+((y-1)(y-1)*a*a-a*a*b*b);
Step 8:
{
Y=y-1;
P=p-2*a*a*y+a*a;
}
Else
{
X=x+1;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Y=y-1;
P=p-2*a*a*y+2*b*b*x+a*a;
}
Step 9:Closegraph();
Step 10:Stop
Program:
#include<stdio.h>
#include<graphics.h>
Void main() {
Int gdriver = DETECT, gmode;
Float p, x, y, xc, yc, a, b;
Initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");

Printf("Enter xc:\n");
Scanf("%f", &xc);
Printf("Enter yc:\n");
Scanf("%f", &yc);
Printf("Enter a:\n");
Scanf("%f", &a);
Printf("Enter b:\n");
Scanf("%f", &b);
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

X = 0;
Y = b;

// Region 1
P = (b * b) - (a * a * b) + (0.25 * a * a);
Do {
Putpixel(xc + x, yc + y, WHITE);
Putpixel(xc + x, yc - y, WHITE);
Putpixel(xc - x, yc + y, WHITE);
Putpixel(xc - x, yc - y, WHITE);
If (p < 0) {
X = x + 1;
P = p + 2 * b * b * x + b * b;
} else {
X = x + 1;
Y = y - 1;
P = p + 2 * b * b * x - 2 * a * a * y + b * b;
}
} while (2 * b * b * x < 2 * a * a * y);

// Region 2
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

P = (b * b * (x + 0.5) * (x + 0.5)) + ((y - 1) * (y - 1) * a * a - a


* a * b * b);
Do {
Putpixel(xc + x, yc + y, WHITE);
Putpixel(xc + x, yc - y, WHITE);
Putpixel(xc - x, yc + y, WHITE);
Putpixel(xc - x, yc - y, WHITE);
If (p > 0) {
Y = y - 1;
P = p - 2 * a * a * y + a * a;
} else {
X = x + 1;
Y = y - 1;
P = p - 2 * a * a * y + 2 * b * b * x + a * a;
}
} while (y != 0);

Getch();
Closegraph();
}
Void circlepoints(int x, int y) {
Putpixel(x + 300, y + 300, 8);
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Putpixel(x + 300, -y + 300, 8);


Putpixel(-x + 300, y + 300, 8);
Putpixel(-x + 300, -y + 300, 8);
Putpixel(y + 300, x + 300, 8);
Putpixel(y + 300, -x + 300, 8);
Putpixel(-y + 300, x + 300, 8);
Putpixel(-y + 300, -x + 300, 8);
}
Output:

Conclusion:
Thus We Midpoint Ellipse Algorithm Implemented Using C
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 5

Title: Implement Area Filling Algorithm: Boundary Fill, Flood


Fill.

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 5
Aim: Boundary fill algorithm
Theory: Boundary Fill Algorithm starts at a pixel inside the
polygon to be filled and paints
the interior proceeding outwards towards the boundary. This
algorithm works only if the
color with which the region has to be filled and the color of the
boundary of the region are
different. If the boundary is of one single color, this approach
proceeds outwards pixel by
pixel until it hits the boundary of the region.
Boundary Fill Algorithm is recursive in nature. It takes an
interior point(x, y), a fill color,
and a boundary color as the input. The algorithm starts by
checking the color of (x, y). If it’s
color is not equal to the fill color and the boundary color, then it
is painted with the fill color
and the function is called for all the neighbours of (x, y). If a
point is found to be of fill color or
of boundary color, the function does not call its neighbours and
returns. This process
continues until all points up to the boundary color for the region
have been tested.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

The boundary fill algorithm can be implemented by 4-connected


pixels or 8-connected
pixels.
1)4-connected pixels : After painting a pixel, the function is
called for four neighbouring
points. These are the pixel positions that are right, left, above,
and below the current pixel.
2) 8-connected pixels : More complex figures are filled using
this approach. The pixels to be
tested are the 8 neighbouring pixels, the pixel on the right, left,
above, below and the 4
diagonal pixels. Areas filled by this method are called 8-
connected. Below given is the
algorithm

Algorithm:
[Link]:
● Begin at a given point (x, y) inside the region.
● Define the fill_color (color to be used for filling) and
boundary_color (color marking the edge of the region).
[Link] Pixel Color:
● If the color of the pixel at (x, y) is not equal to fill_color
and not equal to boundary_color, proceed.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

[Link] and Recurse:


● Set the pixel color to fill_color.
● Recursively call the fill function on the 4-connected
neighbors (right, left, above, below) or 8-connected
neighbors (right, left, above, below, and four diagonal
pixels).
[Link]:
● Stop when all pixels up to the boundary color are filled.

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

void boundaryfill(int x,int y,int f_color,int b_color)


{
if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)
{
putpixel(x,y,f_color);
boundaryfill(x+1,y,f_color,b_color);
boundaryfill(x,y+1,f_color,b_color);
boundaryfill(x-1,y,f_color,b_color);
boundaryfill(x,y-1,f_color,b_color);
}
}
//getpixel(x,y) gives the color of specified pixel
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

int main()
{
int gm,gd=DETECT,radius;
int x,y;
printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle\n");
scanf("%d",&radius);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,radius);
boundaryfill(x,y,4,15);
delay(5000);
closegraph();
return 0;
}

Output:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Conclusion:
The Boundary Fill Algorithm effectively fills enclosed regions
by recursively painting pixels and checking their neighbors. It
can be implemented with either 4-connected or 8-connected
pixel strategies. This algorithm is simple but can be inefficient
for large areas due to its recursive nature. For better
performance, especially with large or complex shapes,
alternative algorithms like scan-line filling may be considered.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 6

Title: Implement 2D Transformations

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 6

Aim: Implement 2D Transfroamtion


Theory:

2d Transformation in Computer Graphics is utilized to


modify the position, orientation, or size of objects within a
two-dimensional space. These transformations involve
applying mathematical operations to the coordinates of
points or vertices in order to achieve the desired changes.
These transformations can be applied in a sequence to
achieve more complex effects. For example, a combination
of translation, rotation, and scaling operations can be used
to animate an object’s movement, rotation, and resizing in
a 2D animation.
The various 2d Transformation in Computer Graphics
examples include object manipulation, computer-aided
design (CAD), image processing, and graphical user
interfaces (GUIs). By manipulating the position,
orientation, and size of objects within a 2D space, these
transformations enable a wide range of visual effects and
graphical applications.
The fundamental geometrical 2d Transformation
in Computer Graphics include:
● Rotation
● Translation
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

● Scaling

Algorithm:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. A) Translation
a. Get the translation value tx, ty
b. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
c. Plot (x’,y’)
5. B) Scaling
a. Get the scaling value Sx,Sy
b. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy)
c. Plot (x’,y’)
6. C) Rotation
a. Get the Rotation angle
b. Rotate the object by the angle ф
x’=x cos ф - y sin ф
y’=x sin ф - y cosф
c. Plot (x’,y’)

Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

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:\\Turboc3\\BGI"); // Adjust path if


necessary
printf("\t Program for basic transformations\n");
printf("\t Enter the points of triangle: ");
setcolor(1);
scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3,
&y3);

// Draw original triangle


line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

printf("\n 1. Translation\n 2. Rotation\n 3. Scaling\n 4. Exit");


printf("\nEnter your choice: ");
scanf("%d", &c);

switch (c) {
case 1:
printf("\nEnter the translation factors: ");
scanf("%d%d", &xt, &yt);
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();
break;

case 2:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

printf("\nEnter the angle of rotation: ");


scanf("%d", &r);
t = 3.14 * r / 180; // Convert to radians

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

case 3:
printf("\nEnter the scaling factors: ");
scanf("%d%d", &sx, &sy);
nx1 = x1 * sx;
ny1 = y1 * sy;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

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

case 4:
break;

default:
printf("Enter the correct choice\n");
break;
}

closegraph();
}
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Output
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Conclusion: This experiment successfully demonstrated


basic graphics programming using the graphics.h library,
focusing on drawing and transforming a triangle through
translation, rotation, and scaling. Key enhancements included
changing the triangle's line color to dark red and attempting
to adjust line width.

The experiment highlighted the limitations of graphics.h,


particularly in terms of color and line width customization.
These constraints emphasize the advantages of modern
graphics libraries, which offer better support for advanced
graphical features. Overall, while graphics.h provided a
foundational experience, exploring modern libraries can
significantly enhance graphical capabilities and flexibility.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 7

Title: Implement Line Clipping Algorithm: Cohen


Sutherland / Liang Barsky

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 7

Aim: Implement Line Clipping Algorithm: Cohen Sutherland.


Algorithm:
1. Start.
2. Initialize the graphic system using init-graph function.
3. Get the input of window co-ordinates from the user and draw
a window.
4. Get the input of line co-ordinates from user and draw the line.
5. Calculate the region code of each end point of line using
relation given in steps 6.
6. Let (x,y) be the co-ordinates of end point of line and
(xmin,ymin), (xmax,ymax) co-ordinates of world window.
7. If y –ymax = +ve
8. MSB region code = 1.
9. Else MSB region code = 0.
10. If ymin – y = +ve
11. Region code = 1.
12. Else Region code = 0.
13. If x – xmax = +ve
14. Region code = 1.
15. Else Region code = 0.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

16. If xmin – x = +ve


17. LSB Region code = 1.
18. Else LSB Region code = 0.
19. Calculate region code of both end points.
20. Logically and both region code.
21. If Logically anded result is = 0
22. Line is not a clipping candidate.
23. Else.
24. Line is a clipping candidate.
25. Calculate slope of line using formula slope=(y2-y1)/(x2-x1).
26. If line is to be horizontally clipped.
27. New y = ymin or ymax.
28. New x = x1 + ((new y - y1)/slope).
29. If line is to be vertically clipped.
30. New x = xmin or xmax.
31. New y = y1+slope*(new x –x1).
32. Clip the lines from these intersection points.
33. Display the new line.
34. Close the graphic system.
35. Stop

Theory:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

The Cohen-Sutherland algorithm is a popular line-clipping


algorithm used in computer graphics to clip a line segment to a
rectangular clipping window. It works by dividing the 2D space
around the clipping window into nine regions, using a 4-bit out-
code for each endpoint of the line segment. These out-codes
represent the region in which the point lies (above, below, left,
right, or inside the window). The algorithm evaluates out-codes
of both endpoints, and based on logical operations, determines if
the line is fully inside, fully outside, or requires clipping. If
clipping is required, the line is iteratively clipped against the
window’s boundaries until it is fully within the visible area or
discarded. This algorithm is efficient as it quickly rejects or
accepts lines without needing complex calculations. It is an
example of a divide-and-conquer strategy, where the problem
of clipping a line segment is broken down based on regions
around the clipping window.
Another important theoretical component is the algorithm’s line
intersection calculations. For lines partially outside the
window, it relies on simple slope-based formulas to determine
intersection points with the clipping window’s boundaries.
These calculations are based on parametric equations for lines,
ensuring precise clipping of the line segment to the window
edges. However, while Cohen-Sutherland is widely used, it is
not always optimal for all cases, especially in 3D clipping or
more complex polygonal environments. Other algorithms like
Liang-Barsky or Sutherland-Hodgman are sometimes
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

preferred, as they can handle specific cases with fewer


computations by avoiding region code generation.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
int xwmax = 300, xwmin = 200, ywmax = 100, ywmin = 200,
ax, ay, bx, by;
void input()
{
printf("Enter points in a & b:\n");
scanf("%d%d%d%d", &ax, &ay, &bx, &by);
}
void draw()
{
rectangle(xwmin, ywmin, xwmax, ywmax);
}
void clip(int x, int y, int p[4])
{
if (y < ywmax)
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

p[0] = 1;
if (y > ywmin)
p[1] = 1;
if (x > xwmax)
p[2] = 1;
if (x < xwmin)
p[3] = 1;
else
p[3] = 0;
}
int main()
{
int gd = DETECT, gm, y, x, c, p1[4], p2[4], p3[4], i;
float m;
initgraph(&gd, &gm, NULL);
cleardevice();
input();
cleardevice();
clip(ax, ay, p1);
clip(bx, by, p2);
for (i = 0; i < 4; i++)
p3[3] = p1[i] && p2[i];
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

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


if (p3[i] == 1)
break;
draw();
line(ax, ay, bx, by);
getch();
cleardevice();
if (i != 4)
draw();
else
{
m = (float)(by - ay) / (bx - ax);
if (p1[0] == 1)
y = ywmax;
if (p1[1] == 1)
y = ywmin;
if (p1[0] == 1 || p1[1] == 1)
{
ax = ax + (y - ay) / m;
ay = y;
}
if (p2[0] == 1)
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

y = ywmax;
if (p2[1] == 1)
y = ywmin;
if (p2[0] == 1 || p2[1] == 1)
{
bx = bx + (y - by) / m;
by = y;
}
if (p1[2] == 1)
x = xwmax;
if (p1[3] == 1)
x = xwmin;
if (p1[2] == 1 || p1[3] == 1)
{
ay = ay + m * (x - ax);
ax = x;
}
if (p2[2] == 1)
x = xwmax;
if (p2[3] == 1)
x = xwmin;
if (p2[2] == 1 || p2[3] == 1)
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

{
by = by + m * (x - bx);
bx = x;
}
draw();
line(ax, ay, bx, by);
}
getch();
closegraph();
restorecrtmode();
return 0;
}

Output:

Conclusion:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Hence, we have successfully implemented the Cohen-


Sutherland Line Clipping Algorithm in code and executed it
to achieve accurate line clipping within a rectangular window.
The implementation demonstrates how the algorithm efficiently
handles line segments through the use of out-codes, allowing for
quick decisions on whether lines are fully inside, outside, or
require clipping. The code has been tested and functions
correctly, effectively clipping the lines and ensuring that only
the visible portions are rendered within the defined boundaries.
This confirms the correctness and efficiency of the algorithm in
practical applications.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 8

Title: Program to perform 3D transformation

Performed On: ___________

Sign: __________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 8

Aim: Implement program to perform 3D Transformation.


Theory:
3D Geometry
Three dimension system has three axis x, y, z. The orientation
of a 3D coordinate system is of two types. Right-handed
system and left-handed system.
In the right -handed system thumb of right- hand points to
positive z-direction and left- hand system thumb point to
negative two directions. Following figure show right-hand
orientation of the cube.
The 2D can show two-dimensional objects. Like the Bar
chart, pie chart, graphs. But some more natural objects can be
represented using 3D. Using 3D, we can see different shapes
of the object in different sections.

Rotation:
3D rotation is not same as 2D rotation. In 3D rotation, we
have to specify the angle of rotation along with the axis of
rotation. We can perform 3D rotation about X, Y, and Z
axes.
Scaling:
You can change the size of an object using scaling
transformation. In the scaling process, you either expand or
compress the dimensions of the object. Scaling can be
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

achieved by multiplying the original coordinates of the object


with the scaling factor to get the desired result.
Shear:
A transformation that slants the shape of an object is called the
shear transformation. Like in 2D shear, we can shear an object
along the X-axis, Y-axis, or Z-axis in 3D.

Algorithm:
3 Dimensional Transformation Source Code
1. Enter the choice for transformation.
2. Perform the translation, rotation, scaling of 3D object.
3. Getthe needed parameters for the transformation from the
user.
4. Increase of rotation, object can be rotated about x or y or z
axis.
5. Display the transmitted object in the screen
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Program:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int maxx, maxy, midx, midy;
void axis() {
getch();
cleardevice();
line(midx, 0, midx, maxy);
line(0, midy, maxx, midy);
}
void main() {
int gd, gm, x, y, z, ang, x1, x2, y1, y2;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, "C:/Turboc3/BGI");
setfillstyle(3, 25);
maxx = getmaxx();
maxy = getmaxy();
midx = maxx / 2;
midy = maxy / 2;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

outtextxy(100, 100, "ORIGINAL OBJECT");


line(midx, 0, midx, maxy);
line(0, midy, maxx, midy);
bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
axis();
outtextxy(100, 20, "TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d", &x, &y);
bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
bar3d(midx + (x + 100), midy - (y + 20), midx + (x + 60),
midy - (y + 90), 20, 5);
axis();
outtextxy(100, 20, "SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d", &x, &y, &z);

bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
bar3d(midx + (x * 100), midy - (y * 20), midx + (x * 60),
midy - (y * 90), 20 * z, 5);
axis();
outtextxy(100, 20, "ROTATION");
printf("\n Enter the Rotation angle: ");
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

scanf("%d", &ang);
x1 = 100 * cos(ang * 3.14 / 180) - 20 * sin(ang * 3.14 / 180);
y1 = 100 * sin(ang * 3.14 / 180) + 20 * cos(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 * cos(ang * 3.14 / 180);
axis();
printf("\n After rotating about z-axis\n");
bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
bar3d(midx + x1, midy - y1, midx + x2, midy - y2, 20, 5);
axis();
printf("\n After rotating about x-axis\n");
bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
bar3d(midx + 100, midy - x1, midx + 60, midy - x2, 20, 5);
axis();
printf("\n After rotating about y-axis\n");
bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 5);
bar3d(midx + x1, midy - 20, midx + x2, midy - 90, 20, 5);
axis();
closegraph();
}
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Output:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Conclusion: Thus, we have implemented the program of 3D


Transformation Using C.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Expt. No. 9

Title: Perform projection of a 3D object on Projection Parallel


and Perspective

Performed On: ___________

Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

EXPERIMENT NO 9

Aim: Perform projection of a 3D object on Projection Plane:


Parallel and Perspective.
Theory:
In Perspective Projection the center of projection is at finite
distance from projection plane. This projection produces
realistic views but does not preserve relative proportions of an
object dimensions. Projections of distant object are smaller than
projections of objects of same size that are closer to projection
plane.
Types of perspective projections:
Types of Perspective Projection: Classification of perspective
projection is on basis of vanishing points (It is a point in image
where a parallel line through center of projection intersects
view plane.). We can say that a vanishing point is a point where
projection line intersects view plane.
1. One Point Perspective Projection – One point perspective
projection occurs when any of principal axes intersects with
projection plane or we can say when projection plane is
perpendicular to principal axis above figure, z axis intersects
projection plane whereas x and y axis remain parallel to
projection plane.
[Link] Point Perspective Projection – Two point perspective
projection occurs when projection plane intersects two of
principal axis. In the above figure, projection plane intersects x
and y axis whereas z axis remains parallel to projection plane.
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

[Link] Point Perspective Projection – Three point


perspective projection occurs when all three axis intersects
with projection plane. There is no any principle axis which is
parallel to projection plane.

Algorithm:
Start the program 3D projection Images
Using for loop get the no of
corrdiante of the 3D view Using line
funtion in graphics we draw the
projection Front view is draw by the
following code
if(i!=fs-1)
line(x[i]+some constang value,y[i],x[i+1]+some constant
value,y[i+1]);
else
line(x[i]+some constant value,y[i],x[0]+some constant
value,y[0]);
in For loop
like that the other top view and side view is drawn by the line
function
Finally terminate the 3 Dimensional image view in Front view,
Top view and side view
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Coding:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>

void draw3d(int fs, int x[20], int y[20], int tx, int ty, int d);

void draw3d(int fs, int x[20], int y[20], int tx, int ty, int d) {
int i, j, k = 0;

for (j = 0; j < 2; j++) {


for (i = 0; i < fs; i++) {
if (i != fs - 1)
line(x[i] + tx + k, y[i] + ty - k, x[i + 1] + tx + k, y[i +
1] + ty - k);
else
line(x[i] + tx + k, y[i] + ty - k, x[0] + tx + k, y[0] + ty -
k);
}
k = d;
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

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


line(x[i] + tx, y[i] + ty, x[i] + tx + d, y[i] + ty - d);
}
}

void main() {
int gd = DETECT, gm;
int x[20], y[20], tx = 0, ty = 0, i, fs, d;

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


printf("No of sides (front view only): ");
scanf("%d", &fs);
printf("Co-ordinates: ");

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


printf("(x%d,y%d): ", i, i);
scanf("%d%d", &x[i], &y[i]);
}

printf("Depth: ");
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

scanf("%d", &d);

draw3d(fs, x, y, tx, ty, d);


getch(); // front view

setcolor(14);
for (i = 0; i < fs; i++) {
if (i != fs - 1)
line(x[i] + 200, y[i], x[i + 1] + 200, y[i + 1]);
else
line(x[i] + 200, y[i], x[0] + 200, y[0]);
}

getch(); // top view


for (i = 0; i < fs - 1; i++) {
line(x[i], 300, x[i + 1], 300);
line(x[i], 300 + d * 2, x[i + 1], 300 + d * 2);
line(x[i], 300, x[i], 300 + d * 2);
line(x[i + 1], 300, x[i + 1], 300 + d * 2);
}

getch(); // side view


for (i = 0; i < fs - 1; i++) {
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

line(10, y[i], 10, y[i + 1]);


line(10 + d * 2, y[i], 10 + d * 2, y[i + 1]);
line(10, y[i], 10 + d * 2, y[i]);
line(10, y[i + 1], 10 + d * 2, y[i + 1]);
}

getch();
closegraph();
}
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]

Output:

Conclusion:
Thus, 3D perspective projections for front, top and side views
are performed successfully and outputs are shown.
[Type here]

Page | 0

You might also like