Program-4: Write A Program To Implement 2D Reflection of An Object

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Program-4

Aim: Write a program to implement 2D Reflection of an object.


Description:
It is a transformation which produces a mirror image of an object. The mirror image can be
either about x-axis or y-axis. The object is rotated by180°.

Types of Reflection:
1.Reflection about the x-axis
2.Reflection about the y-axis
3.Reflection about an axis perpendicular to xy plane and passing through the origin
4.Reflection about line y=x

Code:

#include "graphics.h"
#include<iostream>
#define PI 3.14159265
using namespace std;

void getxy(double m, double c, int x, int y, double& xr, double& yr) {


double angle = atan(m);
double sina = sin(angle);
double cosa = cos(angle);
cout << cosa;
double c2 = cosa * cosa;
double tsc = 2 * sina * cosa;
double ctst = cosa * cosa - sina * sina;
xr = x * ctst + y * tsc - tsc * c;
yr = x * tsc - y * ctst + 2 * c2 * c;
}

int main()
{
double m, c;
int delta = 250;
cout << " Reflection of a 2D object [CIRCLE]" << endl;
cout << endl;
initwindow(500, 500, "Reflection of a 2D object");
cout << "Enter radius and centre (x,y) [space separated] ";
int r, x, y;
cin >> r >> x >> y;
circle(delta + x, delta - y, r);
cout << "Reflection about y=mx+c => Enter m & c [space-separated] ";cin >> m >>
c;
double rx, ry;
getxy(m, c, x, y, rx, ry);
line(delta - 150, delta - (c - 150 * m), delta + 150, delta - (c + 150 * m));
circle(int(rx + 0.5) + delta, delta - int(ry + 0.5), r);

while (!kbhit())
{
delay(200);
}
return 0;
}

Result:

Learning Outcome:
Through this code we learnt how to reflect points along a line by calcultaing the
corresponding points according to the line.

You might also like