geometry
Derived from Koto documentation (MIT, github.com/koto-lang/koto), maintained for koto-calc.
Utilities for working with geometry in Koto.
The module contains the Vec2, Vec3, and
Rect types.
rect
|| -> Rect
Initializes a default Rect with each component set to 0.
|x: Number, y: Number, width: Number, height: Number| -> Rect
|xy: Vec2, size: Vec2| -> Rect
Initializes a Rect with corresponding position and size.
Example
from geometry import rect, vec2
print! rect()
check! Rect{x: 0, y: 0, width: 0, height: 0}
print! rect 10, 20, 30, 40
check! Rect{x: 10, y: 20, width: 30, height: 40}
print! rect (vec2 -1, 2), (vec2 99, 100)
check! Rect{x: -1, y: 2, width: 99, height: 100}
vec2
|| -> Vec2
Initializes a default Vec2 with each component set to 0.
|x: Number| -> Vec2
Initializes a Vec2 with x specified, and y set to 0.
|x: Number, y: Number| -> Vec2
|xy: Vec2| -> Vec2
Initializes a Vec2 with corresponding x and y components.
Example
from geometry import vec2
print! vec2()
check! Vec2{x: 0, y: 0}
print! vec2 99, 100
check! Vec2{x: 99, y: 100}
vec3
|| -> Vec3
Initializes a default Vec3 with each component set to 0.
|x: Number| -> Vec3
Initializes a Vec3 with x specified, and all other components set to 0.
|x: Number, y: Number| -> Vec3
|xy: Vec2| -> Vec3
Initializes a Vec3 with x and y specified, and z set to 0.
|x: Number, y: Number, z: Number| -> Vec3
|xy: Vec2, z: Number| -> Vec3
|xyz: Vec3| -> Vec3
Initializes a Vec3 with specified x, y, and z components.
Example
from geometry import vec2, vec3
print! vec3()
check! Vec3{x: 0, y: 0, z: 0}
print! vec3 -1, 3
check! Vec3{x: -1, y: 3, z: 0}
print! vec3 10, 20, 30
check! Vec3{x: 10, y: 20, z: 30}
print! vec3 (vec2 -1, -2), 5
check! Vec3{x: -1, y: -2, z: 5}
Rect
The Rect type represents a 2-dimensional rectangle,
with a defined position and size.
The position is interpreted as being at the center of the rectangle.
Comparison operations are available, and the rect’s components are iterable.
Example
r = geometry.rect 10, 20, 30, 40
x, y, w, h = r
print! x, y, w, h
check! (10.0, 20.0, 30.0, 40.0)
Rect.left
|Rect| -> Number
Returns the position of rectangle’s left edge.
Example
# Create a rectangle centered at 0, 0
r = geometry.rect 0, 0, 200, 100
print! r.left()
check! -100.0
Rect.right
|Rect| -> Number
Returns the position of rectangle’s right edge.
Example
# Create a rectangle centered at 0, 0
r = geometry.rect 0, 0, 200, 100
print! r.right()
check! 100.0
Rect.top
|Rect| -> Number
Returns the position of rectangle’s top edge.
Example
# Create a rectangle centered at 0, 0
r = geometry.rect 0, 0, 200, 100
print! r.top()
check! 50.0
Rect.bottom
|Rect| -> Number
Returns the position of rectangle’s bottom edge.
Example
# Create a rectangle centered at 0, 0
r = geometry.rect 0, 0, 200, 100
print! r.bottom()
check! -50.0
Rect.width
|Rect| -> Number
Returns the width of the rectangle.
Example
r = geometry.rect 0, 0, 200, 100
print! r.width()
check! 200.0
Rect.height
|Rect| -> Number
Returns the width of the rectangle.
Example
r = geometry.rect 0, 0, 200, 100
print! r.height()
check! 100.0
Rect.center
|Rect| -> Vec2
Returns the center point of the rectangle.
Example
r = geometry.rect -100, 42, 200, 100
print! r.center()
check! Vec2{x: -100, y: 42}
Rect.x
|Rect| -> Vec2
Returns the x component of the rectangle’s center point.
Example
r = geometry.rect -100, 42, 200, 100
print! r.x()
check! -100.0
Rect.y
|Rect| -> Vec2
Returns the y component of the rectangle’s center point.
Example
r = geometry.rect -100, 42, 200, 100
print! r.y()
check! 42.0
Rect.contains
|Rect, xy: Vec2| -> Vec2
Returns true if the given Vec2 is located within the rectangle’s
bounds.
Example
from geometry import rect, vec2
r = rect 0, 0, 200, 200
print! r.contains vec2 50, 50
check! true
print! r.contains vec2 500, 500
check! false
Rect.set_center
|Rect, x: Number y: Number| -> Rect
|Rect, xy: Vec2| -> Rect
Sets the rect’s center position to the given x and y coordinates, and
returns the rect.
Example
from geometry import rect, vec2
r = rect 0, 0, 200, 200
print! r.set_center 10, 10
check! Rect{x: 10, y: 10, width: 200, height: 200}
print! r.set_center vec2()
check! Rect{x: 0, y: 0, width: 200, height: 200}
Vec2
The Vec2 type represents a 2-dimensional vector, with x and y coordinates.
Arithmetic operations are supported, and the vector’s coordinates are iterable.
Example
from geometry import vec2
print! (vec2 10, 20) + (vec2 30, 40)
check! Vec2{x: 40, y: 60}
v = vec2 50, 100
v *= 2 * vec2 0.5, 2
x, y = v
print! x, y
check! (50.0, 400.0)
print! v -= 100
check! Vec2{x: -50, y: 300}
Vec2.angle
|Vec2| -> Number
Returns the angle of the vector, expressed in radians.
Example
from geometry import vec2
print! (vec2 1, 0).angle()
check! 0.0
print '{(vec2 0, 1).angle():.3}'
check! 1.571
print '{(vec2 -1, 0).angle():.3}'
check! 3.142
print '{(vec2 0, -1).angle():.3}'
check! -1.571
Vec2.length
|Vec2| -> Number
Returns the length of the vector.
Example
from geometry import vec2
print! (vec2 0, 0).length()
check! 0.0
print! (vec2 3, 4).length()
check! 5.0
print! (vec2 -4, -3).length()
check! 5.0
Vec2.x
|Vec2| -> Number
Returns the x coordinate of the vector.
Example
from geometry import vec2
print! (vec2 -1, 0).x()
check! -1.0
print! (vec2 3, 4).x()
check! 3.0
Vec2.y
|Vec2| -> Number
Returns the y coordinate of the vector.
Example
from geometry import vec2
print! (vec2 0, -2).y()
check! -2.0
print! (vec2 3, 4).y()
check! 4.0
Vec3
The Vec3 type represents a 3-dimensional vector, with x, y, and z coordinates.
Arithmetic operations are supported, and the vector’s coordinates are iterable.
Example
from geometry import vec3
print! (vec3 10, 20, 30) + (vec3 40, 50, 60)
check! Vec3{x: 50, y: 70, z: 90}
v = 10 * vec3 5, 10, 15
v *= vec3 0.5, 2, -1
x, y, z = v
print! x, y, z
check! (25.0, 200.0, -150.0)
Vec3.x
|Vec3| -> Number
Returns the x coordinate of the vector.
Example
from geometry import vec3
print! (vec3 -1, 0, 1).x()
check! -1.0
Vec3.y
|Vec3| -> Number
Returns the y coordinate of the vector.
Example
from geometry import vec3
print! (vec3 -1, -2, -3).y()
check! -2.0
Vec3.z
|Vec3| -> Number
Returns the z coordinate of the vector.
Example
from geometry import vec3
print! (vec3 10, 20, 30).z()
check! 30.0
Vec3.length
|Vec3| -> Number
Returns the length of the vector.
Example
from geometry import vec3
print! (vec3 0, 0, 10).length()
check! 10.0
print! (vec3 1, 2, 2).length()
check! 3.0