0% found this document useful (0 votes)
51 views32 pages

CG Unit-2

The document explains the Seed Fill Algorithm in computer graphics, which is used to fill connected areas starting from a seed pixel. It details two types of algorithms: Flood Fill, which fills based on region color, and Boundary Fill, which fills until a boundary color is reached. Additionally, it covers the Scanline Algorithm for polygon filling, outlining its step-by-step process and special cases for handling intersections.

Uploaded by

AKASH NAYAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views32 pages

CG Unit-2

The document explains the Seed Fill Algorithm in computer graphics, which is used to fill connected areas starting from a seed pixel. It details two types of algorithms: Flood Fill, which fills based on region color, and Boundary Fill, which fills until a boundary color is reached. Additionally, it covers the Scanline Algorithm for polygon filling, outlining its step-by-step process and special cases for handling intersections.

Uploaded by

AKASH NAYAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

‭ NIT-2‬

U
‭AREA FILLING‬

‭Seed Fill Algorithm‬

‭ 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.‬

‭Types of Seed Fill Algorithms:‬

‭1.‬ ‭Flood Fill Algorithm‬


‭Fills all connected pixels of the same color as the seed.‬
‭2.‬ ‭Boundary Fill Algorithm‬
‭Fills pixels until a specific boundary color is encountered.‬

‭Boundary Fill Algorithm‬


‭●‬ T ‭ he boundary filling algorithm is applied for filling an area of connected pixels‬
‭with a specific color other than the boundary color. It requires a boundary of the‬
‭same color. It fills all the connected areas until it reaches a boundary.‬
‭●‬ ‭The algorithm starts from a point (usually inside the area to be filled) and‬
‭changes the color of all adjacent pixels that match the starting pixel.‬
‭●‬ ‭In polygon filling, the goal is to color the inside of a polygon. It assumes‬
‭the boundary color is different and it remains intact.‬
‭●‬ ‭The‬‭Boundary-fill Algorithm is Used in Computer Graphics‬‭to fill a Closed Shape‬
(‭ like a circle or any drawn figure) With Color.‬
‭ ‬ ‭It starts filling from an inside point & spreads outward until it reaches the‬

‭Boundary Of the Shape.‬

‭How does it work ?‬

‭ . Choose a starting point inside the shape.‬


1
‭2. Check the neighboring pixels:‬
‭●‬ ‭If the neighboring pixel is not a boundary color, fill it with the chosen color.‬
‭●‬ ‭Repeat this process for all connected pixels until the whole shape is filled.‬
‭ et us see the example of how 4-connected boundary filling is working with. Initially we‬
L
‭have a polygon with a blue boundary.‬

‭Now, pick a seed at the center and its color is red.‬

‭ 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 Boundary -fill‬

‭There are two ways to check & fill neighboring pixels.‬

‭1. 4-Connected Fill ( Simple, But may leave Gaps )‬


‭●‬ ‭Fills pixels in four directions: Left,Right,Up,Down.‬
‭●‬ ‭Might Not Fill Complex Shapes Properly Bcz Diagonal pixels are Ignored.‬

‭2. 8-Connected Fill ( More Complete Fill )‬


‭●‬ ‭Fills pixels in Eight Directions : Left, Right, Up, Down & The Four Diagonals.‬
‭●‬ ‭Better For Complex Shapes Since It Ensures All Areas Get Filled.‬
‭Recursive vs Efficient Approach‬

‭ ecursive Approach:‬‭The algorithm Keeps Calling Itself‬‭for Every Neighboring Pixel ,‬


R
‭Which may Cause too much memory usage .‬

‭ fficient Approach:‬ ‭Instead of checking each pixel‬‭Separately , the algorithm fills‬


E
‭Whole Horizontal lines (Spans ) at once, Reducing the no of Recursive Laws.‬

‭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)‬

‭x, y‬‭: Coordinates of the current pixel (seed point).‬

‭f‬‭: Fill color – the color you want to apply to the‬‭region.‬

‭b‭:‬ Boundary color – the color that acts as the limit‬‭for filling.‬

‭🔸 Condition‬

‭if (getpixel(x, y) != b && getpixel(x, y) != f)‬

‭●‬ ‭This checks whether the current pixel is:‬

‭○‬ ‭Not the boundary color (‬‭


b‭)‬‬

‭○‬ ‭Not already filled with the fill color (‬‭


f‭)‬‬

‭If both conditions are true, it means the pixel is part of the area to be filled.‬
‭Advantages and Disadvantages of Boundary Fill Algorithm‬

‭ he boundary fill algorithm‬‭is easy to understand‬‭and simple to implement. It can‬


T
‭handle polygons with complex and irregular shapes, as long as the boundaries are‬
‭well-defined.‬

‭ n the downside‬‭, the boundary fill algorithm can be‬‭slow 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.‬

‭Flood Fill Algorithm‬

‭●‬ 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.‬

‭How does it work ?‬

‭1.Choose a starting pixel.‬


‭●‬ ‭Pick a pixel inside the area you want to fill.‬

‭2.Check the Pixel’s color.‬


‭●‬ ‭If the pixel has the original color (The one we‬‭want to replace),continue.‬
‭●‬ ‭If the pixel is a Boundary or Different color, stop.‬

‭3. Change the Pixels color.‬


‭●‬ ‭Replace it with the new color.‬

‭4. Move to Neighboring pixels.‬


‭●‬ ‭The algorithm moves left, right, up & down (4-Connected).‬
‭●‬ ‭If using 8-connected , it moves diagonally too.‬
‭5. Repeat until the Entire Area is Filled .‬

‭Now, pick a seed at the center and its color is red.‬

‭ 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‬

‭There are two ways to check & fill neighboring pixels.‬

‭1. 4-Connected Fill ( Simple, But may leave Gaps )‬


‭●‬ ‭Fills pixels in four directions: Left,Right,Up,Down.‬
‭●‬ ‭Might Not Fill Complex Shapes Properly Bcz Diagonal pixels are Ignored.‬

‭2. 8-Connected Fill ( More Complete Fill )‬


‭●‬ ‭Fills pixels in Eight Directions : Left, Right, Up, Down & The Four Diagonals.‬
‭●‬ ‭Better For Complex Shapes Since It Ensures All Areas Get Filled.‬

‭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 the‬‭color of the pixel at the‬
‭starting point).‬

‭if (getpixel(x, y) == 0)‬

(x, y)‬‭is color‬‭


‭This checks if the pixel at‬‭ 0‭.‬‬

o‭.‬‬
‭So that it fills all pixels with the same original color‬‭

‭putpixel(x, y, n);‬

(x, y)‬‭to the new color‬‭


‭Changes the pixel at‬‭ n‭.‬‬
‭Advantages and Disadvantages of Flood Fill Algorithm‬

‭Advantages:-‬

‭1.‬ ‭Simple to implement‬‭− The algorithm is easy to code‬‭and understand.‬


‭2.‬ ‭Efficient for irregular regions‬‭− It can fill irregular‬‭shapes where the‬
‭boundaries are not clear-cut.‬

‭Disadvantages:-‬

‭1.‬ ‭Memory intensive −‬‭Flood-fill requires significant‬‭memory, especially‬


‭ hen filling large areas or using a recursive approach.‬
w
‭2.‬ ‭Slow performance‬‭− The algorithm can be slower compared‬‭to other‬
‭filling algorithms, especially if the region is large.‬

‭Feature‬ ‭Flood-Fill‬ ‭Boundary-Fill‬

‭Fills based on‬ ‭Region color‬ ‭Boundary color‬

‭Stops when‬ ‭ ifferent color pixel is‬


D ‭Boundary color is reached‬
‭reached‬

‭Common use‬ ‭ eplacing a specific color‬


R ‭Filling closed shapes‬
‭region‬

‭ isk of‬
R ‭ igher (especially large‬
H ‭ ower (contained by‬
L
‭overflow‬ ‭areas)‬ ‭boundary)‬

‭Requires‬ ‭Background (region) color‬ ‭Boundary color‬


‭Scan Line Algorithm‬

‭ he‬‭scanline‬‭algorithm‬‭is‬‭an‬‭efficient‬‭method‬‭for‬‭filling‬‭polygons‬‭with‬‭color.‬‭This‬
T
‭algorithm‬‭works‬‭by‬‭dividing‬‭the‬‭polygon‬‭into‬‭horizontal‬‭lines,‬‭called‬‭scanlines‬‭.‬
‭Filling the pixels between pairs of intersections.‬

‭Basics of Scanline Polygon Filling‬


‭ canline‬‭filling‬‭is‬‭the‬‭process‬‭of‬‭coloring‬‭the‬‭interior‬‭pixels‬‭of‬‭a‬‭polygon‬‭by‬‭using‬
S
‭horizontal‬ ‭lines,‬ ‭known‬ ‭as‬ ‭scanlines.‬ ‭The‬ ‭algorithm‬ ‭operates‬ ‭by‬ ‭moving‬ ‭from‬
‭the bottom of the polygon to the top, line by line.‬

‭ t‬ ‭each‬ ‭scanline,‬ ‭it‬ ‭checks‬ ‭for‬ ‭intersections‬ ‭with‬ ‭the‬ ‭polygon's‬ ‭edges.‬ ‭When‬‭a‬
A
‭scanline‬ ‭intersects‬ ‭with‬ ‭a‬ ‭polygon‬ ‭edge,‬ ‭the‬ ‭algorithm‬ ‭determines‬ ‭the‬ ‭region‬
‭between pairs of intersections and fills it with color.‬

‭🖋️ How Scanline Filling Works – Step-by-Step‬


‭🧠 Imagine This:‬
‭ hink of drawing a polygon on paper using a pen that moves‬‭horizontally line by line‬‭(like a‬
T
‭scanner). Each line is called a‬‭scanline‬‭.‬

‭✅ Step-by-Step Procedure:‬

‭1. Start from the Top‬

‭●‬ B
‭ egin from the‬‭top y-coordinate‬‭of the polygon and‬‭move‬‭downward‬‭one row at a time‬
‭(y = ymax to ymin).‬

‭2. Check Each Scanline‬

‭●‬ ‭For each horizontal scanline (y):‬

‭○‬ ‭Find where it crosses the polygon edges‬‭(called intersection‬‭points).‬

‭○‬ ‭Mark these intersection points (blue dots).‬

‭3. Sort Intersection Points‬

‭●‬ ‭Sort the x-values of the intersection points‬‭from‬‭left to right‬‭.‬

‭4. Draw Between Pairs‬

‭●‬ ‭Group the intersection points into‬‭pairs‬‭: (x1, x2),‬‭(x3, x4), etc.‬

‭●‬ ‭For each pair, draw a horizontal line‬‭between them‬‭.‬

‭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 a‬‭corner point‬‭of the polygon (a red dot). You‬
S
‭must treat this carefully to avoid‬‭extra lines‬‭or‬‭missing fill‬‭.‬

‭👇 Here's How to Handle:‬

‭📍 Case 1: Same-Side Intersections‬

‭●‬ ‭Both edges from the vertex are either‬‭above‬‭or‬‭below‬‭the scanline.‬

‭●‬ ‭✅ Treat the vertex as‬‭two separate intersection points‬‭.‬

‭📍 Case 2: Opposite-Side Intersections‬

‭●‬ ‭One edge goes‬‭up‬‭, the other goes‬‭down‬‭from the vertex.‬

‭●‬ ‭✅ Treat the vertex as‬‭only one intersection point‬‭.‬

‭🔄 Steps in Simple Language:‬

‭1.‬ ‭Input number of corners (vertices)‬


‭Ask the user how many corners the polygon has (‬‭
n‬ ‭).‬

‭2.‬ ‭Input coordinates of all corners‬


x‬‭and‬‭
‭Get the‬‭ y‬‭positions of all‬‭
n‬‭corners. Store‬‭them in two arrays:‬‭
x[]‬
y[]‬
‭and‬‭ ‭.‬

‭3.‬ ‭Find top and bottom of the polygon‬


‭Find the‬‭minimum y‬‭(bottom of polygon)(‬‭ymin)‬‭and‬‭maximum y‬‭(top‬
‭of polygon)(‬‭ymax)‬‭. These help define the vertical‬‭range of scan lines.‬

‭4.‬ ‭Store edge information‬


‭For every edge (line between two corners), store:‬

‭○‬ ‭x of the starting point (‬‭


x1‬‭)‬

‭○‬ ‭y of the upper point (‬‭


y1‬‭) and lower point (‬‭
y2‬‭)‬

‭○‬ ‭x-increment value (‬‭


Ax‬
‭) to move horizontally between‬‭scan lines‬

‭What I meant to say:‬


"‭ Ax tells us how much the x-position of an edge's intersection point‬
‭changes as we go from one scan line to the next downward."‬

🔁
‭ Make sure‬‭y1‬‭is always greater than‬‭
y2‬‭by swapping‬‭if needed (to make‬
‭edge go from top to bottom).‬

‭5.‬ ‭Sort edges smartly‬


‭Sort all edges using:‬

y1‬‭(top edge comes first)‬


‭○‬ ‭First: bigger‬‭

y2‬
‭○‬ ‭Then: bigger‬‭

‭○‬ T
‭ hen: smaller‬‭ x2‬
‭(This helps in processing scan lines more efficiently)‬

‭6.‬ ‭Start from the top (y = ymax)‬


‭Begin the scan line from the highest y-value and move downward.‬

‭7.‬ ‭Find which edges are currently active (used for filling)‬
y‬
‭For the current scan line‬‭ ‭, check each edge:‬

y1‬‭and‬‭
‭○‬ ‭If y lies between‬‭ y2‬
‭, the edge is‬‭active‬

‭y > y2 and y ≤ y1‬

‭○‬ ‭Else, ignore it for now‬

‭8.‬ ‭Calculate x-intersections with the scan line‬


‭For each‬‭active edge‬‭, find where it cuts the scan‬‭line (x-intersect):‬

x1‬
‭○‬ ‭First time: use‬‭
x = x + Ax‬
‭○‬ ‭Next times: use formula‬‭
Ax‬‭is calculated from the edge's slope.‬
‭ here‬‭
W
‭ ‬ ‭x-intersect = x-intersect + Ax (x‬‭i+1‬‭=x‬‭i‭+
○ ‬ Ax)‬

‭○‬ ‭where Ax = 1 / m, and m = (y2 - y1) / (x2 - x1)‬

‭9.‬ ‭Handle tricky cases at corners (vertex rule)‬


‭If the scan line goes through a vertex (corner), decide whether to count‬
t‭ hat point once or twice based on the neighboring edges. (Avoid‬
‭overfilling or underfilling)‬

‭10.‬ ‭Sort all x-intersection points‬


‭Arrange all the x-points where scan line cuts the polygon, in increasing‬
‭order (left to right).‬

‭11.‬ ‭Make pairs from the x-intersect points‬


‭Group x-points into pairs: (x1, x2), (x3, x4), etc. Each pair represents‬
‭one horizontal line to draw.‬

‭12.‬ ‭Draw horizontal lines between each pair‬


‭For each pair, draw a straight line from x1 to x2 on the current scan line‬
‭y.‬

‭13.‬ ‭Move scan line down‬


‭Reduce y by 1 to move to the next horizontal scan line(y=y-1).‬

‭14.‬ ‭Repeat until you reach the bottom‬


ymin‬‭(bottom‬‭of the‬
‭Keep doing steps 7 to 13 until you reach‬‭
‭polygon)(y>=ymin).‬

‭15.‬ ‭Done! Polygon is filled.‬

‭ et’s go step-by-step through the Scan Line Polygon Fill Algorithm‬


L
‭using a simple triangle as an example.‬

‭🟢 Example Polygon: Triangle‬

‭Let’s say the triangle has 3 corners:‬

‭●‬ ‭Point A: (2, 8)‬

‭●‬ ‭Point B: (6, 2)‬

‭●‬ ‭Point C: (10, 6)‬


‭So:‬

‭x[] = [2, 6, 10]‬

‭y[] = [8, 2, 6]‬

‭We’ll go step by step now using this triangle.‬

‭🔄 STEP-BY-STEP WITH EXAMPLE:‬

‭1. Input number of corners‬

‭●‬ ‭Number of corners = 3‬


‭✅‬‭
n = 3‬

‭2. Input coordinates of all corners‬

‭✅‬

‭A (2, 8)‬

‭B (6, 2)‬

‭C (10, 6)‬

‭3. Find ymin and ymax‬

‭●‬ ‭
ymin = 2‬‭(from point B)‬
‭●‬ ‭
ymax = 8‬‭(from point A)‬

y = 8‬‭down to‬‭
‭We’ll scan from‬‭ y = 2‬

‭4. Store edge information‬

‭We create 3 edges:‬

‭1.‬ ‭A → B‬

‭2.‬ ‭B → C‬

‭3.‬ ‭C → A‬

‭Let’s process each:‬

‭➤ Edge 1: A(2,8) to B(6,2)‬

‭●‬ ‭y1 = 8, y2 = 2 → no swap needed‬

‭●‬ ‭x1 = 2‬

‭●‬ ‭Slope = (y2 - y1) / (x2 - x1) = (2 - 8)/(6 - 2) = -6 / 4 = -1.5‬

‭●‬ ‭So Ax = 1 / slope = 1 / -1.5 = -0.666‬

‭➤ Edge 2: B(6,2) to C(10,6)‬

‭●‬ ‭y1 = 6, y2 = 2 → no swap needed‬

‭●‬ ‭x1 = 10‬

‭●‬ ‭Slope = (2 - 6)/(10 - 6) = -4 / 4 = -1‬


‭●‬ ‭Ax = 1 / slope = -1‬

‭➤ Edge 3: C(10,6) to A(2,8)‬

‭●‬ ‭y1 = 8, y2 = 6 → no swap needed‬

‭●‬ ‭x1 = 10‬

‭●‬ ‭Slope = (6 - 8)/(2 - 10) = -2 / -8 = 0.25‬

‭●‬ ‭Ax = 1 / 0.25 = 4‬

‭✅ So we store:‬

‭Edge 1: x=2, y1=8, y2=2, Ax = -0.666‬

‭Edge 2: x=10, y1=6, y2=2, Ax = -1‬

‭Edge 3: x=10, y1=8, y2=6, Ax = 4‬

‭5. Sort the edges‬

‭Sort by:‬

‭●‬ ‭Highest y1 → then y2 → then lowest x‬

‭✅ Already sorted here as:‬

‭1.‬ ‭Edge A→B (y1=8)‬

‭2.‬ ‭Edge C→A (y1=8)‬

‭3.‬ ‭Edge B→C (y1=6)‬


‭6. Start from‬‭y = ymax = 8‬

‭7. Find active edges for y = 8‬

‭Check if 8 is between y1 and y2:‬


‭ 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‬

‭8. Calculate x-intersections‬

‭For y = 8:‬

‭●‬ ‭Edge A→B: x = 2 (initial x)‬

‭●‬ ‭Edge C→A: x = 10 (initial x)‬

‭✅ So x-intersect points = [2, 10]‬

‭9. Vertex rule‬

‭No issue for y = 8 since it's the topmost point. No duplicate counting.‬

‭10. Sort x-intersections‬

‭ orted:‬
S

‭ [2, 10]‬
‭11. Make pairs‬

‭✅ (2, 10)‬

‭12. Draw horizontal line‬

‭🖌️ Fill between x=2 and x=10 at y=8‬

‭13. Move scan line down‬

‭y = 7‬

‭🔁 Repeat from step 7 for y = 7 to y = 2‬

‭Let’s just summarize a few more iterations:‬

‭For y = 7:‬

‭●‬ ‭Active edges: A→B, C→A‬

‭●‬ ‭Update x values:‬

‭○‬ ‭A→B: x = 2 - 0.666 = 1.334‬

‭○‬ ‭C→A: x = 10 + 4 = 14‬

‭●‬ ‭x-intersects: [1.334, 14]‬

‭●‬ ‭Draw between 1.334 and 14 at y=7‬

‭For y = 6:‬
‭●‬ ‭Now edge B→C becomes active‬

‭●‬ ‭Active edges: A→B, C→A, B→C‬

‭●‬ ‭Update x values:‬

‭○‬ ‭A→B: 1.334 - 0.666 = 0.668‬

‭○‬ ‭C→A: 14 + 4 = 18‬

‭○‬ ‭B→C: starting x = 10‬

‭●‬ ‭x-intersects: [0.668, 10, 18]‬

‭●‬ ‭Sort: [0.668, 10, 18]‬

‭●‬ ‭Pairs: (0.668, 10) and (no pair for 18, ignore or check further)‬

‭... and continue until y = 2.‬

‭✅ 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.‬

‭🔄 Step-by-step in Simple Language:‬

‭1. Ask how many corners the polygon has‬


‭ e ask the user:‬
W
🗣️
‭ “How many corners does your shape have?”‬
📥 n‭.‬‬
‭ Save the number as‬‭

‭2. Get the coordinates of each corner‬

‭ e collect the position of each corner (x and y values).‬


W
📌 x[]‬
‭ Store all x-values in one list:‬‭
‭📌 Store all y-values in another list:‬‭
y[]‬

‭3. Find top and bottom of the polygon‬

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.‬

‭4. Store information about each edge‬

‭Every pair of connected corners makes an edge. For each edge:‬

‭●‬ ‭Pick the top point (bigger y) and bottom point (smaller y)‬

‭●‬ ‭Save:‬

‭○‬ ‭x at the top (x1)‬

‭○‬ ‭y1 (top y), y2 (bottom y)‬

‭○‬ ‭Calculate how much x changes as we go down (Ax = 1/slope)‬

‭📌 Swap points if needed to always go from top to bottom.‬


‭5. Sort the edges smartly‬

‭To process edges properly, we:‬

y1‬‭first‬
‭1.‬‭Sort by higher‬‭

y2‬
‭2.‬‭Then by higher‬‭

‭3.‬ ‭Then by smaller x-values‬


‭This helps in faster scan-line filling.‬

‭6. Start scanning from the top (y = ymax)‬

‭Begin at the highest point of the polygon, and move down one line at a time.‬

‭7. Find active edges for the current scan line‬

y‭:‬‬
‭Check which edges are active (used) for current scan line‬‭
✅ y‬‭is between y1 and y2, include the edge‬
‭ If‬‭

‭ If not, ignore it (it's not touching this line)‬

‭ . Calculate where the scan line cuts the edges‬


8
‭(x-intersections)‬

‭For each active edge:‬

‭●‬ ‭First time: use starting x (x1)‬

‭●‬ ‭Next times: use‬


x = x + Ax‬

‭where Ax = horizontal change based on edge slope‬

‭9. Handle tricky corner cases (vertex rule)‬


I‭ f the scan line exactly passes through a corner (vertex), decide if it should‬
‭be counted once or skipped, based on neighboring edges. This avoids‬
‭double-filling.‬

‭10. Sort the x-intersection points‬

‭ ll the x-values where the scan line hits the polygon are sorted from left to‬
A
‭right.‬

‭11. Make x-intersect pairs‬

‭ roup them like this:‬


G
👉
‭ (x1, x2), (x3, x4), ...‬
‭Each pair is one line to draw.‬

‭12. Draw lines between each pair‬

‭ or each pair:‬
F
🖌️ y‬
‭ Draw a horizontal line from the first x to the second x at the current‬‭ ‭.‬

‭13. Move scan line down‬

y‬‭by 1:‬
‭ ecrease‬‭
D
🔽
‭ Go to the next lower scan line.‬

‭14. Repeat until bottom‬

‭Repeat the steps until you reach the bottom of the polygon (‬‭
ymin‬
‭).‬

‭✅ Done! The shape is filled.‬


‭Attributes Of Output Primitive‬

‭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.‬

‭Key Attributes of a Point:-‬

‭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‬

‭A. Line type:-‬

‭The Line-Type Determines the Pattern Of the Line, Weather it is Solid, Dashed, Dotted.‬

‭Line Type‬ ‭Example‬ ‭Use Case‬

‭Solid Line‬ ‭__________‬ ‭Default, General use.‬

‭Dashed Line‬ ‭__ __ __ __‬ ‭Used in construction Drawing, Invisible Boundaries.‬

‭Dotted Line‬ ‭. . . . . . . . . .‬ ‭Used in Hidden Object Representation.‬

‭Dash-Dot Line‬ ‭_ . _ . _ . _ . _‬ ‭Used in Engineering Diagrams.‬

‭Double Line‬ ‭II II‬ ‭Used For Bold borders.‬


‭B. Line Width:-‬

‭‬
● ‭ 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.‬

‭C. Line Color:-‬

‭‬
● ‭ 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 )‬

‭The Appearance of Curves is controlled by various curve attributes, Such as‬


‭1.‬ ‭Curve type ( Representation )‬
‭2.‬ ‭Curve thickness ( width )‬
‭3.‬ ‭Curve color‬

‭1.‬ ‭Curve Types:-‬

‭Curve Type‬ ‭Definition‬ ‭Example‬

‭Explicit Curves‬ D
‭ efined by a Function of x ! y =‬ ‭ imple Graphs like‬
S
‭F(x)‬ ‭parabolas‬

‭Implicit Curves‬ ‭Defined by an Equation F(x,y)=0‬ ‭Circles ( x‬‭2‭+



y‬‭2‭-‬ r‬‭2‭=

0)‬

‭ 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.‬

‭2.‬ ‭Curve Thickness:-‬

‭‬ 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.‬

‭3.‬ ‭Curve Color:-‬

‭‬ 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.‬

‭4.‬ ‭Special Curves in Graphics:-‬

‭Curve Type‬ ‭Definition‬ ‭Application‬

‭Bezier-Curve‬ ‭Defined Using Control Points‬ ‭ sed in Typography,‬


U
‭Vector Graphics.‬

‭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.‬

‭Cardinal Spline‬ ‭A Smooth Interpolation Curve‬ ‭ sed in Graphs Drawing,‬


U
‭Physics & Gaming.‬

‭Fill Area Attributes‬

I‭n 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.‬

‭Fill Areas Are Widely Used In:‬

‭ .‬
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:‬

‭Fill Areas Can be Applied to Different Geometric Shape, Such as:‬

‭ .‬ 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.‬

‭1.‬ ‭Fill Colors:-‬

‭‬ 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.‬

‭2.‬ ‭Pattern Fill:-‬

‭ 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:‬

‭1.‬ ‭Pattern Table:-‬


‭●‬ ‭A Table Where Different Fill Patterns Are Stored.‬
‭●‬ ‭Each Pattern is Assigned a Number.‬

‭2.‬ ‭Applying a Pattern:-‬


‭●‬ ‭To Use a Pattern, We Select it By Its Index & Apply It to An Area.‬
‭●‬ ‭This Will Fill The Selected Shape With The Second Pattern From‬
‭The Table.‬

‭3.‬ ‭Hatchfill vs Patternfill:-‬


‭●‬ ‭Hatch fill‬‭uses parallel or crisscrossed lines to‬‭fill an area.‬
‭●‬ P
‭ attern fill‬‭uses a repeated Design or Texture ( like checkers or‬
‭stripes )‬

‭4.‬ C
‭ ustom Patterns:-‬
‭We can create our own patterns using a colors array ( a small grid with‬
‭different colors ).‬

‭5.‬ ‭Pattern Size & Positioning :-‬


‭●‬ ‭The Pattern’s Size can be adjusted using ; C=‬
‭Setpatternsize(dx,dy);‬
‭●‬ ‭The Starting Position for filling can be set with : C=‬
‭Setpatternreferencepoint(position);‬
‭●‬ ‭This makes sure the pattern starts filling from a specific point & is‬
‭repeated correctly.‬

‭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.‬

‭7.‬ ‭Background & Transparency:-‬


‭●‬ ‭Pattern can be Combined With Background colors using Boolean‬
‭Operation ( And,Or,Not).‬
‭●‬ ‭Transparent areas in patterns allow the background to show‬
‭through.‬

‭ 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:‬

P(x, y)‬‭to test.‬


‭○‬ ‭A point‬‭

‭○‬ ‭A polygon defined by its vertices in order.‬

‭2.‬ ‭Process:‬

P(x, y)‬‭to the right.‬


‭○‬ ‭Cast a horizontal ray from‬‭

‭○‬ ‭For each edge of the polygon, check if the ray intersects it.‬

‭○‬ ‭Count how many edges the ray crosses.‬

‭3.‬ ‭Decision:‬

‭○‬ ‭If the count is odd, return "Inside".‬

‭○‬ ‭If the count is even, return "Outside".‬

‭🧠 Important Considerations:‬

‭●‬ E
‭ dge on the ray:‬‭If the ray passes exactly through‬‭a 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 carefully‬‭to avoid false‬
‭intersections.‬

You might also like