Computer Graphics: Scan Line Polygon Fill Algorithm

Alberto Scicali
HackerNoon.com
7 min readNov 9, 2016

--

All hail tea pot

This post serves as my first introduction into the “blogosphere” as well, hopefully, the first of many write-ups about computing and technology in general. So with that out of the way lets get on with it.

Today I will be discussing the Scan Line Polygon Fill (SLPF) algorithm, and then showing my implementation of the algorithm in C++. The purpose of the SLPF algorithm is to fill (color) the interior pixels of a polygon given only the vertices of the figure.

Basic Idea:

The basic idea is to collect all of the edges (except horizontal edges) that compose the polygon, fill in the figure scan line by scan line using the edges as starting and stopping points.

Main Components:

To successfully fill in a polygon three main components will be used: Edge Buckets, an Edge Table and an Active List. These components will contain an edge’s information, hold all of the edges that compose the figure and maintain the current edges being used to fill in the polygon, respectively.

Edge Buckets

The edge bucket is a structure that holds information about the polygon’s edges. The edge bucket looks different pending on the implementation of the algorithm, in my case it looks like this:

Edge Bucket representation

Here is a breakdown of what each member represent in an edge bucket:

  • yMax: Maximum Y position of the edge
  • yMin: Minimum Y position of the edge
  • x: The current x position along the scan line, initially starting at the same point as the yMin of the edge
  • sign: The sign of the edge’s slope ( either -1 or 1)
  • dX: The absolute delta x (difference) between the edge’s vertex points
  • dY: The absolute delta y (difference) between the edge’s vertex points
  • sum: Initiated to zero. Used as the scan lines are being filled to x to the next position

Edge Table (ET)

The ET is a list that contains all of the edges that make up the figure.

Important ET notes:

  • When creating edges, the vertices of the edge need to be ordered from left to right
  • The edges are maintained in increasing yMin order
  • The edges are removed from the ET once the Active List is done processing them
  • The algorithm is done filling the polygon once all of the edges are removed from the ET

Active List (AL)

The AL contains the edges that are being processed/used to fill the polygon. Every edge in the AL has a pairing buddy edge, because when filling a scan line, pixels are filled starting from one edge until the buddy edge is encountered.

Important AL notes:

  • Edges are pushed into the AL from the Edge Table once an edge’s yMin is equal to the current scan line being processed
  • Edges will always be in the AL in pairs
  • Edges in the AL are maintained in increasing x order.
  • The AL will be re-sorted after every pass

Steps:

Now that we have the main components covered, lets go over the SLPF algorithm in more detailed steps.

~General assumptions~

  • The user has access to a method that can set the color of individual pixels. You will see that I simply call the setPixel() method whenever I fill in a pixel.
  • The vertices of the given shape are listed in order around the circumference of the polygon
  • This one is important, otherwise the polygons will not be drawn properly

Here are the steps:

(Why doesn’t Medium support nested lists??)

Final Important Notes:

  • When creating the edge buckets if the vertices of the edge are not in left to right order you might get the incorrect sign for the bucket. If the edge buckets have the wrong sign this will happen when you try to fill them:
Something is not right here…

To maintain only integers in the Edge Bucket, the slope is not actually calculated but is instead maintained as three distinct members: sign, dX, dY.

  • This allows the Edge Buckets’ x member to be incremented discreetly without worrying about what happens when encountering a real number in a point
  • If the slope is not interpreted correctly, this may happen:
No x increment == pixelated image?

Implementation:

Now that the details are covered, here is the implementation of the SLPF in C++.

Due to a request from my professor the C++ implementation of the code has been taken down and replaced with pseudo code (There is a strong concern that students will copy the code for projects). If you are interested in my C++ implementation feel free to message me for it.

Main Program (Pseudo code)

Header file with structs

That all it takes to implement the SLPF algorithm! I hope this post is constructive and that you enjoyed reading through it. If there are any mistakes and you want to leave comment, feel free to do so or send me a message.

Cheers!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Alberto Scicali
HackerNoon.com

Co-Founder of Hypnos — Sleep Journal (hypnos.ai) Software/Research Engineer. Commonly referred to as a Bear.