Measurements - jackbackrack/cons-beginners-course GitHub Wiki

Bounds

bounds of polygon

  • we can get the bounds of a shape as the smallest box that fits around it
  • the box is specified by the lowest point and the highest point
  • it is returned as an array of two vectors
const s = square({ center: true }).scale(10);
const b = s.getBounds();
const lo = b[0];
const hi = b[1];

where lo is the point that has the smallest x, y, and z values and hi has the largest values.

Dimensions

We can calculate dimensions by subtracting the highest from the lowest point, and create a square with the same size:

const s = square({ center: true }).scale(10);
const b = s.getBounds();
const lo = b[0];
const hi = b[1];
const dims = hi.minus(lo);
return square({ center: true }).scale([dims.x, dims.y]);

Centering

It's much easier to work with shapes when they are centered to start with. We showed how we can get the bounds as lo and hi points and how we can compute their dimensions by subtracting them from each other. If shapes are centered then the lo point is the negative of the hi point and the dimensions are two times the magnitude of the hi point. It turns out that we can center shapes as follows:

center(true, shape)

where the first argument is true if we want to center all dimensions and an array of booleans if we want to center only certain dimensions. For example, we can center in only x with:

center([true, false, false], shape)

homework

  • write a dims function that computes the dimensions of a given shape
  • write your own center function that can translate a shape so that it is centered
  • rewrite cornerHoles to use measurements to calculate corner positions
    • take hole diameter as parameter
    • take offset as parameter which is distance between hole and sides
  • write stackZ that takes two shapes as parameters
    • stacks the shape arguments on top of each other in Z using their bounds
  • rewrite 3D house in terms of stackZ
    • stack a roof over a cube