Scan Line Polygon Filling in C
An interior polygon can be filled with a specific colour using a computer graphics technique called scanline polygon filling. Finding the pixels that fall inside each scanline that make up the polygon is the fundamental concept behind scanline polygon filling. The polygon is first divided into horizontal bands, or scanlines. This method is frequently applied in computer graphics to produce aesthetically pleasing and realistic images.
Implementing scanline polygon filling in C programming entails first defining the polygon's vertices and then using algorithms to identify the pixels that fall inside the polygon for each scanline. An algorithm that is frequently used for filling scanline polygons is called the "Scanline Fill Algorithm".
Methods:
- initgraph(): Use this function to initialize the graphics system. Before drawing shapes or images, it sets up the graphics environment. It is a component of the C graphics.h library.
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
- drawpoly(): A polygon can be drawn using this graphics.h library function with the help of the provided vertices.
drawpoly(edges, x);
- scanline fill: This is a custom function that applies the scanline filling algorithm to polygons. The number of edges, the x and y coordinates, and other parameters are required.
void scanlineFill(int x[], int y[], int edges) {
// Implementation of scanline polygon filling algorithm
// ...
}
- Putpixel: Set the colour of a pixel at a specified location using this function, which is a part of the graphics.h library.
putpixel(temp, i, WHITE);
Example:
Let us take an example to illustrate the use of Scan Line Polygon Filling in C.
#include <GL/glut.h>
#include <vector>
struct Point {
int x, y;
Point(int _x, int _y) : x(_x), y(_y) {}
};
std::vector<Point> polygonVertices;
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
gluOrtho2D(0, 500, 0, 500);
}
void drawPolygon() {
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
for (const auto& vertex : polygonVertices) {
glVertex2i(vertex.x, vertex.y);
}
glEnd();
}
void scanlineFill() {
glColor3f(0.0, 0.7, 0.0);
int yMin = 500, yMax = 0;
for (const auto& vertex : polygonVertices) {
if (vertex.y < yMin) yMin = vertex.y;
if (vertex.y > yMax) yMax = vertex.y;
}
for (int y = yMin + 1; y < yMax; ++y) {
std::vector<int> intersections;
for (std::size_t i = 0; i < polygonVertices.size(); ++i) {
const Point& p1 = polygonVertices[i];
const Point& p2 = polygonVertices[(i + 1) % polygonVertices.size()];
if ((p1.y <= y && p2.y > y) || (p1.y > y && p2.y <= y)) {
int x = p1.x + (y - p1.y) / (p2.y - p1.y) * (p2.x - p1.x);
intersections.push_back(x);
}
}
for (std::size_t i = 0; i < intersections.size(); i += 2) {
int x1 = intersections[i];
int x2 = intersections[i + 1];
glBegin(GL_LINES);
glVertex2i(x1, y);
glVertex2i(x2, y);
glEnd();
}
}
glFlush();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawPolygon();
scanlineFill();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Scanline Polygon Filling");
polygonVertices.emplace_back(100, 200);
polygonVertices.emplace_back(200, 400);
polygonVertices.emplace_back(300, 200);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
Output:
g++ filename.cpp -o outputName -lGL -lGLU –lglut
./outputName
Steps to be Followed:
- Make sure your system has both GLUT and OpenGL installed.
- Use a C++ compiler (such as g++, Visual C++, or clang++) to compile the code. For instance, you can type the following command into the terminal to use g++:
- Change filename.cpp to the name of your source code file and outputName to the name of the executable you want the output to be.
- Launch the executable that has been compiled.
Conclusion:
A basic technique in computer graphics for effectively colouring or shading a polygon's interior is the scanline polygon filling algorithm in C. Finding the intersections between scanlines and polygon edges is the basis of this method, which involves scanning the image pixel by pixel along each horizontal line. The algorithm essentially applies a predetermined colour to the interior of the polygon by determining the x-coordinates of these intersections. The algorithm's implementation using graphics libraries like GLUT, OpenGL, and Turbo C is demonstrated in the provided C examples. For graphics programmers to create realistic and visually appealing computer-generated images, they must grasp and become proficient in scanline polygon filling, which serves as the foundation for more advanced rendering techniques. This algorithm provides the ability to render complex shapes by carefully adjusting vertices and pixels, and it is a fundamental idea in the larger field of computer graphics.