Tranformations
This post is part of a course on geometric modeling at the Summer Liberal Arts Institute for Computer Science held at Carleton College in 2021.
All of our shape generators center the shapes around the origin. As we carve out new shapes using boolean operations, we want to be able to transform parts so that they have different sizes, locations, or orientations. In this exercise, you add a couple of utility functions to translate and scale your meshes.
Translate
Write a function named translate
. Translation moves an object along the three axes, but does not rotate or resize the object. Have it accept these parameters:
- The
mesh
to shift around. - The
offset
by which to shift the mesh. It is an array of three numbers. Element 0 is the x-offset, element 1 is the y-offset, and element 2 is the z-offset.
Inside the function, reassign every position in the mesh so that it is offset from its original position. Follow this pseudocode:
for each position apply offset to position
Test out your implementation of this method in render.js
by adding a call like this:
const mesh = generateCube();
translate(mesh, [2, 0, 0]);
You should see the cube shifted right 2 units.
Scale
Write a function named scale
. Have it accept these parameters:
- The
mesh
to shift around. - The
factors
by which to scale the mesh. It is an array of three numbers. Element 0 is the x-factor, element 1 is the y-factor, and element 2 is the z-factor.
Inside the function, reassign every position in the mesh so that it is scaled from its original position. Follow this pseudocode:
for each position apply scale to position
Test out your implementation of this method in render.js
by adding a call like this:
const mesh = generateCube();
scale(mesh, [1, 2, 1]);
You should see the cube scaled to be twice as tall as it is wide and deep.
Rotation
Another common operation is rotation. The math behind rotation is more involved. If you find yourself needing to rotate a part or wanting to understand how rotation is done, let me know and we can talk through its implementation.