Write a C program to fill a polygon using Scan Line fill algorithm.

Below is a C program that implements the Scan Line Fill Algorithm to fill a polygon.
The Scan Line Fill Algorithm is used to fill the interior of a polygon. The idea is to scan the polygon horizontally from top to bottom and determine which parts of the polygon need to be filled for each scan line.

	#include <stdio.h>
	#include <stdlib.h>
    #include <graphics.h>

    #define MAX_VERTICES 20
    
    // Structure to store the edges
    struct Edge {
        int x;        // X coordinate of the intersection
        float slope;  // Slope of the edge
    };
    
    // Function to sort the edges by their X-coordinate
    void sortEdges(struct Edge edges[], int n) {
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (edges[j].x > edges[j + 1].x) {
                    struct Edge temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }
    
    // Function to fill a polygon using the Scan Line Fill algorithm
    void scanLineFill(int polygon[][2], int n) {
        int y_min = polygon[0][1], y_max = polygon[0][1];
    
        // Find the min and max y-coordinates of the polygon
        for (int i = 1; i < n; i++) {
            if (polygon[i][1] < y_min)
                y_min = polygon[i][1];
            if (polygon[i][1] > y_max)
                y_max = polygon[i][1];
        }
    
        // Loop through every scan line from y_min to y_max
        for (int y = y_min; y <= y_max; y++) {
            struct Edge edges[MAX_VERTICES];
            int edge_count = 0;
    
            // Find the intersections of the scan line with the edges of the polygon
            for (int i = 0; i < n; i++) {
                int x1 = polygon[i][0], y1 = polygon[i][1];
                int x2 = polygon[(i + 1) % n][0], y2 = polygon[(i + 1) % n][1];
    
                // Check if the scan line intersects this edge
                if ((y1 <= y && y2 > y) || (y2 <= y && y1 > y)) {
                    // Calculate the x-coordinate of the intersection
                    int x_intersection = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
                    edges[edge_count].x = x_intersection;
                    edges[edge_count].slope = (float)(x2 - x1) / (y2 - y1);
                    edge_count++;
                }
            }
    
            // Sort the edges by their x-coordinate
            sortEdges(edges, edge_count);
    
            // Fill the pixels between pairs of intersections
            for (int i = 0; i < edge_count; i += 2) {
                for (int x = edges[i].x; x <= edges[i + 1].x; x++) {
                    putpixel(x, y, WHITE);
                }
            }
        }
    }
    
    int main() {
        int gd = DETECT, gm;
        initgraph(&gd, &gm, "");  // Initialize graphics mode
    
        int n;
        printf("Enter the number of vertices of the polygon: ");
        scanf("%d", &n);
    
        int polygon[n][2];
        printf("Enter the coordinates of the vertices (x, y):\n");
        for (int i = 0; i < n; i++) {
            printf("Vertex %d: ", i + 1);
            scanf("%d %d", &polygon[i][0], &polygon[i][1]);
        }
    
        // Draw the polygon outline
        for (int i = 0; i < n; i++) {
            line(polygon[i][0], polygon[i][1], polygon[(i + 1) % n][0], polygon[(i + 1) % n][1]);
        }
    
        // Call the scan line fill algorithm
        scanLineFill(polygon, n);
    
        // Wait for the user to press a key before closing the graphics window
        getch();
        closegraph();
        return 0;
    }