Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

19BCD7246 Cse2006 Lab3 L1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

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