aboutsummaryrefslogtreecommitdiff
path: root/examples/vectors
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-03-10 16:02:16 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-03-10 16:02:16 -0400
commit038a9f548e2e26c90083fc95a459b63dc848daf7 (patch)
tree9e87cc1f33d320dc59cb3054267fd87b901b6dec /examples/vectors
parente2fd5b49fdafdc3019d8b8ac9fef0cccf0bb77b0 (diff)
Add some vector methods
Diffstat (limited to 'examples/vectors')
-rw-r--r--examples/vectors/vectors.tm12
1 files changed, 12 insertions, 0 deletions
diff --git a/examples/vectors/vectors.tm b/examples/vectors/vectors.tm
index c07880a5..3f07e500 100644
--- a/examples/vectors/vectors.tm
+++ b/examples/vectors/vectors.tm
@@ -8,6 +8,8 @@ struct Vec2(x,y:Num):
return Vec2(a.x-b.x, a.y-b.y)
func times(a,b:Vec2->Vec2; inline):
return Vec2(a.x*b.x, a.y*b.y)
+ func negative(v:Vec2->Vec2; inline):
+ return Vec2(-v.x, -v.y)
func dot(a,b:Vec2->Num; inline):
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
func cross(a,b:Vec2->Num; inline):
@@ -27,6 +29,10 @@ struct Vec2(x,y:Num):
return v
len := v:length()
return Vec2(v.x/len, v.y/len)
+ func rotated(v:Vec2, radians:Num -> Vec2):
+ cos := radians:cos() or return v
+ sin := radians:sin() or return v
+ return Vec2(cos*v.x - sin*v.y, sin*v.x + cos*v.y)
func mix(a,b:Vec2, amount:Num -> Vec2):
return Vec2(
amount:mix(a.x, b.x),
@@ -41,6 +47,8 @@ struct Vec3(x,y,z:Num):
return Vec3(a.x-b.x, a.y-b.y, a.z-b.z)
func times(a,b:Vec3->Vec3; inline):
return Vec3(a.x*b.x, a.y*b.y, a.z*b.z)
+ func negative(v:Vec3->Vec3; inline):
+ return Vec3(-v.x, -v.y, -v.z)
func dot(a,b:Vec3->Num; inline):
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z)
func cross(a,b:Vec3->Vec3; inline):
@@ -74,6 +82,8 @@ struct IVec2(x,y:Int):
return IVec2(a.x-b.x, a.y-b.y)
func times(a,b:IVec2->IVec2; inline):
return IVec2(a.x*b.x, a.y*b.y)
+ func negative(v:IVec2->IVec2; inline):
+ return IVec2(-v.x, -v.y)
func dot(a,b:IVec2->Int; inline):
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
func cross(a,b:IVec2->Int; inline):
@@ -99,6 +109,8 @@ struct IVec3(x,y,z:Int):
return IVec3(a.x-b.x, a.y-b.y, a.z-b.z)
func times(a,b:IVec3->IVec3; inline):
return IVec3(a.x*b.x, a.y*b.y, a.z*b.z)
+ func negative(v:IVec3->IVec3; inline):
+ return IVec3(-v.x, -v.y, -v.z)
func dot(a,b:IVec3->Int; inline):
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z)
func cross(a,b:IVec3->IVec3; inline):