GURU NANAK DEV ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER APPLICATIONS
PRACTICAL FILE
Interactive Computer Graphics Laboratory
(PGCA-2220)
Submitted To:- Submitted By:-
[Link] Manvir Singh
Anand CRN:-2329013
Programme URN:-2303299
Coordinator (MCA 3rd Semester)
Batch:-2023-2025
INDEX
[Link] Experiments Page No. Date Signature
1. Write a program to plot a pixel on thescreen in a 1-2
particular color.
2. Write a program for creating a simple two- 3-6
dimensional shape of any object using lines, circle,
etc.
3. Using different graphics functions available for text 7-8
formatting, write a program for displaying text in
different sizes, different colors, font styles.
4. Implement the DDA algorithm for drawing line 9-11
(programmer is expected to shift the origin to the
center of the screen and divide the screen into
required quadrants)
5. Write a program to input the linecoordinates from the 12-14
user to generate a line using Bresenham’s method and
DDA algorithm. Compare the lines for theirvalues on
the plotted line
6. Write a program to generate a complete moving 15-17
wheel using Midpoint circle drawing algorithm and
DDA line drawing algorithm.
7. Write a program to draw an ellipse using theMidpoint 18-20
ellipse generation algorithm for both the regions.
8. Write a program to draw any 2-D object and perform 21-26
the transformations on it according to the input
parameters from the user, namely: Translation,
Rotation or Scaling.
9. Write a program to rotate a triangle aboutany one 27-29
of its end coordinates.
10. Write program to draw a house like figure and 30-33
perform the following operations. a) Scaling about the
origin followed by translation. b) Scaling with
reference to an arbitrary point.
11. Write a program for filling a given rectangle with 34-35
some particular color using boundary fill algorithm.
12. Write a program for filling a polygon usingScan line 36-38
Polygon fill algorithm.
13. Write a program to perform clipping on a line against 39-44
the clip window using any line clipping algorithm.
The output must be twofold showing the before
clipping and after clipping images.
14. Write a program to implement the Sutherland 45-48
Hodgeman algorithm for clipping any polygon.
OUTPUT:-
1
Experiment-1
Write a program to plot a pixel on the screen in a particular color
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void get_driver(){
int gDriver=DETECT,gMode; // define variables
initgraph(&gDriver,&gMode,"c:\\tc\\bgi"); // initialize graphics
Librarysetbkcolor(15); // make background to white
void main(){
get_driver();// To make function of define drivers
int dx,dy,x,y; //define variables
x = abs(getmaxx()/2); // To get center on x axis
y = abs(getmaxy()/2); // To get center on y axis
putpixel(x,y,4); //for put pix
getch(); // getch used to hold program execution
2
OUTPUT:-
3
Experiment-2
Write a program for creating a single two-dimensional shape of any object using
lines, circle, etc
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
rectangle(60,80,150,200);
rectangle(95,140,120,200);
line(60,80,100,15);
line(100,15,150,80);
circle(100,60,10);
getch();
closegraph();
4
5
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
{int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
line( 150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320, 150, 320, 200);
line(100, 200, 110, 200);
line( 320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc( 290, 200, 0, 180, 20);
line( 270, 200, 150, 200);
circle(130, 200, 17);
circle(290, 200, 17);
getch();
}
6
OUTPUT:-
7
Experiment-3
Using different graphics function available for text formatting, write a programfor
displaying text in different sizes, different colors, font styles:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++){
settextstyle(font,HORIZ_DIR,font+1);// sets font type, font direction, size
setcolor(font+1); // sets color for text.
outtextxy(x,y,"text with different fonts"); // prints message on screen at (x,y)
y=y+25;
for(font=0;font<=2;font++)
settextstyle(font,VERT_DIR,font+2);
setcolor(font+1);
x=250;
y=100;
outtextxy(x,y,"text in vertical direction");
y=y+25;
getch();
closegraph();
}
8
OUTPUT:-
9
Experiment:-4
Implement the DDA algorithm for drawing line (programmer is expected to shift
the origin to the center of the screen and divide the screen into required
quadrants)
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cout<<"Enter the value of x1 and y1 : ";
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1; y=y1;
i=1;
while(i<=step)
10
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
closegraph();
11
OUTPUT:-
12
Experiment-5
Write a program to input the line coordinates from the user to generate a line
using Bresenham’s method and DDA. Compare the lines for their values on the
plotted line
#include<iostream.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;
13
}
int main()
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);
return 0;
return 0;
14
OUTPUT:-
15
Experiment-6
Write a program to generate a complete moving wheel using Midpoint circle
drawing algorithm and DDA line drawing algorithm
#include<iostream.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
y += 1;
err += 2*y + 1;
if (err > 0)
{
16
x -= 1;
err -= 2*x + 1;
int main()
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter co-ordinates of center(x and y): ";
cin>>x>>y;
drawcircle(x, y, r);
return 0;
17
OUTPUT:-
18
Experiment-7
Write a program to draw an ellipse using the Midpoint ellipse generation algorithm for
both the regions
#include <graphics.h>
#include <conio.h>
#include <math.h>
void drawEllipse(int xc, int yc, int a, int b)
{int x = 0, y = b;
int a2 = a * a;
int b2 = b * b;
int d = b2 - (a2 * b) + (0.25 * a2);
// Region 1
while ((2 * b2 * x) < (2 * a2 * y)) {
putpixel(xc + x, yc - y, WHITE); // Upper half
putpixel(xc - x, yc - y, WHITE); // Upper half
putpixel(xc + x, yc + y, WHITE); // Lower half
putpixel(xc - x, yc + y, WHITE); // Lower half
x++;
if (d < 0) {
d += (2 * b2 * x) + b2;
} else
{y--;
d += (2 * b2 * x) - (2 * a2 * y) + b2;
}
}
// Region 2
d = (b2 * (x + 0.5) * (x + 0.5)) + (a2 * (y - 1) * (y - 1)) - (a2 * b2);
while (y >= 0) {
putpixel(xc + x, yc - y, WHITE); // Upper half
putpixel(xc - x, yc - y, WHITE); // Upper half
putpixel(xc + x, yc + y, WHITE); // Lower half
putpixel(xc - x, yc + y, WHITE); // Lower half
y--;
if (d > 0) {
d += a2 - (2 * a2 * y);
} else
19
{x+
+;
d += (2 * b2 * x) - (2 * a2 * y) + a2;
}
}
}int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Adjust path if necessary
// Center coordinates and axes lengths
int xc = 320; // X coordinate of the center
int yc = 240; // Y coordinate of the center
int a = 100; // Semi-major axis
int b = 50; // Semi-minor axis
// Draw the ellipse
drawEllipse(xc, yc, a, b);
// Wait for a key press
getch();
// Close the graphics window
closegraph();
return 0;
}
20
OUTPUT:-
BEFORE TRANSLATION:-
AFTER TRANSLATION:-
21
Experiment-8:
Write a program to draw any 2-D object and perform the transformations on it
according to the input parameters from the user, namely: Translation, Rotation or
Scaling.
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
void drawTriangle(int vertices[3][2]) {
for (int i = 0; i < 3; i++) {
line(vertices[i][0], vertices[i][1], vertices[(i + 1) % 3][0], vertices[(i + 1) % 3][1]);
}
}
void translate(int vertices[3][2], int tx, int ty) {
for (int i = 0; i < 3; i++) {
vertices[i][0] += tx;
vertices[i][1] += ty;
}
}
void rotate(int vertices[3][2], float angle) {
float radians = angle * (M_PI / 180); // Convert to radians
for (int i = 0; i < 3; i++) {
int x = vertices[i][0];
int y = vertices[i][1];
vertices[i][0] = (int)(x * cos(radians) - y * sin(radians));
vertices[i][1] = (int)(x * sin(radians) + y * cos(radians));
}
}
void scale(int vertices[3][2], float sx, float sy) {
for (int i = 0; i < 3; i++) {
vertices[i][0] = (int)(vertices[i][0] * sx);
vertices[i][1] = (int)(vertices[i][1] * sy);
}
}
void main() {
int gd = DETECT, gm;
22
BEFORE ROATATION:-
AFTER ROTATION:-
23
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Define the initial triangle vertices
int vertices[3][2] = {{200, 150}, {250, 250}, {150, 250}};
// Draw the original triangle
drawTriangle(vertices);
// Get user choice for transformation
int choice;
printf("Choose a transformation:\n");
printf("1. Translation\n");
printf("2. Rotation\n");
printf("3. Scaling\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: {
int tx, ty;
printf("Enter translation values (tx ty): ");
scanf("%d %d", &tx, &ty);
translate(vertices, tx, ty);
break;
}
case 2: {
float angle;
printf("Enter rotation angle (in degrees): ");
scanf("%f", &angle);
rotate(vertices, angle);
break;
}
case 3: {
float sx, sy;
printf("Enter scaling factors (sx sy): ");
scanf("%f %f", &sx, &sy);
scale(vertices, sx, sy);
break;
}
default:
printf("Invalid choice!\n");
getch();
24
BEFORE SCALING:-
AFTER SCALING:-
25
closegraph();
return;
}
// Clear the screen and draw the transformed triangle
cleardevice();
drawTriangle(vertices);
getch();
closegraph();
}
26
OUTPUT:-
27
Experiment-9
Write a program to rotate a triangle about any one of its end coordinates.
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
void drawTriangle(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);
}
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
double angle;
int choice;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Update this path as needed
// Input triangle coordinates
printf("Enter coordinates of triangle (x1 y1 x2 y2 x3 y3): ");
scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3);
// Draw original triangle in Yellow color
setcolor(YELLOW);
cleardevice();
drawTriangle(x1, y1, x2, y2, x3, y3);
getch();
// Input rotation angle
printf("Enter rotation angle: ");
scanf("%lf", &angle);
// Choose which vertex to rotate around
printf("Choose vertex to rotate around:\n");
printf("1. Vertex 1 (%d, %d)\n", x1, y1);
printf("2. Vertex 2 (%d, %d)\n", x2, y2);
printf("3. Vertex 3 (%d, %d)\n", x3, y3);
28
printf("Enter your choice (1/2/3): ");
scanf("%d", &choice);
int pivotX, pivotY;
// Set pivot point based on user's choice
if (choice == 1) {
pivotX = x1;
pivotY = y1;
} else if (choice == 2) {
pivotX = x2;
pivotY = y2;
} else if (choice == 3) {
pivotX = x3;
pivotY = y3;
} else {
printf("Invalid choice!\n");
closegraph();
return 0;
}
// Rotate the triangle around the chosen vertex
double rad = angle * M_PI / 180; // Convert angle to radians
int newX2 = pivotX + (x2 - pivotX) * cos(rad) - (y2 - pivotY) * sin(rad);
int newY2 = pivotY + (x2 - pivotX) * sin(rad) + (y2 - pivotY) * cos(rad);
int newX3 = pivotX + (x3 - pivotX) * cos(rad) - (y3 - pivotY) * sin(rad);
int newY3 = pivotY + (x3 - pivotX) * sin(rad) + (y3 - pivotY) * cos(rad);
// Clear and draw the rotated triangle in Cyan color
cleardevice();
setcolor(CYAN);
drawTriangle(pivotX, pivotY, newX2, newY2, newX3, newY3);
getch();
closegraph();
return 0;
}
29
OUTPUT:-
30
Experiment-10
Write program to draw a house like figure and perform the following operations. a)
Scaling about the origin followed by translation. b) Scaling with reference to an
arbitrary point.
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
// Function to draw a house (rectangle + triangle for roof + door)
void drawHouse(int xOffset, int yOffset) {
// Draw the base of the house (rectangle)
rectangle(200 + xOffset, 300 + yOffset, 400 + xOffset, 500 + yOffset); // House base
// Draw the roof (triangle)
line(200 + xOffset, 300 + yOffset, 300 + xOffset, 200 + yOffset); // Left roof
line(300 + xOffset, 200 + yOffset, 400 + xOffset, 300 + yOffset); // Right roof
// Draw the door (rectangle)
rectangle(280 + xOffset, 400 + yOffset, 320 + xOffset, 500 + yOffset); // Door
}
// Function to scale and translate the house
void scaleAndTranslate(float sx, float sy, float tx, float ty) {
// Clear the screen before drawing
cleardevice();
// Draw scaled house
setcolor(YELLOW); // Change color to YELLOW for visibility
drawHouse(0, 0);
// Apply scaling
int scaledX1 = (200 * sx);
int scaledY1 = (300 * sy);
int scaledX2 = (400 * sx);
int scaledY2 = (500 * sy);
// Clear the screen and redraw after scaling
cleardevice();
31
drawHouse(0, 0);
// Translate the house
cleardevice();
drawHouse(tx, ty);
}
// Function to scale with respect to an arbitrary point
void scaleWithReference(float sx, float sy, int rx, int ry) {
cleardevice();
// Draw the original house
setcolor(WHITE); // Original house in WHITE color
drawHouse(0, 0);
// Scale with respect to (rx, ry) - reference point
setcolor(CYAN); // Scaled house in CYAN color for better visibility
int xOffset = rx - (rx * sx);
int yOffset = ry - (ry * sy);
// Apply scaling
drawHouse(xOffset, yOffset);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Initial drawing of the house
drawHouse(0, 0);
getch();
// Scaling about the origin followed by translation
float sx, sy, tx, ty;
printf("Enter scaling factors (sx, sy): ");
scanf("%f %f", &sx, &sy);
printf("Enter translation factors (tx, ty): ");
scanf("%f %f", &tx, &ty);
scaleAndTranslate(sx, sy, tx, ty);
32
// Scaling with reference to an arbitrary point
float rx, ry;
printf("Enter reference point (rx, ry): ");
scanf("%f %f", &rx, &ry);
printf("Enter scaling factors (sx, sy): ");
scanf("%f %f", &sx, &sy);
scaleWithReference(sx, sy, rx, ry);
getch();
closegraph();
return 0;
}
33
OUTPUT:-
34
Experiment-11
Write a program for filling a given rectangle with some particular color using
boundary fill algorithm.
#include <graphics.h>
#include <conio.h>
#include <iostream.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);
}
}
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;
cout << "Enter the top-left corner (x1, y1) of the rectangle:\n";
cin >> x1 >> y1;
cout << "Enter the bottom-right corner (x2, y2) of the rectangle:\n";
cin >> x2 >> y2;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
// Draw the rectangle
rectangle(x1, y1, x2, y2);
// Fill the rectangle
boundaryfill((x1 + x2) / 2, (y1 + y2) / 2, 4, WHITE); // Filling starting from the center
delay(5000); // Wait for 5 seconds to view the filled rectangle
closegraph(); // Close the graphics mode
return 0;
}
35
OUTPUT:-
36
Experiment-12
Write a program for filling a polygon using Scan line Polygon fill algorithm.
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void fillPolygon(int vertices[][2], int n) {
int yMin = 9999, yMax = -9999;
// Find the minimum and maximum y values
for (int i = 0; i < n; i++) {
if (vertices[i][1] < yMin) yMin = vertices[i][1];
if (vertices[i][1] > yMax) yMax = vertices[i][1];
}
// Scan line filling
for (int y = yMin; y <= yMax; y++) {
int xMin = 9999, xMax = -9999;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n; // Next vertex
if ((vertices[i][1] <= y && vertices[j][1] > y) || (vertices[j][1] <= y && vertices[i][1] > y)) {
float x = (float)(vertices[j][0] - vertices[i][0]) * (y - vertices[i][1]) / (vertices[j][1] - vertices[i][1]) +
vertices[i][0];
if (x < xMin) xMin = (int)x;
if (x > xMax) xMax = (int)x;
}
}
// Draw horizontal line between xMin and xMax
if (xMin < xMax) {
setcolor(YELLOW); // Set the fill color
line(xMin, y, xMax, y);
}
}
}
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
37
// Define the polygon vertices
int vertices[][2] = {{200, 200}, {300, 150}, {400, 200}, {350, 300}, {250, 300}};
int n = sizeof(vertices) / sizeof(vertices[0]);
// Draw the polygon outline
setcolor(WHITE); // Set outline color
for (int i = 0; i < n; i++) {
line(vertices[i][0], vertices[i][1], vertices[(i + 1) % n][0], vertices[(i + 1) % n][1]);
}
// Fill the polygon
fillPolygon(vertices, n);
getch();
closegraph();
}
38
OUTPUT:-
BEFORE CLIPPING:-
AFTER CLIPPING:-
39
Experiment-13
Write a program to perform clipping on a line against the clip window using any line
clipping algorithm. The output must be twofold showing the before clipping and after
clipping images.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
typedef struct coordinate
{
int x, y;
char code[4];
} PT;
void drawwindow();
void drawline(PT p1, PT p2);
PT setcode(PT p);
int visibility(PT p1, PT p2);
PT resetendpt(PT p1, PT p2);
void main()
{
int gd = DETECT, v, gm;
PT p1, p2, p3, p4, ptemp;
// Taking input from user for line coordinates
cout << "\nEnter x1 and y1\n";
cin >> p1.x >> p1.y;
cout << "\nEnter x2 and y2\n";
cin >> p2.x >> p2.y;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
// Draw the clipping window
drawwindow();
delay(500); // Delay to allow user to see the clipping window
40
// Draw the original line
drawline(p1, p2);
delay(500); // Delay to show the original line before clipping
// **Add 1-minute delay before clipping**
delay(60000); // Delay for 1 minute (60000 milliseconds)
// Clear the screen after the 1-minute delay
cleardevice();
delay(500); // Give a small delay before clipping starts
// Set the codes for the two endpoints of the line
p1 = setcode(p1);
p2 = setcode(p2);
// Check visibility (completely inside, completely outside, or partially inside)
v = visibility(p1, p2);
delay(500); // Delay to allow user to understand the clipping logic
switch (v)
{
case 0: // Line is completely inside the window
drawwindow();
delay(500);
drawline(p1, p2);
break;
case 1: // Line is completely outside the window
drawwindow();
delay(500);
break;
case 2: // Line is partially inside, need to clip
p3 = resetendpt(p1, p2); // Reset p1 endpoint
p4 = resetendpt(p2, p1); // Reset p2 endpoint
drawwindow();
delay(500); // Delay before drawing the clipped line
drawline(p3, p4); // Draw the clipped line
break;
}
// 1-minute delay (60000 milliseconds)
delay(60000); // Delay for 1 minute (60000 milliseconds)
41
closegraph(); // Close the graphics mode
}
void drawwindow()
{
// Draw a rectangle (the clipping window)
line(150, 100, 450, 100); // Top side
line(450, 100, 450, 350); // Right side
line(450, 350, 150, 350); // Bottom side
line(150, 350, 150, 100); // Left side
}
void drawline(PT p1, PT p2)
{
// Draw a line from point p1 to p2
line(p1.x, p1.y, p2.x, p2.y);
}
PT setcode(PT p) // For setting the 4-bit code for the point
{
PT ptemp;
// Set top, bottom, right, left codes based on the point's coordinates
if (p.y < 100) [Link][0] = '1'; // Top
else [Link][0] = '0';
if (p.y > 350) [Link][1] = '1'; // Bottom
else [Link][1] = '0';
if (p.x > 450) [Link][2] = '1'; // Right
else [Link][2] = '0';
if (p.x < 150) [Link][3] = '1'; // Left
else [Link][3] = '0';
ptemp.x = p.x;
ptemp.y = p.y;
return ptemp;
}
42
int visibility(PT p1, PT p2)
{
int i, flag = 0;
// Check if the line is completely inside, outside, or partially inside the clipping window
for (i = 0; i < 4; i++)
{
if (([Link][i] != '0') || ([Link][i] != '0'))
flag = 1;
}
if (flag == 0) return 0; // Line completely inside
for (i = 0; i < 4; i++)
{
if (([Link][i] == [Link][i]) && ([Link][i] == '1'))
flag = 0; // Both points outside in the same region
}
if (flag == 0) return 1; // Line completely outside
return 2; // Line partially inside, need to clip
}
PT resetendpt(PT p1, PT p2)
{
PT temp;
int x, y, i;
float m, k;
// Calculate the new endpoint based on the clipping window
if ([Link][3] == '1') // Left
x = 150;
else if ([Link][2] == '1') // Right
x = 450;
if ([Link][3] == '1' || [Link][2] == '1')
{
m = (float)(p2.y - p1.y) / (p2.x - p1.x);
k = p1.y + (m * (x - p1.x));
43
temp.y = k;
temp.x = x;
for (i = 0; i < 4; i++) [Link][i] = [Link][i];
if (temp.y <= 350 && temp.y >= 100)
return temp;
}
if ([Link][0] == '1') // Top
y = 100;
else if ([Link][1] == '1') // Bottom
y = 350;
if ([Link][0] == '1' || [Link][1] == '1')
{
m = (float)(p2.y - p1.y) / (p2.x - p1.x);
k = (float)p1.x + (float)(y - p1.y) / m;
temp.x = k;
temp.y = y;
for (i = 0; i < 4; i++) [Link][i] = [Link][i];
return temp;
}
else
return p1;
}
44
OUTPUT:-
45
Experiment-14
Write a program to implement the Sutherland Hodgeman algorithm for clipping any
polygon
#include <stdio.h>
#include <graphics.h>
#include <conio.h> // For getch()
#include <stdlib.h>
// Structure to represent a 2D point
struct Point {
int x, y;
};
// Function to check if a point is inside the clipping window
int inside(Point p, Point a, Point b) {
return ((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x)) >= 0;
}
// Function to find the intersection point of two lines (p1, p2) and (a, b)
Point intersect(Point p1, Point p2, Point a, Point b) {
Point intersection;
int dx1 = p2.x - p1.x;
int dy1 = p2.y - p1.y;
int dx2 = b.x - a.x;
int dy2 = b.y - a.y;
float t = ((a.x - p1.x) * dy2 - (a.y - p1.y) * dx2) / (float)(dx1 * dy2 - dy1 * dx2);
intersection.x = p1.x + t * dx1;
intersection.y = p1.y + t * dy1;
return intersection;
}
// Sutherland-Hodgman polygon clipping algorithm
void clip_polygon(Point* polygon, int& n, Point* clip_window) {
Point* new_polygon = (Point*)malloc(n * sizeof(Point)); // Temporary storage for clipped polygon
int new_n;
// Iterate over the edges of the clipping window
for (int i = 0; i < 4; i++) {
46
new_n = 0; // Reset the count of new polygon vertices
for (int j = 0; j < n; j++) {
int k = (j + 1) % n; // Next point (circular)
// Check if the point is inside the clipping window
if (inside(polygon[k], clip_window[i], clip_window[(i + 1) % 4])) {
if (!inside(polygon[j], clip_window[i], clip_window[(i + 1) % 4])) {
// Add the intersection point if the current point is outside
new_polygon[new_n++] = intersect(polygon[j], polygon[k], clip_window[i], clip_window[(i +
1) % 4]);
}
new_polygon[new_n++] = polygon[k]; // Add the point inside the clipping window
}
else if (inside(polygon[j], clip_window[i], clip_window[(i + 1) % 4])) {
// Add the intersection point if the current point is inside
new_polygon[new_n++] = intersect(polygon[j], polygon[k], clip_window[i], clip_window[(i + 1) %
4]);
}
}
n = new_n; // Update the number of vertices
// Copy the new polygon vertices back to the original array
for (int j = 0; j < n; j++) {
polygon[j] = new_polygon[j];
}
}free(new_polygon); // Free the allocated memory for the temporary polygon
}
// Function to draw a polygon
void draw_polygon(Point* polygon, int n, int color) {
setcolor(color);
for (int i = 0; i < n; i++) {
int j = (i + 1) % n; // Next point (circular)
line(polygon[i].x, polygon[i].y, polygon[j].x, polygon[j].y);
}
}
int main() {
int gd = DETECT, gm;
// Define the clipping window (rectangular window)
Point clip_window[4] = {{220, 140}, {420, 140}, {420, 340}, {220, 340}};
47
// Define the polygon vertices
int n;
printf("Enter the number of vertices of the polygon: ");
scanf("%d", &n);
Point* polygon = (Point*)malloc(n * sizeof(Point)); // Dynamically allocate memory for the polygon
printf("Enter the coordinates of the polygon:\n");
for (int i = 0; i < n; i++) {
printf("Vertex %d (x, y): ", i + 1);
scanf("%d %d", &polygon[i].x, &polygon[i].y);
}
// Initialize graphics
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
// Draw the clipping window and polygon
setcolor(RED);
draw_polygon(clip_window, 4, RED);
setcolor(WHITE);
draw_polygon(polygon, n, WHITE);
printf("\nPress any key to clip the polygon...\n");
getch();
// Perform polygon clipping using the Sutherland-Hodgman algorithm
clip_polygon(polygon, n, clip_window);
// Draw the clipped polygon
setcolor(GREEN);
draw_polygon(polygon, n, GREEN);
printf("\nThis is the clipped polygon...\n");
getch();
// Free the dynamically allocated memory for the polygon
free(polygon);
// Close the graphics window
closegraph();
return 0;
}
48
49