From 6d986e1f0419ff76e4ba4b83832a1ae6b7b6e890 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 8 Sep 2024 18:48:22 -0400 Subject: Add vector :norm() --- examples/vectors.tm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'examples') diff --git a/examples/vectors.tm b/examples/vectors.tm index cee0f305..9f0ef20d 100644 --- a/examples/vectors.tm +++ b/examples/vectors.tm @@ -22,6 +22,16 @@ struct Vec2(x,y:Num): return a:minus(b):length() func angle(v:Vec2; inline)->Num: return Num.atan2(v.y, v.x) + func norm(v:Vec2; inline)->Vec2: + if v.x == 0 and v.y == 0: + return v + len := v:length() + return Vec2(v.x/len, v.y/len) + func mix(v,other:Vec2, amount:Num)->Vec2: + return Vec2( + v.x:mix(other.x, amount), + v.y:mix(other.y, amount), + ) struct Vec3(x,y,z:Num): ZERO := Vec3(0, 0, 0) @@ -43,6 +53,18 @@ struct Vec3(x,y,z:Num): return (v.x*v.x + v.y*v.y + v.z*v.z):sqrt() func dist(a,b:Vec3; inline)->Num: return a:minus(b):length() + func norm(v:Vec3; inline)->Vec3: + if v.x == 0 and v.y == 0 and v.z == 0: + return v + len := v:length() + return Vec3(v.x/len, v.y/len, v.z/len) + func mix(v,other:Vec3, amount:Num)->Vec3: + return Vec3( + v.x:mix(other.x, amount), + v.y:mix(other.y, amount), + v.z:mix(other.z, amount), + ) + struct IVec2(x,y:Int): ZERO := IVec2(0, 0) -- cgit v1.2.3