Practical No.
5
Program to fill a Polygon
Algorithm:
Step 1: Start
Step 2: Read any seed pixels position (x, y).
Step 3: Check to see if this pixel (x, y) has old interior colour. If old
colour, then set it to new colour.
Step 4: Repeat step 3 for 4 neighbouring pixels of (x, y).
Step 5: In step 3 and 4 if pixel (x, y) does not have old interior colour
then jump to step 6.
Step 6: Stop.
Program Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
int main ()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
rectangle (100,200,300,350);
flood (101,101,3,0);
getch();
}
void flood (int x,int y,int newColor, int oldColor)
{
if(getpixel(x,y)==oldColor)
{
putpixel(x,y,newColor);
flood (x+1, y,newColor, oldColor);
flood (x-1, y,newColor, oldColor);
flood (x, y+1, newColor,oldColor);
flood (x, y-1, newColor,oldColor);
}
}
Output:
Practical Related Questions:
1).Define polygon.
Ans. Polygon is a representation of the surface. It is Primitive which
is closed in nature. It is formed using many a collection of lines. It is
also called as many-sided figure. The lines combined to form Polygon
are called sides or edges The lines are obtained by combining two
vertices.
2) Explain types of polygons.
Ans. There are mainly two types of polygons:
i] Concave: A polygon is called concave of line joining any two
interior points of the polygon lies outside the polygon & an Interior
angle is greater than 180°.
ii] Convex: When a line joining any two interior points of the
polygon lies outsidethe polygon and an interior angle is less than
180°.
3) List co-ordinates of neighboring pixels in 8-connected method
for seed pixel with coordinates (x , y)
Ans. (x + 1, y) (x - 1, y) (x, y + 1) (x, y - 1) (x + 1, y + 1)
(x - 1, y - 1) (x - 1, y + 1) (x + 1, y - 1)
4) List co-ordinates of neighboring pixels in 4-connected method
for seed pixel with coordinates (x , y)
Ans. (x+1, y) (x-1, y) (x, y+1) (x, y-1)
5) Explain inside-outside test of polygon.
Ans. While filling an object, we often need to identify whether, point
is inside the object or outside the object. This method is known as
Inside outside test.
Exercise:
1. WAP to draw hexagon and fill hexagon with pink color using
flood fill algorithm with 8-connected method.
#include <stdio.h>
#include<graphics.h>
#include<conio.h>
int main ()
{
int gd = DETECT, gm;
int np= 7; int hex [14]={ 400, 50, 450, 50, 500, 100, 450, 150, 400, 150,
350, 100, 400, 50};
initgraph (&gd, & gm, "C://TURBOC3 // BGI ");
drawpoly (np, hex);
flood (401, 51, 13, 0);
getch();
closegraph ();
return 0;
}
void flood (int x, int y,int newt, int old)
{
if(getpixel(x,y) == old)
{ putpixel (x,y, newt);
flood ( x +1,y , newt, old);
flood (x-1,y,newt,old);
flood (x, y+1 , newt,old);
flood (x, y-1 , newt,old);
flood (x+1,y+1 ,newt, old);
flood ( x - 1, y - 1 ,newt, old);
flood (x-1, y + 1, newt,old);
flood ( x + 1 , y - 1 , newt, old);
}
}
Output:
2. WAP to draw triangle(use line function) and fill it with blue color
using flood fill algorithm with 4-connected method.
#include<stdio.h>
#include <conio.h>
#include <graphics.h>
void flood(int,int,int,int);
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/TURBOC3/bgi");
line(100, 100, 150, 150);
line(150, 150, 50, 150);
line(50, 150, 100, 100);
flood(100,125,10,0);
getch();
}
void flood(int x,int y,int fillColor, int defaultColor)
{
if(getpixel(x,y)==defaultColor)
{
putpixel(x,y,fillColor);
flood(x+1,y,fillColor,defaultColor);
flood(x-1,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor);
flood(x,y-1,fillColor,defaultColor);
}
}
Output:
Dated Signature of
Marks Obtained
Teacher
Process Product
Total(25)
Related(10) Related(15)
Practical No. 6
Program to fill polygon using boundary fill
algorithm
Algorithm:
Step 1: Start.
Step 2: Initialize the value of seed point (x,y), fill color (fcolor) and
default color (dcolor).
Step 3: Define the boundary values of polygon.
Step 4: Check if the current seed point is of default color, then
repeat the steps 4 and 5 till the boundary pixels are reached.
Step 5: Change the default color with the fill color at the seed point.
Step 6: Recursively follow the procedure with four/eight
neighborhood points.
Step 7: Stop.
Program Code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void BOUNDARY_FILL (int x,int y,int fillcolor,int boundarycolor)
{
if(getpixel(x,y)!=fillcolor && getpixel(x,y)!=boundarycolor)
{
putpixel(x, y, fillcolor);
BOUNDARY_FILL (x+1, y, fillcolor, boundarycolor);
BOUNDARY_FILL (x-1, y, fillcolor, boundarycolor);
BOUNDARY_FILL (x, y+1, fillcolor, boundarycolor);
BOUNDARY_FILL (x, y-1, fillcolor, boundarycolor);
}
}
int main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "C://TURBOC3//BGI");
int x=300, y=300, fillcolor, boundarycolor;
int left=150, top=250, right=500, bottom=450;
rectangle(left, top, right, bottom);
BOUNDARY_FILL (x, y, 3, 15);
getch( );
closegraph();
return 0;
}
Output:
Practical Related Questions:
1) Compare 4-Connected & 8-Connected method to fill polygon.
• 4 connected polygons: In this technique 4-connected pixels, we are
putting the pixels above, below, to the right, and to the left side of the
current pixels and this process will continue until we find a boundary
with different color.
• 8 connected polygon: We are putting pixels above, below, right and
left side of the current pixels as we were doing in 4-connected
technique. We are also putting pixels in diagonals so that entire area
of the current pixel is covered. This process will continue until we
find a boundary with different color.
2) Identify type of polygon in following diagram.
Ans.
1. Convex Polygon
2. Concave Polygon
3. Convex Polygon
4. Concave Polygon
3) Give the basic difference between flood fill and boundary fill.
Ans.
Exercise:
1) Write a program to draw a pentagon with red color using boundary
fill algorithm with 8-way connected method.
#include <conio.h>
#include <graphics.h>
void BOUNDARY_FILL (int x,int y,int fillcolor,int boundarycolor)
{
if(getpixel(x,y)!=fillcolor && getpixel(x,y)!=boundarycolor)
{
putpixel(x, y, fillcolor);
BOUNDARY_FILL (x+1, y, fillcolor, boundarycolor);
BOUNDARY_FILL (x, y+1, fillcolor, boundarycolor);
BOUNDARY_FILL (x-1, y, fillcolor, boundarycolor);
BOUNDARY_FILL (x, y-1, fillcolor, boundarycolor);
}
}
int main ()
{
int gd=DETECT, gm;
int x=81, y=101, fillcolor, boundarycolor;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
line (80, 100, 180, 40);
line (180, 40, 280, 100);
line (280, 100, 250, 200);
line (250, 200, 120, 200);
line (120, 200, 80, 100);
BOUNDARY_FILL (x, y, 4, 15);
getch();
closegraph();
return 0;
}
Output:
2)Write a program to draw a Trapeoid with blue color using boundary
fill algorithm with 4-way connected method.
#include<conio.h>
#include<graphics.h>
void BOUNDARY_FILL(int x,int y,int fillcolor,int boundarycolor)
{
if(getpixel(x,y)!=fillcolor && getpixel(x,y)!=boundarycolor)
{
putpixel(x, y, fillcolor);
BOUNDARY_FILL(x+1, y, fillcolor, boundarycolor);
BOUNDARY_FILL(x, y+1, fillcolor, boundarycolor);
BOUNDARY_FILL(x-1, y, fillcolor, boundarycolor);
BOUNDARY_FILL(x, y-1, fillcolor, boundarycolor);
}
}
int main()
{ int gd=DETECT, gm;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
int x=81, y=101, fillcolor, boundarycolor;
line (80, 100, 180, 40);
line (180, 40, 280, 100);
line (280, 100, 250, 200);
line (250, 200, 80, 100);
BOUNDARY_FILL(x, y, 4, 15);
getch();
closegraph();
return 0;
}
Output:
Dated Signature of
Marks Obtained
Teacher
Process Product
Total(25)
Related(10) Related(15)
Practical No. 7
Program for two dimensional transformations
(translation & scaling)
Algorithm:
Step 1: Start
Step 2: Input the object coordinates.
Step 3: For Translation:
a) Enter the translation factor tx and ty.
b) Move the original position(x,y) to a new position
(x1, y1).
c) Display the object after translation.
Step 4: For Scaling:
a) Input the scaled factors sx and sy.
b) Transform the coordinates (x1, y1)
i.e. x1=x.sx & y1=y.sx
c) Display the object after scaling.
Step 5: Stop.
Program Code:
1.Translation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x, y, xe, ye, x1, y1, x2, y2,tx,ty;
clrscr();
printf("Enter the two cordinates of the line segment.");
scanf("%d%d%d%d", &x, &y, &xe,&ye);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
tx=200;
ty=0;
printf("Original line");
line(x,y,xe,ye);
x1=x+tx;
y1=y+ty;
x2=xe+tx;
y2=ye+ty;
outtextxy(400,10,"After translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}
Output:
2)Scaling:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
float sx=4,sy=1.2;
float x1,x2,x3,y1,y2,y3;
x1=100,x2=150,x3=125;
y1=250,y2=250,y3=175;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:/turboc3/bgi");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
outtextxy(100,252,"original traingle");
//scaling
x1=x1*sx;
x2=x2*sx;
x3=x3*sx;
y1=y1*sy;
y2=y2*sy;
y3=y3*sy;
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
outtextxy(x1,377,"after scaling);
getch();
closegraph();
}
Output:
Practical Related Questions:
1)Write the transformation matrix for 2D Translation
| |
1 0 0
[X’ Y’ 1] =[X Y 1] + 0 1 0
tx ty 1
2)Write the transformation matrix for 2D scaling
| |
1 0 0
[X’ Y’ 1] =[X Y 1] + 0 1 0
tx ty 1
3)What does scaling transformation do?
Scaling is the transformation that is used to change the
object's size. The operation is carried out for polygon by
multiplying the coordinate value(X, Y) of each vertex with the
scaling factors
4)Whether size of object remains same or changed in case of
translation?
A translation is when a geometric figure slides up-down
left or right on the co-ordinate plane. The figure moves its
location but doesn’t change its size or shape when you perform
translations.
Exercise:
1) Translate Polygon with Co-ordinates A(2,5), B(7,10), C(10,2) by 3
units in x direction and 4 units in y direction.
Given: A (2,15), B. (7,10), C(10,2), tx=3, ty=4
| || |
1 0 0 1 0 0
T= 0 1 0 = 0 1 0
tx ty 1 3 4 1
| || | | |
2 5 1 1 0 0 (2+3) (5+4 ) (0+1)
[x’,y’] = 7 10 1 0 1 0 = (7+3) (10+4 ) (0+1)
10 2 1 3 4 1 (10+3) (2+4 ) (0+1)
A’=(x1’,y1’)=(5,9)
B’=(x2’,y2’)=(10,14)
C’=(x3’,y3’)=(13,6)
2) Scale the polygon with co-ordinates A(2,5), B(7,10), C(10,2) by 2
units in x direction and 2 units in Y direction.
Given : A(2,5) ,B(7,10), C(10,2), sx=2, sy = 2.
For Scaling we have
| || |
sx 0 0 2 0 0
S= 0 sy 0 = 0 2 0
0 0 1 0 0 1
[ ] | || | | |
A' 2 5 1 2 0 0 (2∗2) (5∗2) 1
B ' = 7 10 1 0 2 0 = (7∗2) (10∗2) 1
C ' 10 2 1 0 0 1 (10∗2) (2∗2) 1
[]| |
A' 4 10 1
B' = 14 20 1
C' 20 4 1
A’= (x1’, y1’)=(4,10)
B’=(x2’,y2’)=(14,20)
C’=(x3’,y3’)=(20,4)
3)Give a 3x3 homogeneous co-ordinates transformations matrix for
each of the following translations.
i)Shift the image to right 3 units
| |
1 0 0
T= 0 1 0 (here,tx=3 and ty=0)
3 0 1
ii) Shift the image to up 2 units
| |
1 0 0
T= 0 1 0 (here,tx=0 and ty=2)
0 2 1
iii)Move the image down 1/2 unit and right 1 units
| |
1 0 0
T= 0 1 0 (here,tx=1 and ty=0.5)
1 0.5 1
iv) Move the image down 2/3 unit and left 4 units
| |
1 0 0
T= 0 1 0 (here,tx=(-4) and ty=0.6)
(−4 ) 0.6 1
4)Find the transformation matrix that transforms. given Square
ABCD to half its size with centre Still remaining at Same position.
The co-ordinates of Square are A (1,1), B(3,1), C (3,3) and D(1,3) and
centre at (2,2). Also find resultant co-ordinates of Square.
Given :- A (x1,Y1) = (1,1),B(X2, Y2)=(3, 1), C(Y3,Y3) = (3,3)
D(X4, Y4) = (1,3).
Sx=Sy= 0.5
Pivot Point = (2,2)
Dated Signature of
Marks Obtained
Teacher
Process Product
Total(25)
Related(10) Related(15)