0% found this document useful (0 votes)
118 views12 pages

2D Transformations in C Programming

The document describes code to perform 2D transformations on shapes in a C program, including translation, shearing, scaling, reflection, and rotation. Specifically, it provides code samples and output to: 1. Translate a triangle by moving it to the right and down. 2. Shear a triangle in the X and Y directions by shifting its vertices along the Y-axis or X-axis, respectively. 3. Scale a triangle by shrinking or enlarging it along the X and Y axes. 4. Reflect a triangle across the line X=Y. 5. Rotate a triangle 270 degrees clockwise around its center.
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
0% found this document useful (0 votes)
118 views12 pages

2D Transformations in C Programming

The document describes code to perform 2D transformations on shapes in a C program, including translation, shearing, scaling, reflection, and rotation. Specifically, it provides code samples and output to: 1. Translate a triangle by moving it to the right and down. 2. Shear a triangle in the X and Y directions by shifting its vertices along the Y-axis or X-axis, respectively. 3. Scale a triangle by shrinking or enlarging it along the X and Y axes. 4. Reflect a triangle across the line X=Y. 5. Rotate a triangle 270 degrees clockwise around its center.
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

1.

To write a C program to perform 2D basic transformations such as translation, rotation, scaling, shearing
and reflection.

OUTPUT:

X direction Shearing w.r.t Y reference

Y direction Shearing w.r.t X reference


Scaling:

Reflection:

w.r.t line x=y

Rotation:

Enter the Degree to rotate the object: 270


1. Translation:
CODE:
function setup(){
createCanvas(500,500);
background(255);
smooth();
}
function draw(){
var Tx = -120;
var Ty = 100
var x1 = 200;
var y1 = 200;
var x2 = 400;
var y2 = 200;
var x3 = 400;
var y3 = 300;
noFill();
stroke(0);
strokeWeight(3);
triangle(x1,y1,x2,y2,x3,y3);
triangle(x1+Tx,y1+Ty,x2+Tx,y2+Ty,x3+Tx,y3+Ty);
}
OUTPUT:

2. Shearing

(a) X direction shearing w.r.t Y reference


Code:
function setup(){
createCanvas(500,500);
background(255);
smooth();
}
function draw(){
var Shx = 1;
var x1 = 0;
var y1 = 0;
var x2 = 300;
var y2 = 0;
var x3 = 300;
var y3 = 150;
noFill();
stroke(0);
strokeWeight(3);
triangle(x1,y1,x2,y2,x3,y3);
triangle(x1+Shx*y1,y1,x2+Shx*y2,y2,x3+Shx*y3,y3);
}
OUTPUT:

(b) Y direction shearing w.r.t X reference


Code:
OUTPUT:
function setup(){
createCanvas(500,500);
background(255);
smooth();
}
function draw(){
var Shy = 1;
var x1 = 0;
var y1 = 0;
var x2 = 300;
var y2 = 0;
var x3 = 300;
var y3 = 150;
noFill();
stroke(0);
strokeWeight(3);
triangle(x1,y1,x2,y2,x3,y3);
triangle(x1,y1+Shy*x1,x2,y2+Shy*x2,x3,y3+Shy*x3);
}

OUTPUT:
3. Scaling
function setup(){
createCanvas(500,500);
background(255);
smooth();
}
function draw(){
var Sx = 0.85;
var Sy = 1.15;
var x1 = 150;
var y1 = 200;
var x2 = 450;
var y2 = 200;
var x3 = 450;
var y3 = 400;
noFill();
stroke(0);
strokeWeight(3);
triangle(x1,y1,x2,y2,x3,y3);
triangle(x1*Sx,y1*Sy,x2*Sx,y2*Sy,x3*Sx,y3*Sy);
}
OUTPUT:

4. Reflection
W.r.t x = y line
Code
function setup(){
createCanvas(500,500);
background(255);
smooth();
}
function draw(){
var x1 = 250;
var y1 = 250;
var x2 = 400;
var y2 = 250;
var x3 = 400;
var y3 = 350;
noFill();
stroke(0);
strokeWeight(3);
triangle(x1,y1,x2,y2,x3,y3);
triangle(y1,x1,y2,x2,y3,x3);
}

OUTPUT:
5. Rotation (Angle = 270 degrees)
function setup() {
createCanvas(500, 500);
fill(0);
}

function draw() {
background(255);
translate(225, 225);
drawGrid();
noFill();
stroke(0);
strokeWeight(2);
triangle(0,50,250,50,250,200);
rotate(radians(270));
translate(-250,-100);
triangle(0,50,200,50,200,200);
}

function drawGrid() {
stroke(200);
fill(120);
for (var x=-width; x < width; x+=50) {
line(x, -height, x, height);
text(x, x+1, 10);
}
for (var y=-height; y < height; y+=50) {
line(-width, y, width, y);
text(y, 1, y+10);
}
}

OUTPUT:

You might also like