Implement the algorithm to Draw the polygon using filling technique in C Language.

 Here's a C++ program to draw and fill a polygon using the scan-line filling technique. This program uses the graphics.h library, which is typically available in Turbo C++ or similar environments. If you're using modern compilers like GCC, you'll need to use other graphics libraries like OpenGL or SDL.

Steps of the Algorithm

  1. Accept the vertices of the polygon.

  2. Find intersections of the scan line with polygon edges.

  3. Sort intersections by increasing x-coordinate.

  4. Fill pixels between pairs of intersections.


The Scan-Line Filling algorithm is a popular technique used in computer graphics to fill polygons. It works by drawing horizontal lines (scan lines) through the entire area of the polygon and determining which parts of these scan lines are inside the polygon and should be filled. Here's a detailed explanation and C program to draw and fill a polygon using the scan-line filling technique.

Steps for the Scan-Line Filling Algorithm:

  1. Input Polygon Coordinates: The user provides the polygon's vertices.

  2. Sort Edges by Y-coordinate: The edges of the polygon are sorted by their y-coordinate. This helps in determining which edges intersect the scan line.

  3. Intersect with Scan Lines: For each scan line (horizontal line), the algorithm calculates which edges of the polygon it intersects.

  4. Sort Intersections: Once the scan line intersects the polygon's edges, the intersections are sorted by their x-coordinate.

  5. Fill Between Intersections: The polygon is filled between each pair of intersection points.


C++ Code for Scan-Line Polygon Filling


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

#define MAX_VERTICES 100

void drawPolygon(int polyX[], int polyY[], int n) {
    for (int i = 0; i < n - 1; i++) {
        line(polyX[i], polyY[i], polyX[i + 1], polyY[i + 1]);
    }
    line(polyX[n - 1], polyY[n - 1], polyX[0], polyY[0]);  // Closing the polygon
}

void fillPolygon(int polyX[], int polyY[], int n) {
    int i, j, k;
    int y, temp;
    int x_intersections[MAX_VERTICES], intersections;

    int minY = polyY[0], maxY = polyY[0];
    
    // Find min and max Y values of the polygon
    for (i = 1; i < n; i++) {
        if (polyY[i] < minY) minY = polyY[i];
        if (polyY[i] > maxY) maxY = polyY[i];
    }

    // Scan-line filling
    for (y = minY; y <= maxY; y++) {
        intersections = 0;

        // Find intersections with polygon edges
        for (i = 0; i < n; i++) {
            j = (i + 1) % n;  // Next vertex
            
            if ((polyY[i] <= y && polyY[j] > y) || (polyY[j] <= y && polyY[i] > y)) {
                x_intersections[intersections++] =
                    polyX[i] + (y - polyY[i]) * (polyX[j] - polyX[i]) / (polyY[j] - polyY[i]);
            }
        }

        // Sort intersections using Bubble Sort
        for (i = 0; i < intersections - 1; i++) {
            for (j = 0; j < intersections - 1 - i; j++) {
                if (x_intersections[j] > x_intersections[j + 1]) {
                    temp = x_intersections[j];
                    x_intersections[j] = x_intersections[j + 1];
                    x_intersections[j + 1] = temp;
                }
            }
        }

        // Fill pixels between pairs of intersections
        for (i = 0; i < intersections; i += 2) {
            for (j = x_intersections[i]; j <= x_intersections[i + 1]; j++) {
                putpixel(j, y, WHITE);
            }
        }
    }
}

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

    int polyX[] = {100, 200, 250, 150};
    int polyY[] = {100, 50, 150, 200};
    int n = 4;  // Number of vertices

    drawPolygon(polyX, polyY, n);
    fillPolygon(polyX, polyY, n);

    getch();
    closegraph();
    return 0;
}



Explanation

  1. drawPolygon(): Draws the outline of the polygon.

  2. fillPolygon():

    • Determines the min and max Y values.

    • Finds intersections for each scan-line.

    • Sorts the intersection points.

    • Fills pixels between intersection pairs.


Comments

Popular posts from this blog

Implement the DDA algorithm to draw the line. Generalize it for co-ordinates in C language.

Perform the Line Clipping Algorithm in C Language.

Implement the Bresenham’s algorithm to draw the line. Generalize it for co-ordinates in C language