0% found this document useful (0 votes)
19 views5 pages

2D Polygon Rotation and Reflection Code

The document describes a C++ code to perform 2D rotation transformations on polygons. The code takes vertex coordinates of a polygon as input, draws the original polygon, and allows the user to select a reflection transformation - about the x-axis, y-axis, origin, or line y=x. It then applies the selected transformation and redraws the reflected polygon.

Uploaded by

dannyboy
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)
19 views5 pages

2D Polygon Rotation and Reflection Code

The document describes a C++ code to perform 2D rotation transformations on polygons. The code takes vertex coordinates of a polygon as input, draws the original polygon, and allows the user to select a reflection transformation - about the x-axis, y-axis, origin, or line y=x. It then applies the selected transformation and redraws the reflected polygon.

Uploaded by

dannyboy
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

Practical 15

Aim: To perform 2D Rotation Transformation on any Polygon

Code:

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm,n,x[10],y[10],ch;

initgraph(&gd,&gm,"");

int xmax=getmaxx();

int ymax=getmaxy();

int xc=(xmax)/2;

int yc=(ymax)/2;

line(xc,0,xc,ymax);

line(0,yc,xmax,yc);

cout<<"Enter the no of vertices:";

cin>>n;

for(int i=0;i<n;i++)

cout<<"Enter the x co-ordinate:";

cin>>x[i];

cout<<"Enter the y co-ordinate:";

cin>>y[i];

for(i=0;i<n-1;i++)
{

line(xc+x[i],yc-y[i],xc+x[i+1],yc-y[i+1]);

line(xc+x[n-1],yc-y[n-1],xc+x[0],yc-y[0]);

cout<<"\nPress 1 for Reflection about x-axis"<<endl;

cout<<"\nPress 2 for Reflection about y-axis"<<endl;

cout<<"\nPress 3 for Reflection about Origin"<<endl;

cout<<"\nPress 4 for Reflection about y=x"<<endl;

cout<<"Press 5 to exit"<<endl;

cout<<"\nEnter the value:";

cin>>ch;

switch(ch)

case 1:

for(i=0;i<n-1;i++)

line(x[i]+xc,y[i]+yc,x[i+1]+xc,y[i+1]+yc);

line(x[n-1]+xc,y[n-1]+yc,x[0]+xc,y[0]+yc);

getch();

break;

case 2:

for(i=0;i<n-1;i++)

line(-x[i]+xc,-y[i]+yc,-x[i+1]+xc,-y[i+1]+yc);

}
line(-x[n-1]+xc,-y[n-1]+yc,-x[0]+xc,-y[0]+yc);

getch();

break;

case 3:

for(i=0;i<n-1;i++)

line(xc-x[i],yc+y[i],xc-x[i+1],yc+y[i+1]);

line(xc-x[n-1],yc+y[n-1],xc-x[0],yc+y[0]);

getch();

break;

case 4:

line(xc,yc,xmax,0);

for(i=0;i<n-1;i++)

line(xc+y[i],yc-x[i],xc+y[i+1],-x[i+1]+yc);

line(xc+y[n-1],-x[n-1]+yc,xc+y[0],-x[0]+yc);

getch();

break;

case 5:

getch();

getch();

Output:
Reflection about X-Axis

Reflection aboutOrigin
Reflection about Y-Axis

Reflection about Y=X

You might also like