Geometry
Doodle provides many utilities for working with two-dimensional geometry.
Angle
The Angle type represents an angle, as the name suggests.
Most of the time you'll create Angles
using the extension methods shown below. Degrees and radians should be familiar, but turns may not be. One turn corresponds to a full circle (i.e. 360 degrees), so using turns in a convenient way to represent simple fractions or multiples of circles.
45.degrees
1.radians
0.5.turns // One turn is a full circle, so this is half a circle
There are various methods to perform arithmetic on angles. Here are some examples. See the Angle API for a complete list.
(45.degrees + 45.degrees) < 180.degrees
// res3: Boolean = true
(45.degrees * 2) < 90.degrees
// res4: Boolean = false
180.degrees - 0.5.turns
// res5: Angle = Angle(0.0)
Other useful methods are calculating the sine and cosine of an angle, and normalizing an angle to between zero and 360 degrees.
0.5.turns.sin
// res6: Double = 1.2246467991473532E-16
0.5.turns.cos
// res7: Double = -1.0
2.turns.normalize == 1.turns
// res8: Boolean = true
Point
A Point represents a location in the 2-D plane. We can construct points from cartesian (xy-coordinates) or polar (radius and angle) coordinates as shown below.
Point(1.0, 1.0) // cartesian coordinates
// res9: Point = Cartesian(x = 1.0, y = 1.0)
Point(1.0, 90.degrees) // polar coordinates
// res10: Point = Polar(r = 1.0, angle = Angle(1.5707963267948966))
No matter how we construct a Point
we can still access x- and y-coordinates or radius and angle.
val pt1 = Point(1.0, 0.0)
// pt1: Point = Cartesian(x = 1.0, y = 0.0)
pt1.x
// res11: Double = 1.0
pt1.y
// res12: Double = 0.0
pt1.r
// res13: Double = 1.0
pt1.angle
// res14: Angle = Angle(0.0)
Transform
A Transform, in Doodle, represents an affine transform in two-dimensions. The easiest way to create a Transform
is via the methods on the Transform. Here are some examples.
Transform.scale(5.0, -2.0)
Transform.rotate(90.degrees)
Transform.translate(10, 10)
A Transform
can be applied to a Point
to transform that point.
Transform.scale(5.0, -2.0)(Point(1,1))
// res18: Point = Cartesian(x = 5.0, y = -2.0)
Transform.rotate(90.degrees)(Point(1,1))
// res19: Point = Cartesian(x = -0.9999999999999999, y = 1.0)
Transform.translate(10, 10)(Point(1,1))
// res20: Point = Cartesian(x = 11.0, y = 11.0)
Transforms
can be composed together using the andThen
method.
Transform.scale(5.0, -2.0).andThen(Transform.translate(10, 10))(Point(1,1))
// res21: Point = Cartesian(x = 15.0, y = 8.0)
Transform.scale(5.0, -2.0).translate(10, 10)(Point(1,1)) // Shorter version
// res22: Point = Cartesian(x = 15.0, y = 8.0)
Vec
A Vec represents a two-dimensional vector. You can construct Vecs
from cartesian (xy-coordinates) or polar (length and angle) coordinates, just like Point
.
Vec(0, 1)
// res23: Vec = Vec(x = 0.0, y = 1.0)
Vec(1, 90.degrees)
// res24: Vec = Vec(x = 6.123233995736766E-17, y = 1.0)