2d Reflection In Computer Graphics C Program With Output

4 min read

2D Reflection in Computer Graphics: C Program Implementation and Output

Introduction to 2D Reflection

2D reflection in computer graphics is a fundamental transformation operation that creates a mirror image of an object across a specified axis or line. In practice, unlike rotation or scaling, reflection produces a symmetrical counterpart that maintains the same shape and size as the original object but reverses its orientation. This transformation is widely used in computer graphics applications, game development, CAD systems, and animation software to create realistic visual effects and symmetrical designs Small thing, real impact..

Quick note before moving on.

In computer graphics, 2D reflection can be performed across three primary axes: the X-axis, Y-axis, and the origin point. Each type of reflection follows specific mathematical rules that determine how the coordinates of each point in the object are transformed. Understanding these principles is essential for implementing reflection algorithms in programming languages like C.

Mathematical Foundation of 2D Reflection

The mathematical representation of 2D reflection uses transformation matrices to calculate new coordinates for each point. The basic reflection matrices are:

  • X-axis reflection: Points are reflected across the horizontal axis, changing the sign of the Y-coordinate while keeping the X-coordinate unchanged.
  • Y-axis reflection: Points are reflected across the vertical axis, changing the sign of the X-coordinate while keeping the Y-coordinate unchanged.
  • Origin reflection: Points are reflected through the origin point, changing the signs of both X and Y coordinates.

These transformations can be represented using homogeneous coordinates and 3x3 transformation matrices, which allow for efficient computation in computer programs Less friction, more output..

C Program Implementation

Here's a complete C program that demonstrates 2D reflection in computer graphics with practical implementation:

#include 
#include 
#include 
#include 

// Function to perform X-axis reflection
void reflectX(int x[], int y[], int n) {
    int i;
    for(i = 0; i < n; i++) {
        y[i] = -y[i];
    }
}

// Function to perform Y-axis reflection
void reflectY(int x[], int y[], int n) {
    int i;
    for(i = 0; i < n; i++) {
        x[i] = -x[i];
    }
}

// Function to perform origin reflection
void reflectOrigin(int x[], int y[], int n) {
    int i;
    for(i = 0; i < n; i++) {
        x[i] = -x[i];
        y[i] = -y[i];
    }
}

// Function to draw a polygon
void drawPolygon(int x[], int y[], int n, int color) {
    setcolor(color);
    for(int i = 0; i < n; i++) {
        int j = (i + 1) % n;
        line(x[i] + 320, 240 - y[i], x[j] + 320, 240 - y[j]);
    }
}

// Function to fill polygon with color
void fillPolygon(int x[], int y[], int n, int pattern, int color) {
    setcolor(color);
    setfillstyle(pattern, color);
    fillpoly(n, x);
}

int main() {
    int gd = DETECT, gm;
    int x[4], y[4];
    int choice;
    
    // Initialize graphics mode
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    
    // Define coordinates for a square
    x[0] = 0;   y[0] = 0;
    x[1] = 100; y[1] = 0;
    x[2] = 100; y[2] = 100;
    x[3] = 0;   y[3] = 100;
    
    // Draw original object
    drawPolygon(x, y, 4, WHITE);
    setcolor(YELLOW);
    outtextxy(250, 100, "Original Object");
    
    printf("2D Reflection in Computer Graphics\n");
    printf("==================================\n");
    printf("1. Reflection along Y-axis\n");
    printf("3. Reflection along X-axis\n");
    printf("2. Reflection along Origin\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    
    // Create copies for different reflections
    int x1[4], y1[4], x2[4], y2[4], x3[4], y3[4];
    
    // Copy original coordinates
    for(int i = 0; i < 4; i++) {
        x1[i] = x[i]; y1[i] = y[i];
        x2[i] = x[i]; y2[i] = y[i];
        x3[i] = x[i]; y3[i] = y[i];
    }
    
    switch(choice) {
        case 1:
            // X-axis reflection
            reflectX(y1, y1, 4);
            drawPolygon(x1, y1, 4, RED);
            setcolor(RED);
            outtextxy(250, 300, "Reflected (X-axis)");
            break;
            
        case 2:
            // Y-axis reflection
            reflectY(x2, y2, 4);
            drawPolygon(x2, y2, 4, GREEN);
            setcolor(GREEN);
            outtextxy(250, 300, "Reflected (Y-axis)");
            break;
            
        case 3:
            // Origin reflection
            reflectOrigin(x3, y3, 4);
            drawPolygon(x3, y3, 4, BLUE);
            setcolor(BLUE);
            outtextxy(250, 300, "Reflected (Origin)");
            break;
            
        default:
            printf("Invalid choice!

## Advanced Implementation with Matrix Operations

For more complex applications, here's an enhanced version using matrix multiplication:

```c
#include 
#include 
#include 

#define MAX_POINTS 20

// Structure to represent a 2D point
typedef struct {
    float x, y;
} Point;

// Function to apply reflection matrix
void applyReflectionMatrix(Point points[], int n, int type) {
    float temp;
    switch(type) {
        case 1: // X-axis reflection
            for(int i = 0; i < n; i++) {
                points[i].y = -points[i].y;
            }
            break;
        case 2: // Y-axis reflection
            for(int i = 0; i < n; i++) {
                points[i].x = -points[i].x;
            }
            break;
        case 3: // Origin reflection
            for(int i = 0; i < n; i++) {
                points[i].x = -points[i].x;
                points[i].y = -points[i].

// Function to draw coordinate axes
void drawAxes() {
    setcolor(WHITE);
    line(0, 240, 640, 240); // X-axis
    line(320, 0, 320, 480); // Y-axis
    outtextxy(320, 240, "O"); // Origin
}

int main() {
    int gd = DETECT, gm;
    Point triangle[3] = {{50, 50}, {150, 50}, {100, 150}};
New Additions

Fresh Reads

Others Explored

While You're Here

Thank you for reading about 2d Reflection In Computer Graphics C Program With Output. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home