0% found this document useful (0 votes)
63 views7 pages

Graphics Transformations Guide

The document describes code for performing 2D and 3D geometric transformations on graphical objects using C++ and graphics libraries. It includes functions for translation, scaling, rotation, reflection, shearing, and their combinations. The key steps are: 1. Define graphical objects like rectangles and cubes using coordinates. 2. Provide menus to select the type of transformation. 3. Define transformation matrices for translation, scaling, rotation, and reflection. 4. Apply the transformation matrices to the object coordinates to compute the transformed coordinates. 5. Draw the transformed objects using the new coordinates.

Uploaded by

Kaustubh Mejari
Copyright
© Attribution Non-Commercial (BY-NC)
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)
63 views7 pages

Graphics Transformations Guide

The document describes code for performing 2D and 3D geometric transformations on graphical objects using C++ and graphics libraries. It includes functions for translation, scaling, rotation, reflection, shearing, and their combinations. The key steps are: 1. Define graphical objects like rectangles and cubes using coordinates. 2. Provide menus to select the type of transformation. 3. Define transformation matrices for translation, scaling, rotation, and reflection. 4. Apply the transformation matrices to the object coordinates to compute the transformed coordinates. 5. Draw the transformed objects using the new coordinates.

Uploaded by

Kaustubh Mejari
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 7

2-d Transform.

#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include<math.h>
void main()
{
int gd, gm,choice,lx,ly,w,h;
clrscr() ;
detectgraph( &gd, &gm ) ;
initgraph( &gd, &gm, "..\\bgi");
cout<<"enter your choice... 1.translation 2.scaling 3.rotation 4. reflection";
cin>>choice;
switch(choice)
{
case 1:
int tx,ty;
cout<<"enter the top left coordinates of rect";
cin>>lx>>ly;
cout<<"enter the width and height of rect";
cin>>w>>h;
rectangle(lx,ly,lx+w,ly+h);
cout<<"\nTranslation";
cout<<"\nenter the translation factor..";
cin>>tx>>ty;
rectangle(lx+tx,ly+ty,lx+w+tx,ly+h+ty);
break;
case 2:
int sx,sy;
cout<<"enter the top left coordinates of rect";
cin>>lx>>ly;
cout<<"enter the width and height of rect";
cin>>w>>h;
rectangle(lx,ly,lx+w,ly+h);
cout<<"\nScaling";
cout<<"\nenter the scaling factors..";
cin>>sx>>sy;
rectangle(lx*sx,ly*sy,(lx+w)*sx,(ly+h)*sy);
break;

case 3:
line(0,300,600,300);
line(300,50,300,600);
int x1=10,y1=30,x2=100,y2=30,d;
float r;
line(x1+300,300-y1,300+x2,300-y2);
cout<<"enter the angle of rotation";
cin>>d;
r=d*0.0174;
float x22=(x2-300)*cos(r)+(y2-300)*sin(r);
float y22=(x2-300)*sin(r)-(y2-300)*cos(r);
line(x1+300,300-y1,300+(int)x22,300-(int)y22);
break;
case 4:
line(0,300,600,300);
line(300,50,300,600);
cout<<"enter the top left coordinates of rect";
cin>>lx>>ly;
cout<<"enter the width and height of rect";
cin>>w>>h;
int temp_lx=lx;//mp_ly=ly;
rectangle(lx+300,300-ly,300+lx+w,300-ly+h);
lx=-(lx+w);
rectangle(lx+300,300-ly,lx+300+w,300-ly+h);
ly=-(ly-h);
rectangle(temp_lx+300,300-ly,temp_lx+300+w,300-ly+h);
rectangle(lx+300,300-ly,lx+300+w,300-ly+h);
break;
}
getch();
closegraph();
}

Shear Transform
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd, gm, i, j, k ,choice,sx,sy,yr,xr;

clrscr() ;
detectgraph( &gd, &gm ) ;
initgraph( &gd, &gm, "..\\bgi");
line(300,100,300,500);
line(100,300,500,300);
setcolor(RED);
rectangle(0+300,300-50,50+300,300-0);
cout<<"Enter your choice \n 1.x shear 2.y shear 3. xshear with y ref 4. y_shear with x ref";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter the shear factor";
cin>>sx;
setcolor(BLUE);
line(0+300,300-0,50+300,300-0);
line(0+300+sx*50,300-50,50+300+sx*50,300-50);
line(0+300,300-0,0+300+sx*50,300-50);
line(50+300,300-0,50+300+sx*50,300-50);
break;
case 2:
cout<<"enter the shear factor";
cin>>sy;
setcolor(GREEN);
line(0+300,300-0,0+300,300-50);
line(50+300,300-50-sy*50,50+300,300-0-sy*50);
line(0+300,300-0,50+300,300-0-sy*50);
line(0+300,300-50,50+300,300-50-sy*50);
break;
case 3:
cout<<"enter the shear factor and y reference";
cin>>sx>>yr;
setcolor(BLUE);
line(0+300,300-0,50+300,300-0);
line(0+300+sx*50-sx*yr,300-50,50+300+sx*50-sx*yr,300-50);
line(0+300,300-0,0+300+sx*50-sx*yr,300-50);
line(50+300,300-0,50+300+sx*50-sx*yr,300-50);
break;
case 4:
cout<<"enter the shear factor and x reference";
cin>>sy>>xr;

setcolor(GREEN);
line(0+300,300-0,0+300,300-50);
line(50+300,300-50-sy*50+sy*xr,50+300,300-0-sy*50+sy*xr);
line(0+300,300-0,50+300,300-0-sy*50+sy*xr);
line(0+300,300-50,50+300,300-50-sy*50+sy*xr);
break;
}
getch() ;
closegraph() ;
}

3-D transform
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd, gm, i, j, k ,tx,ty,tz,choice,sx,sy,sz;
clrscr() ;
detectgraph( &gd, &gm ) ;
initgraph( &gd, &gm, "..\\bgi" ) ;
int cube[8][4]={{0,0,0,1},{100,0,0,1},{100,100,0,1},{0,100,0,1},{0,0,100,1},{100,0,100,1},
{0,100,100,1},{100,100,100,1}};
cout<<"Enter the choice 1.Translate 2.Scale 3.Reflect";
cin>>choice;
switch(choice)
{
case 1:
setcolor(RED);
line(cube[0][0]+200,cube[0][1]+200,cube[1][0]+200,cube[1][1]+200);
line(cube[1][0]+200,cube[1][1]+200,cube[2][0]+200,cube[2][1]+200);
line(cube[2][0]+200,cube[2][1]+200,cube[3][0]+200,cube[3][1]+200);
line(cube[3][0]+200,cube[3][1]+200,cube[0][0]+200,cube[0][1]+200);
cout<<"Enter the values of tx,ty,tz";
cin>>tx>>ty>>tz;
int trans[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{tx,ty,tz,1}};
int t_cube[8][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)

{
for(k=0;k<4;k++)
{
t_cube[i][j]+=cube[i][k]*trans[k][j];
}
}
}
line(t_cube[0][0]+200,t_cube[0][1]+200,t_cube[1][0]+200,t_cube[1][1]+200);
line(t_cube[1][0]+200,t_cube[1][1]+200,t_cube[2][0]+200,t_cube[2][1]+200);
line(t_cube[2][0]+200,t_cube[2][1]+200,t_cube[3][0]+200,t_cube[3][1]+200);
line(t_cube[3][0]+200,t_cube[3][1]+200,t_cube[0][0]+200,t_cube[0][1]+200);
break;
case 2:
setcolor(RED);
line(cube[0][0]+200,cube[0][1]+200,cube[1][0]+200,cube[1][1]+200);
line(cube[1][0]+200,cube[1][1]+200,cube[2][0]+200,cube[2][1]+200);
line(cube[2][0]+200,cube[2][1]+200,cube[3][0]+200,cube[3][1]+200);
line(cube[3][0]+200,cube[3][1]+200,cube[0][0]+200,cube[0][1]+200);
cout<<"Enter the values of sx,sy,sz";
cin>>sx>>sy>>sz;
int trans1[4][4]={{sx,0,0,0},{0,sy,0,0},{0,0,sz,0},{0,0,0,1}};
int t_cube1[8][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
t_cube1[i][j]+=cube[i][k]*trans1[k][j];
}
}
}
line(t_cube1[0][0]+200,t_cube1[0][1]+200,t_cube1[1][0]+200,t_cube1[1][1]+200);
line(t_cube1[1][0]+200,t_cube1[1][1]+200,t_cube1[2][0]+200,t_cube1[2][1]+200);
line(t_cube1[2][0]+200,t_cube1[2][1]+200,t_cube1[3][0]+200,t_cube1[3][1]+200);
line(t_cube1[3][0]+200,t_cube1[3][1]+200,t_cube1[0][0]+200,t_cube1[0][1]+200);
break;
case 3:
line(0,300,600,300);
line(300,0,300,600);

int c;
setcolor(RED);
line(cube[0][0]+300,300-cube[0][1],cube[1][0]+300,300-cube[1][1]);
line(cube[1][0]+300,300-cube[1][1],cube[2][0]+300,300-cube[2][1]);
line(cube[2][0]+300,300-cube[2][1],cube[3][0]+300,300-cube[3][1]);
line(cube[3][0]+300,300-cube[3][1],cube[0][0]+300,300-cube[0][1]);
cout<<"reflection about: 1.yz plane 2.xz plane 3.origin";
cin>>c;
switch(c)
{
case 1:
int trans21[4][4]={{-1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
int
t_cube21[8][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
t_cube21[i][j]+=cube[i][k]*trans21[k][j];
}
}
}
setcolor(BLUE);
line(t_cube21[0][0]+300,300-t_cube21[0][1],t_cube21[1][0]+300,300-t_cube21[1][1]);
line(t_cube21[1][0]+300,300-t_cube21[1][1],t_cube21[2][0]+300,300-t_cube21[2][1]);
line(t_cube21[2][0]+300,300-t_cube21[2][1],t_cube21[3][0]+300,300-t_cube21[3][1]);
line(t_cube21[3][0]+300,300-t_cube21[3][1],t_cube21[0][0]+300,300-t_cube21[0][1]);
break;
case 2:
int trans22[4][4]={{1,0,0,0},{0,-1,0,0},{0,0,1,0},{0,0,0,1}};
int
t_cube22[8][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{

t_cube22[i][j]+=cube[i][k]*trans22[k][j];
}
}
}
setcolor(BLUE);
line(t_cube22[0][0]+300,300-t_cube22[0][1],t_cube22[1][0]+300,300-t_cube22[1][1]);
line(t_cube22[1][0]+300,300-t_cube22[1][1],t_cube22[2][0]+300,300-t_cube22[2][1]);
line(t_cube22[2][0]+300,300-t_cube22[2][1],t_cube22[3][0]+300,300-t_cube22[3][1]);
line(t_cube22[3][0]+300,300-t_cube22[3][1],t_cube22[0][0]+300,300-t_cube22[0][1]);
break;
case 3:
int trans23[4][4]={{-1,0,0,0},{0,-1,0,0},{0,0,-1,0},{0,0,0,1}};
int
t_cube23[8][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for(i=0;i<8;i++)
{
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
t_cube23[i][j]+=cube[i][k]*trans23[k][j];
}
}
}
setcolor(BLUE);
line(t_cube23[0][0]+300,300-t_cube23[0][1],t_cube23[1][0]+300,300-t_cube23[1][1]);
line(t_cube23[1][0]+300,300-t_cube23[1][1],t_cube23[2][0]+300,300-t_cube23[2][1]);
line(t_cube23[2][0]+300,300-t_cube23[2][1],t_cube23[3][0]+300,300-t_cube23[3][1]);
line(t_cube23[3][0]+300,300-t_cube23[3][1],t_cube23[0][0]+300,300-t_cube23[0][1]);
break;
}
}
getch() ;
closegraph() ;
}

You might also like