COMPUTER GRAPHICS LABORATORY AY 2022-2023 8 Practical
COMPUTER GRAPHICS LABORATORY AY 2022-2023 8 Practical
COMPUTER GRAPHICS-LAB
Semester – VII
RAJARSHI SHAHU
COLLEGE OF ENGINEERING
INDIA.
RAJARSHI SHAHU
COLLEGE OF ENGINEERING, BULDANA
CERTIFICATE
INDEX
Write a program to
generate
10 snowflake using concept of fractals.
Sign of Faculty
EXPERIMENT NO.1
PRIOR CONCEPT:
ALGORITHM:
(dy)
Then step = abs
(dx) Else
Step7: xinc=dx/step
yinc=dy/step
assign x =
x1 assign y
= y1
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round
PROGRAM:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
BGI"); setcolor(WHITE);
line:"); scanf("%d%d",&x0,&y0);
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:
CONCLUSION:
In this way we have studied a program to draw line using DDA algorithm.
EXPERIMENT NO:-2
PRIOR CONCEPT:
ALGORITHM:
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
Step9: Increment x = x + 1
Step11: Go to step 7
PROGRAM:
#include<stdio.h>
#include<conio.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; }}
void main()
y1; clrscr();
y1); getch();}
OUTPUT:
CONCLUSION:
EXPERIMENT NO:-3
PRIOR CONCEPT:
THEORY:
a) Scaling of traingle
Scaling : It is used to change the size of objects. The change is done using scaling factors. There are
two scaling factors, i.e. Sx in x direction Sy in y-direction. If the original position is x and y. Scaling
factors are Sx and Sy .
b) Translation : It is the straight line movement of an object from one position to another is called
Translation. Here the object is positioned from one coordinate location to another.
Translation of point: To translate a point from coordinate position (x, y) to another (x1 y1), we add
algebraically the translation distances Tx and Ty to original coordinate.
x1=x+Tx
y1=y+Ty
c) Rotation : It is a process of changing the angle of the object. Rotation can be clockwise or
anticlockwise. For rotation, we have to specify the angle of rotation and rotation point. Rotation point
is also called a pivot point. It is print about which object is rotated.
Types of Rotation
Anticlockwise - The positive value of the rotation angle rotates an object in a anti-clockwise
direction.Counterclockwise - The negative value of the pivot point (rotation angle) rotates an object
in a clockwise direction.
PROGRAM:
a) Scaling of traingle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int temp[2][1] = { 0 };
+)
[j]);
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
// Triangle before
x[1], y[1]);
};
int p[2][1];
p[0][0] = x[i];
p[1][0] = y[i];
fC(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
COMPUTER GRAPHICS Lab Page
Rajarshi Shahu College of Engineering,
Buldana
void main()
int sx = 2, sy =
detectgraph(&gd, &gm);
initgraph(&gd, &gm,"
");
scale(x, y, sx,sy);
getch();
a)OUTPUT:
b) Translation:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int x3,y3,x1,y1,x2,y2,tx,ty;
clrscr();
initgraph(&gd,&gm , "c:\\turboc3\\bgi");
scanf("%d%d",&x1,&y1);
"); scanf("%d%d",&x2,&y2);
"); scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
"); scanf("%d%d",&tx,&ty);
setcolor(BLUE);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
line(x3+tx,y3+ty,x1+tx,y1+ty);
getch();
closegraph();}
b)OUTPUT:
c) Rotation:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
int gd=0,gm,x1,y1,x2,y2,x3,y3;
setcolor(RED);
setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
y1); getch();
scanf("%lf", &angle);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 *
c); x2 = floor(x2 * c + y2 *
s);
COMPUTER GRAPHICS Lab Page
Rajarshi Shahu College of Engineering,
Buldana
y2 = floor(-x2 * s + y2 *
c); x3 = floor(x3 * c + y3 *
s); y3 = floor(-x3 * s + y3
* c); cleardevice();
line(x2,y2, x3,y3);
y1); getch();
closegraph();
C)OUTPUT:
CONCLUSION:
In this way we have studied a program for 2-D transformations, a) Scaling b) Translation c) Rotation.
EXPERIMENT NO:-4
PRIOR CONCEPT:
THEORY:
In this program, we will create scenery with a hut, sun, and rainfall. In this scenery, a man holds an
umbrella and walks through the ground. When a key is pressed, the rain stops, and a rainbow
appears.
Ground Level: To create a ground line, we will first use GroundY ScreenHeight to define the
ground level.
Hut: The hut will be built for the scenery. For the base and roof, rectangles and lines will be
used. The hut will be colorful.
Man and Umbrella: Build a man holding an umbrella. A circle forms the head of the man, and
lines make up his body. Lines representing the legs have variable coordinates so it appears as if
the man is walking on the ground. Using the pieslice function we will create the body of an
umbrella and lines for the stick.
Rain: Create rain using the rand() function to generate random pixels and create small lines to
create a rain effect
Circle: To create a sun in the top left corner we will use a circle.
Rainbow: Create a rainbow in the top left corner using the arc function. The delay function
gives it an animation.
Rainfall continues until any key is pressed.
The rainbow appears when a key is pressed.
PROGRAM:
// C program to implement
// the above approach
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#define ScreenWidth getmaxx()
#define ScreenHeight getmaxy()
#define GroundY ScreenHeight * 0.75
int ldisp = 0;
// Creating a hut
void hut()
{
setcolor(WHITE);
rectangle(150, 180, 250, 300);
rectangle(250, 180, 420, 300);
rectangle(180, 250, 220, 300);
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
}
{
int x, y, i;
circle(ScreenWidth - 100,
50, 30);
setfillstyle(SOLID_FILL,
YELLOW);
floodfill(ScreenWidth - 100,
50, WHITE);
setcolor(i / 10);
// Driver code
void main()
{
int gd = DETECT, gm, x = 0;
DrawManAndUmbrella(x, ldisp);
rainbow();
getch();
}
OUTPUT:
CONCLUSION:
EXPERIMENT NO:-5
PRIOR CONCEPT:
THEORY:
1. We will process the polygon edge after edge, and store in the edge Table.
2.Storing is done by storing the edge in the same scanline edge tuple as
the lowermost point's y-coordinate value of the edge.
3.After addition of any edge in an edge tuple, the tuple is
sorted using insertion sort, according to its xofymin value.
4.After the whole polygon is added to the edge table,
the figure is now filled.
5.Filling is started from the first scanline at the bottom,
and continued till the top.
6.Now the active edge table is taken and the following things
are repeated for each scanline:
i. Copy all edge buckets of the designated scanline
to the active edge tuple
ii. Perform an insertion sort according
to the xofymin values
iii. Remove all edge buckets whose ymax is equal
or greater than the scanline
iv.Fillup pairs of edges in active tuple, if any vertex is got,
follow these instructions:
o If both lines intersecting at the vertex are on
the same side of the scanline, consider it as two points.
o If lines intersecting at the vertex are at
opposite sides of the scanline, consider it as only one point.
v. Update the xofymin by adding slopeinverse for each bucket.
PROGRAM:
// CPP program to illustrate
// Scanline Polygon fill Algorithm
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#define maxHt 800
#define maxWd 600
#define maxVer 10000
FILE *fp;
// Scanline Function
void initEdgeTable()
{
int i;
for (i=0; i<maxHt; i++)
{
EdgeTable[i].countEdgeBucket = 0;
}
ActiveEdgeTuple.countEdgeBucket = 0;
}
if (tup->countEdgeBucket)
printf("\nCount %d-----\n",tup->countEdgeBucket);
printf(" %d+%.2f+%.2f",
tup->buckets[j].ymax, tup->buckets[j].xofymin,tup->buckets[j].slopeinverse);
}
}
void printTable()
{
int i,j;
printTuple(&EdgeTable[i]);
}
}
(receiver->countEdgeBucket)++;
if (x2==x1)
{
minv=0.000000;
}
else
{
m = ((float)(y2-y1))/((float)(x2-x1));
minv = (float)1.0/m;
printf("\nSlope string for %d %d & %d %d: %f",x1,y1,x2,y2,minv);
}
if (y1>y2)
{
scanline=y2;
ymaxTS=y1;
xwithyminTS=x2;
}
else
{
scanline=y1;
ymaxTS=y2;
xwithyminTS=x1;
}
// the assignment part is done..now storage..
storeEdgeInTuple(&EdgeTable[scanline],ymaxTS,xwithyminTS,minv);
Tup->countEdgeBucket--;
i--;
}
}
}
void ScanlineFill()
{
/* Follow the following rules:
1. Horizontal edges: Do not include in edge table
2. Horizontal edges: Drawn either on the bottom or on the top.
3. Vertices: If local max or min, then count twice, else count
once.
4. Either vertices at local minima or at local maxima are drawn.*/
printTuple(&ActiveEdgeTuple);
//3. Fill lines on scan line y by using pairs of x-coords from AET
j = 0;
FillFlag = 0;
coordCount = 0;
x1 = 0;
x2 = 0;
ymax1 = 0;
ymax2 = 0;
while (j<ActiveEdgeTuple.countEdgeBucket)
{
if (coordCount%2==0)
{
x1 = (int)(ActiveEdgeTuple.buckets[j].xofymin);
ymax1 = ActiveEdgeTuple.buckets[j].ymax;
if (x1==x2)
{
/* three cases can arrive-
1. lines are towards top of the intersection
2. lines are towards bottom
3. one line is towards top and other is towards bottom
*/
if (((x1==ymax1)&&(x2!=ymax2))||((x1!=ymax1)&&(x2==ymax2)))
{
x2 = x1;
ymax2 = ymax1;
}
else
{
coordCount++;
}
}
else
{
coordCount++;
}
}
else
{
x2 = (int)ActiveEdgeTuple.buckets[j].xofymin;
ymax2 = ActiveEdgeTuple.buckets[j].ymax;
FillFlag = 0;
}
}
else
{
coordCount++;
FillFlag = 1;
}
if(FillFlag)
{
//drawing actual lines...
glColor3f(0.0f,0.7f,0.0f);
glBegin(GL_LINES);
glVertex2i(x1,i);
glVertex2i(x2,i);
glEnd();
glFlush();
j++;
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,maxHt,0,maxWd);
glClear(GL_COLOR_BUFFER_BIT);
}
void drawPolyDino()
{
glColor3f(1.0f,0.0f,0.0f);
int count = 0,x1,y1,x2,y2;
rewind(fp);
while(!feof(fp) )
{
count++;
if (count>2)
{
x1 = x2;
y1 = y2;
count=2;
}
if (count==1)
{
fscanf(fp, "%d,%d", &x1, &y1);
}
else
{
fscanf(fp, "%d,%d", &x2, &y2);
printf("\n%d,%d", x2, y2);
glBegin(GL_LINES);
glVertex2i( x1, y1);
glVertex2i( x2, y2);
glEnd();
storeEdgeInTable(x1, y1, x2, y2);//storage of edges in edge table.
glFlush();
}
}
void drawDino(void)
{
initEdgeTable();
drawPolyDino(); printf("\
nTable"); printTable();
glutMainLoop();
fclose(fp);
}
OUTPUT:
CONCLUSION:
EXPERIMENT NO:-6
TITLE: Write a program to clip line using following algorithm : Cohen-Sutherland algorithm
PRIOR CONCEPT:
ALGORITHM:
Step 1 : Assign a region code for two endpoints of given line.
Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region codes.
Step 3.1 : If the result is not 0000, then given line is completely
outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line
that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the
rectangular boundary (based on region code).
Step 3.2.3 : Replace endpoint with the intersection point
and update the region code.
Step 3.2.4 : Repeat step 2 until we find a clipped line either
trivially accepted or trivially rejected.
Step 4 : Repeat step 1 for other lines
PROGRAM:
#include<stdio.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;
printf("\nEnter x1 and y1\n");
scanf("%d %d",&p1.x,&p1.y); printf("\
nEnter x2 and y2\n"); scanf("%d
%d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0:
drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1:
drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{ line(150,100,450,100
);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
OUTPUT:
Before clipping
After clipping
CONCLUSION:
EXPERIMENT NO:-7
PRIOR CONCEPT:
To Learn Graphics.
THEORY:
Create two rectangles, one inside the other, using the rectangle() function to act as the outer
outline of the clock & the other is the inner outline of the clock.
Color the space between the two rectangles brown using setfillstyle() and floodfill() functions
Implement a circle inside the inner rectangle using the circle() function
Color all other parts leaving the circle with dark gray
using setfillstyle() and floodfill() functions.
In the circle, insert all the digits using settextstyle() and outtextxy() functions.
Calculate the coordinates of the digits.
Implement the pendulum with two lines by using the line() function & another circle by using
the circle() function which will act as the bob.
Color all of them black using the setfillstyle() and floodfill() functions again.
Implement hour, minute & second hands by using the line() function.
Color the objects individually by using the setcolor() function.
PROGRAM:
// C program toc draw the pendulum clock
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
void main()
{
int gd = DETECT, gm;
// Initialize of gdriver
initgraph(&gd, &gm, "C:\\"
"turboc3\\bgi");
// Clock Outline
// Inserting Digits
settextstyle(6, 0, 3);
outtextxy(697, 100, "01");
outtextxy(730, 140, "02");
outtextxy(742, 190, "03");
outtextxy(721, 240, "04");
outtextxy(690, 280, "05");
outtextxy(630, 300, "06");
outtextxy(578, 280, "07");
outtextxy(540, 240, "08");
outtextxy(530, 190, "09");
outtextxy(537, 140, "10");
outtextxy(569, 100, "11");
outtextxy(630, 80, "12");
// Pendulum Bob
circle(650, 546, 20);
OUTPUT:
CONCLUSION:
EXPERIMENT NO:-8
TITLE: Write a program to draw following type of curve-Koch curve, Bezier curves
PRIOR CONCEPT:
THEORY:
PROGRAM:
/ C program to implement
// Cubic Bezier Curve
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<SDL2/SDL.h>
{
xu = pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]
+pow(u,3)*x[3];
yu = pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]
+pow(u,3)*y[3];
SDL_RenderDrawPoint(renderer , (int)xu , (int)yu) ;
}
}
*/
if(SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer) == 0)
{
SDL_bool done = SDL_FALSE;
int i = 0 ;
int x[4] , y[4] , flagDrawn = 0 ;
while (!done)
{
SDL_Event event;
if(event.type == SDL_MOUSEMOTION)
{
/*get x and y positions from motion of mouse*/
xnew = event.motion.x ;
ynew = event.motion.y ;
int j ;
/*clean up SDL*/
SDL_Quit();
return 0;
}
OUTPUT:
CONCLUSION: