100% found this document useful (1 vote)
2K views

Computer Graphics - Lab Project 4: 2D Transformation

Computer Graphics- Lab Project 4 : 2D Transformation Write code to Solve 2D Transformation using any programming language. Write code to solve Translation, Rotation, Scaling, and Shear.

Uploaded by

Zeeshan Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Computer Graphics - Lab Project 4: 2D Transformation

Computer Graphics- Lab Project 4 : 2D Transformation Write code to Solve 2D Transformation using any programming language. Write code to solve Translation, Rotation, Scaling, and Shear.

Uploaded by

Zeeshan Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Graphics (ITEC-613)

Lab Project 4 : 2D Transformation


BSIT P-IV

By: Dr. Zeeshan Bhatti

Task 1: Search and write how Matrices are used in Matlab and in CPP or JAVA. How to Solve simple
Matrices using these programming languages.
Programming Task: Write code to Solve 2D Transformation using any programming language.
Write code to solve Translation, Rotation, Scaling, and Shear.

Task 2: Write a program to implement 2D


Scaling
Coding:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()

{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}

Task 3: Write a program to implement 2D Rotation


Coding:
include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

void TriAngle(int x1,int y1,int x2,int y2,int


x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int
y3);
void main()
{
int gd=DETECT,gm;

int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
void TriAngle(int x1,int y1,int x2,int y2,int
x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);

line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int
y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
TriAngle(a1,b1,a2,b2,a3,b3);
}

Task 4: Write a program to implement 2D Translation Triangle Program


Coding:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void tri();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,"d:\\tc\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
getch();
tri();
getch();

}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}

Task 5: 2D Translation Using Matlab


%Simple demo of 2D Translation in dx,dy in MATLAB
dx = 3; % set translation values
dy = 2;
% create a simple box to draw and manipulate
% row 1 is x coords row 2 is y coords
pts = [1 1 4 4 1;1 4 4 1 1];
[n m] = size(pts);
% Plot box
figure(1)
plot(pts(1,1:end),pts(2,1:end),'b*-');
axis([0 5 0 5])
shg
% create a 2D Translation in dx, dy NEED TO MAKE A HOMOGENEOUS
COORDS matrix
trans = [1 0 dx;0 1 dy; 0 0 1];
% Do Translation
% Make PTS HOMOGENEOUS
homogeneous_pts = [pts; ones(1,5)];
%Translate
trans_pts = trans*homogeneous_pts;
% Plot Translated box
% just extract out X and Y points to plot ignore third dimension
hold on
plot(trans_pts(1,1:end),trans_pts(2,1:end),'r*-');
axis([0 10 0 10]);

Task 6: 2D Rotation Using Matlab


%Simple demo of 2D rotation in MATLAB
% create a simple box to draw and manipulate
% row 1 is x coords row 2 is y coords
pts = [1 1 4 4 1;1 4 4 1 1];
% Plot box
figure(1)
plot(pts(1,1:end),pts(2,1:end),'b*-');
axis([0 5 0 5])
shg

% create a 2D rotation matrix


rot = [cos_deg(30) sin_deg(30);-sin_deg(30) cos_deg(30)]
% Do Rotation
rot_pts = rot*pts
% Plot Rotated box
hold on
plot(rot_pts(1,1:end),rot_pts(2,1:end),'r*-');
axis([-8 8 -8 8]);

Task 7: 2D Shear using Matlab


% shear_demo_2D.m
close all
clear all
%Simple demo of 2D Shear in X in MATLAB
k = 3; % shear factor
% create a simple box to draw and manipulate
% row 1 is x coords row 2 is y coords
pts = [1 1 4 4 1;1 4 4 1 1];
% Plot box
figure(1)
plot(pts(1,1:end),pts(2,1:end),'b*-');
axis([0 5 0 5])
shg
% create a 2D Shear in X matrix
shear = [1 k;0 1]
% Do Shear
shear_pts = shear*pts;
% Plot Sheared box
hold on
plot(shear_pts(1,1:end),shear_pts(2,1:end),'r*-');
axis([0 20 0 5]);

You might also like