|
| 1 | +Bounds |
| 2 | +--- |
| 3 | + |
| 4 | +ShapeScript's [size](transforms.md#size) ands [scale](transforms.md#relative-transforms) commands let you control the relative size of a shape, but sometimes it's useful to know the exact dimensions. |
| 5 | + |
| 6 | +A cube of size 1 has an easily-predicted size of one world unit square, but what about a more complex shape, such as a 5-pointed star (see the [procedural paths](paths.md#procedural-paths) and [blocks](blocks.md) sections for details): |
| 7 | + |
| 8 | +```swift |
| 9 | +define star path { |
| 10 | + for 1 to 5 { |
| 11 | + point 0 -0.5 |
| 12 | + rotate 1 / 5 |
| 13 | + point 0 -1 |
| 14 | + rotate 1 / 5 |
| 15 | + } |
| 16 | + point 0 -0.5 |
| 17 | +} |
| 18 | + |
| 19 | +// draw star |
| 20 | +extrude { |
| 21 | + color red |
| 22 | + star |
| 23 | +} |
| 24 | + |
| 25 | +// draw cube |
| 26 | +cube { |
| 27 | + color green 0.5 |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +We can see that the star is larger than the unit cube, but other than trial-and-error or complex math, how can we get the exact size? This is where the `bounds` [member property](expressions.md#members) comes in. |
| 34 | + |
| 35 | +## Mesh Bounds |
| 36 | + |
| 37 | +Paths and meshes both expose a `bounds` property that represents a bounding box around the shape. From this you can get the exact size and position needed to place a box around the star: |
| 38 | + |
| 39 | +```swift |
| 40 | +define star { |
| 41 | + ... |
| 42 | +} |
| 43 | + |
| 44 | +// define star shape |
| 45 | +define shape extrude { |
| 46 | + color red |
| 47 | + star |
| 48 | +} |
| 49 | + |
| 50 | +// draw star |
| 51 | +shape |
| 52 | + |
| 53 | +// draw box around star |
| 54 | +cube { |
| 55 | + color green 0.5 |
| 56 | + position shape.bounds.center |
| 57 | + size shape.bounds.size |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Path Bounds |
| 64 | + |
| 65 | +In the example above we computed the bounds of a solid `mesh` (an extruded star-shaped `path`) but you can also get the bounds of a `path` directly. The following code draws the star path inside its bounding rectangle: |
| 66 | + |
| 67 | +```swift |
| 68 | +define star { |
| 69 | + ... |
| 70 | +} |
| 71 | + |
| 72 | +// draw star |
| 73 | +star |
| 74 | + |
| 75 | +// draw rectangle around star |
| 76 | +square { |
| 77 | + position shape.bounds.center |
| 78 | + size shape.bounds.size |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +## Bounds Members |
| 85 | + |
| 86 | +The `bounds` member property has the following sub-properties that you can use: |
| 87 | + |
| 88 | +* `min` - The position of the corner of the box with the smallest X, Y and Z values relative to the origin. |
| 89 | +* `max` - The position of the corner of the box with the largest X, Y and Z values relative to the origin. |
| 90 | +* `center` - The position of the center of the box relative to the origin. |
| 91 | +* `size` - The size (width, height and depth) of the box in world units. |
| 92 | +* `width` - The width of the box along the X axis (equivalent to `size.width`) |
| 93 | +* `height` - The height of the box along the Y axis (equivalent to `size.height`) |
| 94 | +* `depth` - The depth of the box along the Z axis (equivalent to `size.depth`) |
| 95 | + |
| 96 | +So, for example, to get the height of a shape, you could use: |
| 97 | + |
| 98 | +```swift |
| 99 | +print someShape.bounds.size.height |
| 100 | +``` |
| 101 | + |
| 102 | +or just: |
| 103 | + |
| 104 | +```swift |
| 105 | +print someShape.bounds.height |
| 106 | +``` |
| 107 | + |
| 108 | +And to get the X coordinate of its rightmost edge you could use: |
| 109 | + |
| 110 | +```swift |
| 111 | +print someShape.bounds.max.x |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | +[Index](index.md) | Next: [Meshes](meshes.md) |
0 commit comments