Open In App

C program to perform reflection of the given 2D image using computer graphics

In this article, we will discuss how to perform reflection in C using computer graphics using logic rather than the direct matrix formula of translation followed by rotation and translation.

This technique has currently been applied for the object taken as a triangle, the code can be applied to any object, provided the (Xn, Yn) coordinates are adapted into it.



The base logic of this technique is that in any given reflection, the lateral inversion of the reflected object is at the same distance from any given side as the source object. i.e., if a source object is at a distance of 10 pixels from the left, then the reflected object will be at a distance of 10 pixels from the right and the same is applicable for top and bottom sides.

Example on a real-life basis:



Steps of the aforementioned technique:

Approach:

  1. Draw a line in graphics to act as a Y-axis, by passing along 4 values as parameters of the line() function as line(getmaxx()/2, 0, getmaxx()/2, getmaxy()).
  2. Draw a line in graphics to act as X-axis by passing along 4 values as parameters of the line() function as line(0, getmaxy()/2, getmaxx(), getmaxy()/2).
  3. Draw an object using the line() function with parameters from set variables.
  4. Perform reflection and draw the object along origin using Step 3 of the technique and color it red to distinguish it from other objects.
  5. Perform reflection and draw the object along the X-axis using Step 1 of the technique and color it cyan to distinguish it from other objects.
  6. Perform reflection and draw the object along the Y-axis using Step 2 of the technique and color it green to distinguish it from other objects.

Below is the implementation of the above approach:




// C program for the above approach
  
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
  
// Driver Code
void main()
{
    // Initialize the drivers
    int gm, gd = DETECT, ax, x1 = 100;
    int x2 = 100, x3 = 200, y1 = 100;
    int y2 = 200, y3 = 100;
  
    // Add in your BGI folder path
    // like below initgraph(&gd, &gm,
    // "C:\\TURBOC3\\BGI");
    initgraph(&gd, &gm, "");
    cleardevice();
  
    // Draw the graph
    line(getmaxx() / 2, 0, getmaxx() / 2,
         getmaxy());
    line(0, getmaxy() / 2, getmaxx(),
         getmaxy() / 2);
  
    // Object initially at 2nd quadrant
    printf("Before Reflection Object"
           " in 2nd Quadrant");
  
    // Set the color
    setcolor(14);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    getch();
  
    // After reflection
    printf("\nAfter Reflection");
  
    // Reflection along origin i.e.,
    // in 4th quadrant
    setcolor(4);
    line(getmaxx() - x1, getmaxy() - y1,
         getmaxx() - x2, getmaxy() - y2);
  
    line(getmaxx() - x2, getmaxy() - y2,
         getmaxx() - x3, getmaxy() - y3);
  
    line(getmaxx() - x3, getmaxy() - y3,
         getmaxx() - x1, getmaxy() - y1);
  
    // Reflection along x-axis i.e.,
    // in 1st quadrant
    setcolor(3);
    line(getmaxx() - x1, y1,
         getmaxx() - x2, y2);
    line(getmaxx() - x2, y2,
         getmaxx() - x3, y3);
    line(getmaxx() - x3, y3,
         getmaxx() - x1, y1);
  
    // Reflection along y-axis i.e.,
    // in 3rd quadrant
    setcolor(2);
    line(x1, getmaxy() - y1, x2,
         getmaxy() - y2);
    line(x2, getmaxy() - y2, x3,
         getmaxy() - y3);
    line(x3, getmaxy() - y3, x1,
         getmaxy() - y1);
    getch();
  
    // Close the graphics
    closegraph();
}

Output:


Article Tags :