Harshdeep Icf File
Harshdeep Icf File
Harshdeep Icf File
PRACTICAL FILE
Interactive Computer Graphics Laboratory
(PGCA-2220)
1
Experiment-1
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void get_driver(){
void main(){
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);
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++){
y=y+25;
for(font=0;font<=2;font++)
settextstyle(font,VERT_DIR,font+2);
setcolor(font+1);
x=250;
y=100;
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");
cin>>x1>>y1;
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>
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()
cin>>x0>>y0;
cin>>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>
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()
cin>>r;
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>
// 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
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 main() {
int gd = DETECT, gm;
22
BEFORE ROATATION:-
AFTER ROTATION:-
23
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
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;
}
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;
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>
// Apply scaling
int scaledX1 = (200 * sx);
int scaledY1 = (300 * sy);
int scaledX2 = (400 * sx);
int scaledY2 = (500 * sy);
// Apply scaling
drawHouse(xOffset, yOffset);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
32
// Scaling with reference to an arbitrary point
float rx, ry;
printf("Enter reference point (rx, ry): ");
scanf("%f %f", &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>
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;
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 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]);
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>
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;
40
// Draw the original line
drawline(p1, p2);
delay(500); // Delay to show the original line before clipping
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;
}
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
}
// Set top, bottom, right, left codes based on the point's coordinates
if (p.y < 100) ptemp.code[0] = '1'; // Top
else ptemp.code[0] = '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 ((p1.code[i] != '0') || (p2.code[i] != '0'))
flag = 1;
}
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>
// 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;
}
int main() {
int gd = DETECT, gm;
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
// Initialize graphics
initgraph(&gd, &gm, "c:\\turboc3\\bgi");