0% found this document useful (0 votes)
41 views3 pages

C++ 2D Image Reflection Program

Computer graphics practical

Uploaded by

tejasrahane27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

C++ 2D Image Reflection Program

Computer graphics practical

Uploaded by

tejasrahane27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Pr. 4B.

] C++ program to perform reflection of the given 2D image


using computer graphics

#include <iostreams.h>
#include <conio.h>
#include <graphics.h>
#include <math.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


initgraph(&gd, &gm, "C:\\TURBOC3\\BGI ");
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("\n After Reflection");

// Reflection along origin [Link] 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);
Pr. 4B.] C++ program to perform reflection of the given 2D image
using computer graphics

// Reflection along x-axis [Link] 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 [Link] 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();

closegraph();
}

Output :
Pr. 4B.] C++ program to perform reflection of the given 2D image
using computer graphics

Common questions

Powered by AI

The cleardevice() function in the program is used to clear the graphical screen, thereby ensuring a clean state for new drawings and preventing image overlap from previous executions. This helps in resetting the drawing area so that each reflection or graphical operation starts afresh. The closegraph() function is employed at the end of the graphics operations to properly shut down the graphics system, releasing resources, and ensuring that the program exits the graphics mode cleanly. These functions collectively contribute to stable program execution without graphical glitches .

The program operates within a Cartesian coordinate system, partitioning the space into four quadrants to manage image placement and reflection. The second quadrant is initially where the image is placed using original coordinates. Reflection transformations are coded using conditional logic and coordinate arithmetic; for instance, the x-coordinates are inverted for horizontal reflection using `getmaxx() - x`, while the y-coordinates for vertical reflection are managed with `getmaxy() - y`. The program uses integer arithmetic based on maximum dimensions returned by getmaxx() and getmaxy() functions to compute these transformations efficiently .

The getmaxx() and getmaxy() functions are critical in managing graphical transformations dynamically by providing the maximum x and y dimensions of the current graphics mode. These values are used to calculate transformations based on the screen's extremities for reflecting images into different quadrants. For example, to reflect coordinates into another quadrant, these functions allow the program to determine new x or y values by subtracting the original coordinates from the maximum values. This dynamic approach allows transformations to adjust automatically to different screen sizes or resolutions .

The C++ program implements reflection by drawing the object in different quadrants using specific functions and properties from computer graphics libraries. Initially, the object is displayed in the second quadrant using its original coordinates (x1, y1), (x2, y2), and (x3, y3). Reflection along the origin moves the image to the fourth quadrant, using the formula (getmaxx() - x, getmaxy() - y) for each point. For reflection along the x-axis, the image appears in the first quadrant with coordinates (getmaxx() - x, y), and along the y-axis, it is shown in the third quadrant with (x, getmaxy() - y).

In the program, the line() function is used to draw lines between points, thereby forming the edges of polygons or outlines of shapes in different quadrants after reflection transformations. The setcolor() function determines the color of the lines that are drawn, allowing the program to visually distinguish different reflections by assigning unique colors to lines in each quadrant. For example, the before-reflection object is drawn with color code 14, and reflections are assigned colors 4, 3, and 2 .

Reflecting the 2D image along the x-axis in the program effectively flips it vertically, moving the object to the first quadrant from its original position in the second quadrant by using coordinates (getmaxx() - x, y). Reflection along the y-axis, on the other hand, flips the image horizontally, shifting it to the third quadrant by adjusting coordinates to (x, getmaxy() - y). Reflection along the origin inverts both x and y coordinates, moving the image to the fourth quadrant with transformed coordinates (getmaxx() - x, getmaxy() - y), thus flipping the image both horizontally and vertically .

Color coding in the program facilitates visual distinction between various transformations and reflections of the object. By assigning different colors to the original and reflected images, such as yellow (14) for the original and red (4), blue (3), and green (2) for various reflections, users can easily perceive changes and symmetry in graphical output. This approach enhances the comprehension of geometric transformations by providing a clear visual reference that differentiates each stage, aiding both educational demonstrations and debugging processes .

Using hardcoded paths in the initgraph() function, such as "C:\TURBOC3\BGI", can lead to challenges related to portability and compatibility across different systems, as this requires the specific directory structure to be present. A system where such paths do not exist can result in errors during execution. To address these issues, dynamic path determination or environment variables can be used to set paths at runtime. Additionally, relative paths or user inputs can be introduced for more flexible and portable code execution .

The program uses the initgraph() function to initialize the graphics mode and drivers, using variables gm (for graphics mode) and gd set to DETECT to auto-detect the best driver. Additionally, a specific directory path for the graphics library is provided in the initgraph() function, namely "C:\TURBOC3\BGI", to ensure that the required graphics drivers are correctly accessed and initialized .

The program demonstrates symmetry through transformations that create mirror images across specified axes. By reflecting the image along the x-axis, y-axis, and the origin, it creates symmetrical patterns in each quadrant. This is executed by using inverse calculations based on maximum screen dimensions for x and y coordinates, effectively mirroring the shapes relative to their starting points. The program distinguishes each reflection through color codes, visually emphasizing symmetric transformation across the axes and origin .

You might also like