CG Unit-2
CG Unit-2
U
AREA FILLING
he Seed Fill Algorithm is a method in computer graphics used to fill a connected area
T
starting from a seed pixel (initial point) inside the region. It works by spreading outwards
from the seed and filling neighboring pixels until it reaches a boundary or a different
color.
fter that, it will expand using 4-connected pixels and the nearest 4 pixels are
A
turned red.
hen again, the 4 connected pixels of the newly colored pixels will be turned red
T
but here all pixels which are colored before will not be affected.
And finally, it will be filled like below −
Algorithm:
oundary (x,y,f,b)
B
{
If (get pixel (x,y)!=b
&&
Get pixel (x,y)!=f)
{
utpixel (x,y,f)
P
Boundary (x+1,y,f,b)
Boundary (x,y+1,f,b)
Boundary (x-1,y,f,b)
Boundary (x,y-1,f,b)
Boundary (x-1,y-1,f,b)
Boundary (x-1,y+1,f,b)
Boundary (x+1,y-1,f,b)
Boundary (x+1,y+1,f,b)
}
}
🔸 Function Signature
Boundary(x, y, f, b)
b: Boundary color – the color that acts as the limitfor filling.
🔸 Condition
If both conditions are true, it means the pixel is part of the area to be filled.
Advantages and Disadvantages of Boundary Fill Algorithm
n the downside, the boundary fill algorithm can beslow for large polygons, especially
O
in the recursive version, which might cause stack overflow. The fill is uncontrolled
beyond the boundary condition, so filling precision is determined by the boundary's
accuracy.
● F lood Fill is a coloring algorithm that starts from one pixel (called the seed) and
fills all the nearby pixels that have the same color as the starting pixel, replacing
them with a new color.
● Flood fill checks a pixel, and if it has the target color, it changes it to a new color
and then checks its neighbors (left, right, top, bottom – or all 8 directions). This
repeats until all connected same-color pixels are filled.
● The Flood-Fill Algorithm is Used to Fill an enclosed area with a Different Color,
just like a paint bucket tool in ms-paint.
fter that, it will expand using 4-connected pixels and the nearest 4 pixels are
A
turned red.
hen again the 4 connected pixels of the newly colored pixels will be turned red
T
but here all pixels which are colored before will not be affected.
And finally, it will be filled like below −
Types Of Flood-fill
Algorithm:
lood(x,y,n,o)
F
{
If (get pixel (x,y)==0)
{
Putpixel(x,y,n)
lood(x+1,y,n,o);
F
Flood(x,y+1,n,o);
Flood(x-1,y,n,o);
Flood(x,y-1,n,o);
Flood(x-1,y-1,n,o);
Flood(x-1,y+1,n,o);
Flood(x+1,y-1,n,o);
Flood(x+1,y+1,n,o);
}
}
lood(x, y, n, o)
F
Flood
This is a recursive function named .
Parameters:
●
x, y
: Coordinates of the current pixel.
●
n: New color to fill (the color you want to apply).
● o
: Old color (the color to be replaced — usually thecolor of the pixel at the
starting point).
o.
So that it fills all pixels with the same original color
putpixel(x, y, n);
Advantages:-
Disadvantages:-
isk of
R igher (especially large
H ower (contained by
L
overflow areas) boundary)
hescanlinealgorithmisanefficientmethodforfillingpolygonswithcolor.This
T
algorithmworksbydividingthepolygonintohorizontallines,calledscanlines.
Filling the pixels between pairs of intersections.
t each scanline, it checks for intersections with the polygon's edges. Whena
A
scanline intersects with a polygon edge, the algorithm determines the region
between pairs of intersections and fills it with color.
✅ Step-by-Step Procedure:
● B
egin from thetop y-coordinateof the polygon andmovedownwardone row at a time
(y = ymax to ymin).
● Group the intersection points intopairs: (x1, x2),(x3, x4), etc.
5. Repeat
● Move down to the next scanline (y = y - 1) and repeat the above steps.
⚠️
Special Case: When Scanline Hits a Polygon Corner
(Vertex)
ometimes the scanline goes exactly through acorner pointof the polygon (a red dot). You
S
must treat this carefully to avoidextra linesormissing fill.
🔁
Make surey1is always greater than
y2by swappingif needed (to make
edge go from top to bottom).
y2
○ Then: bigger
○ T
hen: smaller x2
(This helps in processing scan lines more efficiently)
7. Find which edges are currently active (used for filling)
y
For the current scan line , check each edge:
y1and
○ If y lies between y2
, the edge isactive
x1
○ First time: use
x = x + Ax
○ Next times: use formula
Axis calculated from the edge's slope.
here
W
x-intersect = x-intersect + Ax (xi+1=xi+
○ Ax)
✅
A (2, 8)
B (6, 2)
C (10, 6)
●
ymin = 2(from point B)
●
ymax = 8(from point A)
y = 8down to
We’ll scan from y = 2
1. A → B
2. B → C
3. C → A
● x1 = 2
✅ So we store:
Sort by:
✅
Edge A→B: 8 > 2 and ≤ 8 → active
✅
Edge C→A: 8 > 6 and ≤ 8 → active
❌ Edge B→C: 8 not between 6 and 2 → inactive
For y = 8:
No issue for y = 8 since it's the topmost point. No duplicate counting.
orted:
S
✅
[2, 10]
11. Make pairs
✅ (2, 10)
y = 7
For y = 7:
For y = 6:
● Now edge B→C becomes active
● Pairs: (0.668, 10) and (no pair for 18, ignore or check further)
✅ Final Output
t the end, you would have filled the triangle line by line from top to bottom
A
using horizontal lines between x-intersections.
y[]values to find:
We look through all
🔼 The highest y =
ymax(top of the shape)
🔽
The lowest y =ymin(bottom of the shape)
These values show where our filling should start and end.
● Pick the top point (bigger y) and bottom point (smaller y)
● Save:
y1first
1.Sort by higher
y2
2.Then by higher
Begin at the highest point of the polygon, and move down one line at a time.
y:
Check which edges are active (used) for current scan line
✅ yis between y1 and y2, include the edge
If
❌
If not, ignore it (it's not touching this line)
ll the x-values where the scan line hits the polygon are sorted from left to
A
right.
or each pair:
F
🖌️ y
Draw a horizontal line from the first x to the second x at the current .
yby 1:
ecrease
D
🔽
Go to the next lower scan line.
Repeat the steps until you reach the bottom of the polygon (
ymin
).
1.Point:-
point is the most basic element in computer graphics. It is defined by a coordinate pair
A
(x,y) in 2d or (x,y,z) in 3d . The appearance of a point in the screen is connected by its
attributes , which include its size, shape & color.
A.Point size:-
● Define how large the point appears on the screen.
● In vector Graphics, points are typically single-pixel dots.
● In Raster Graphics, we can increase the point size to make it more visible.
● Ex= A small point appears as a single pixel, while a large point may be drawn as
a square or circle.
.Point shape:-
B
A point can be represented by a different maker symbol, Such as
● DOT
● CROSS
● SQUARE
● TRIANGLE
● STAR
EX= In Scatter Plot, Different maker types are used to differentiate Data points.
C.Point Color:-
● Define the color of the point.
● Specified using RGB (RED,GREEN,BLUE) Values or Hex Codes.
● Some systems allow grayscale points for Black & White Display.
● EX= A Blue Point in RGB would be (0,0,255),while Red is (255,0,0).
2.Lines:-
lines is a Fundamental Graphics Primitive Used to Connect Points, Create Shapes
A
And Define Object Boundaries, The Appearance Of a Line is Controlled by Various Line
Attributes, Such as
A. Line type
B. Line Width
C. Line color
The Line-Type Determines the Pattern Of the Line, Weather it is Solid, Dashed, Dotted.
● efine how thick or thin a line appears on the screen.
D
● Expressed in pixels or Real world units.
● Thicker lines are used for emphasis, while thin lines are used for finer details.
● EX= In the roadmap, highways are Drawn with thicker lines, while small streets
use thin lines.
● etermines the color of the line using RGB values, CMYK, or Predefined Colors.
D
● In Monochrome Displays, Colors Are Limited to Black and white.
● In Grayscale Graphics, Different shades of gray represent varying intersities.
● EX=In Bar charts,Different Colored lines represent Different categories.
3.Curve:-
Curve is a smooth flowing line that connects points without sharp angles in computer
A
graphics, curves are widely used in
.
1 omputer-Aided Design( CAD )
C
2. Data Visualization ( Graph & Chart )
3. Animation & Game Development
4. Font Design ( Bezier Curve in Typography )
Explicit Curves D
efined by a Function of x ! y = imple Graphs like
S
F(x) parabolas
arametric
P efined as x(t),y(t) using a
D Bezier and B-spin curves
Curves Parameter ‘t’
X= In Vector Graphics, Parametric Curves Are Widely Used Bcz They allow
E
smooth scalling without pixelation.
D
● efine How Thick or Thin a Curve Appears .
● Expressed In Pixels or Real World Units(mm,inches,etc).
● Thicker Curves are Used to Highlight Important Data, While Thin Curves
Are Used For Delicate Details.
● EX= In Scientific charts, Thicker Curves Represent Major Trends, While
Thinner Curves Represent Secondary Trends.
D
● etermine the color of the curve using RGB, CMYK or predefined color models.
● Helps in Distinguishing between multiple curves in data visualization & design
application .
● E
X=In a multiple-line graph,each curve is assigned a different color to
differentiate database.
B-Spline Curve S
mooth, Flexible Curve sed in Animation &
U
Approximation Modeling.
Hermite Curve D
efined By Endpoints & sed in motion Path
U
Tangents Planning.
In Computer Graphics, A Fill Area Refers to Closed Shape ( Polygon or Region ) that is
Filled With a Specific Colors, Pattern or Shading Effect. The Attributes of a Filled Area
Determines its Appearance and Rendering Style.
.
1 D & 3D Object Rendering.
2
2. Graphical User Interfaces (GUI).
3. Data Visualization (Chart,Graph,Maps).
4. Game Design & animation.
Types Of Fill Areas:
. P
1 olygones
2. Circles & Ellipse
3. Irregular Regions
he Shapes Can Be Filled Using Different Attributes, Such as Solid Colors, Patterns,
T
Gradients and Textures.
T
● he Most Basic Attribute is SolidColor Filling.
● Colors are Defined Using RGB (Red-Green-Blue),
CMYK(Cyan-Magenta-Yellow-Black) or indexed Colors Models.
● EX= In Data Visualization, Bar Charts Use Different Fill Colors For Each
Category.
attern Fill is a Method Used in Computer Graphics to Fill a Shape or Area with a
P
Repeated Design or Texture Instead of a Solid Color.
Key Concept:
4. C
ustom Patterns:-
We can create our own patterns using a colors array ( a small grid with
different colors ).
6. Tiling:-
● When patterns are repeated to fill an area, it's called tiling (like floor
tiles)
● The Pattern keeps Repeating horizontally & vertically to cover the
shape.
he Odd-Even Test(also called the Ray Casting Algorithm)is a classic algorithm used
T
in computer graphics and computational geometry to determine whether a point lies
inside or outside a polygon.
🔍 Core Idea:
ast a horizontal ray (or any straight line) from the point in question to infinity and count
C
how many times it intersects the edges of the polygon.
● If the number of intersections is odd, the point is inside the polygon.
● If the number is even, the point is outside the polygon.
✅ Steps of the Odd-Even Test:
1. Input:
2. Process:
○ For each edge of the polygon, check if the ray intersects it.
3. Decision:
🧠 Important Considerations:
● E
dge on the ray:If the ray passes exactly througha vertex, it should not be
double-counted. A common technique is to treat the edge as intersected only if
the edge goes upward (i.e., one endpoint is below the ray and the other is at or
above it).
● H
orizontal edges:Should be ignored or handled carefullyto avoid false
intersections.