CG Lab Manual
CG Lab Manual
NO:72
Expt. No. 1
Sign: __________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 1
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]
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]
OUTPUT:-
Expt. No. 2
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]
Expt. No. 3
Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 3
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]
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
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]
Getch();
Closegraph();
}
Void circlepoints(int x, int y) {
Putpixel(x + 300, y + 300, 8);
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
Conclusion:
Thus We Midpoint Ellipse Algorithm Implemented Using C
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
Expt. No. 5
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]
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]
Program:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
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
Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 6
● 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;
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;
case 2:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
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;
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]
Expt. No. 7
Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 7
Theory:
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
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]
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]
Expt. No. 8
Sign: __________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 8
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]
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]
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]
Expt. No. 9
Sign:__________
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
EXPERIMENT NO 9
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;
void main() {
int gd = DETECT, gm;
int x[20], y[20], tx = 0, ty = 0, i, fs, d;
printf("Depth: ");
NAME : ZUBIA SAYED COMPUTER GRAPHICS UIN:242P013 [Link]
scanf("%d", &d);
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();
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