Thanks to visit codestin.com
Credit goes to www.scribd.com

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

2D Transformations in C++

This document contains code for performing 2D transformations - translation, rotation, scaling, and shearing - on lines in C graphics programming. It includes functions to get user input for coordinates and transformation values, perform the transformations by adjusting the coordinates, and display the original and transformed lines. Translation is done by adding values to x and y coordinates. Rotation uses trigonometry and matrix multiplication. Scaling multiplies coordinates by input values. Shearing displays lines sheared along the x and y axes separately.

Uploaded by

Sp Patel
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)
60 views5 pages

2D Transformations in C++

This document contains code for performing 2D transformations - translation, rotation, scaling, and shearing - on lines in C graphics programming. It includes functions to get user input for coordinates and transformation values, perform the transformations by adjusting the coordinates, and display the original and transformed lines. Translation is done by adding values to x and y coordinates. Rotation uses trigonometry and matrix multiplication. Scaling multiplies coordinates by input values. Shearing displays lines sheared along the x and y axes separately.

Uploaded by

Sp Patel
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
You are on page 1/ 5

2D Translation:

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter translation co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}

2D Rotation:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
clrscr();
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("\n\n\n[ Enter the angle");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
//printf("%lf %lf %lf %lf",r11,r12,r21,r22);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}

2D Scaling:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");
line(x1,y1,x2,y2);
getch();
closegraph();
}

2D shearing
#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <iostream.h>
int x1,y1,x2,y2,x,y;

int main(void)

int gdriver = DETECT, gmode, errorcode;

clrscr();

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

cout<<"enter the top left coordinates:";

cin>>x1>>y1;

cout<<"enter the bottom right coordinates:";

cin>>x2>>y2;

cout<<"enter the values of shearing coordinate for x_shear:\n";

cin>>x;

cout<<"enter the values of shearing coordinate for y_shear:\n";

cin>>y;

cleardevice();

rectangle(x1,y1,x2,y2);

cout<<"now hit a key to see shear in x_axis:";

getch();

rectangle(x1,y1,x2*x,y2);

cout<<"\nnow hit a key to see shear in y_axis:";

getch();

rectangle(x1,y1,x2,y2*y);

getch();

closegraph();

You might also like