code / tomo-vectors

Lines135 Tomo127 Markdown8
(135 lines)
1 # A math vector library for 2D and 3D vectors of Nums or Ints
3 struct Vec2(x,y:Num)
4 ZERO := Vec2(0, 0)
5 func plus(a,b:Vec2->Vec2; inline)
6 return Vec2(a.x+b.x, a.y+b.y)
7 func minus(a,b:Vec2->Vec2; inline)
8 return Vec2(a.x-b.x, a.y-b.y)
9 func times(a,b:Vec2->Vec2; inline)
10 return Vec2(a.x*b.x, a.y*b.y)
11 func negative(v:Vec2->Vec2; inline)
12 return Vec2(-v.x, -v.y)
13 func dot(a,b:Vec2->Num; inline)
14 return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
15 func cross(a,b:Vec2->Num; inline)
16 return a.x*b.y - a.y*b.x
17 func scaled_by(v:Vec2, k:Num->Vec2; inline)
18 return Vec2(v.x*k, v.y*k)
19 func divided_by(v:Vec2, divisor:Num->Vec2; inline)
20 return Vec2(v.x/divisor, v.y/divisor)
21 func length(v:Vec2->Num; inline)
22 return (v.x*v.x + v.y*v.y).sqrt()
23 func dist(a,b:Vec2->Num; inline)
24 return a.minus(b).length()
25 func angle(v:Vec2->Num; inline)
26 return Num.atan2(v.y, v.x)
27 func norm(v:Vec2->Vec2; inline)
28 if v.x == 0 and v.y == 0
29 return v
30 len := v.length()
31 return Vec2(v.x/len, v.y/len)
32 func rotated(v:Vec2, radians:Num -> Vec2)
33 cos := radians.cos() or return v
34 sin := radians.sin() or return v
35 return Vec2(cos*v.x - sin*v.y, sin*v.x + cos*v.y)
36 func mix(a,b:Vec2, amount:Num -> Vec2)
37 return Vec2(
38 amount.mix(a.x, b.x),
39 amount.mix(a.y, b.y),
42 struct Vec3(x,y,z:Num)
43 ZERO := Vec3(0, 0, 0)
44 func plus(a,b:Vec3->Vec3; inline)
45 return Vec3(a.x+b.x, a.y+b.y, a.z+b.z)
46 func minus(a,b:Vec3->Vec3; inline)
47 return Vec3(a.x-b.x, a.y-b.y, a.z-b.z)
48 func times(a,b:Vec3->Vec3; inline)
49 return Vec3(a.x*b.x, a.y*b.y, a.z*b.z)
50 func negative(v:Vec3->Vec3; inline)
51 return Vec3(-v.x, -v.y, -v.z)
52 func dot(a,b:Vec3->Num; inline)
53 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)
54 func cross(a,b:Vec3->Vec3; inline)
55 return Vec3(a.y*b.z - a.z-b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x)
56 func scaled_by(v:Vec3, k:Num->Vec3; inline)
57 return Vec3(v.x*k, v.y*k, v.z*k)
58 func divided_by(v:Vec3, divisor:Num->Vec3; inline)
59 return Vec3(v.x/divisor, v.y/divisor, v.z/divisor)
60 func length(v:Vec3->Num; inline)
61 return (v.x*v.x + v.y*v.y + v.z*v.z).sqrt()
62 func dist(a,b:Vec3->Num; inline)
63 return a.minus(b).length()
64 func norm(v:Vec3->Vec3; inline)
65 if v.x == 0 and v.y == 0 and v.z == 0
66 return v
67 len := v.length()
68 return Vec3(v.x/len, v.y/len, v.z/len)
69 func mix(a,b:Vec3, amount:Num -> Vec3)
70 return Vec3(
71 amount.mix(a.x, b.x),
72 amount.mix(a.y, b.y),
73 amount.mix(a.z, b.z),
77 struct IVec2(x,y:Int)
78 ZERO := IVec2(0, 0)
79 func plus(a,b:IVec2->IVec2; inline)
80 return IVec2(a.x+b.x, a.y+b.y)
81 func minus(a,b:IVec2->IVec2; inline)
82 return IVec2(a.x-b.x, a.y-b.y)
83 func times(a,b:IVec2->IVec2; inline)
84 return IVec2(a.x*b.x, a.y*b.y)
85 func negative(v:IVec2->IVec2; inline)
86 return IVec2(-v.x, -v.y)
87 func dot(a,b:IVec2->Int; inline)
88 return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
89 func cross(a,b:IVec2->Int; inline)
90 return a.x*b.y - a.y*b.x
91 func scaled_by(v:IVec2, k:Int->IVec2; inline)
92 return IVec2(v.x*k, v.y*k)
93 func divided_by(v:IVec2, divisor:Int->IVec2; inline)
94 return IVec2(v.x/divisor, v.y/divisor)
95 func length(v:IVec2->Num; inline)
96 x := Num(v.x)
97 y := Num(v.y)
98 return Num.sqrt(x*x + y*y)
99 func dist(a,b:IVec2->Num; inline)
100 return a.minus(b).length()
101 func angle(v:IVec2->Num; inline)
102 return Num.atan2(Num(v.y), Num(v.x))
104 struct IVec3(x,y,z:Int)
105 ZERO := IVec3(0, 0, 0)
106 func plus(a,b:IVec3->IVec3; inline)
107 return IVec3(a.x+b.x, a.y+b.y, a.z+b.z)
108 func minus(a,b:IVec3->IVec3; inline)
109 return IVec3(a.x-b.x, a.y-b.y, a.z-b.z)
110 func times(a,b:IVec3->IVec3; inline)
111 return IVec3(a.x*b.x, a.y*b.y, a.z*b.z)
112 func negative(v:IVec3->IVec3; inline)
113 return IVec3(-v.x, -v.y, -v.z)
114 func dot(a,b:IVec3->Int; inline)
115 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)
116 func cross(a,b:IVec3->IVec3; inline)
117 return IVec3(a.y*b.z - a.z-b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x)
118 func scaled_by(v:IVec3, k:Int->IVec3; inline)
119 return IVec3(v.x*k, v.y*k, v.z*k)
120 func divided_by(v:IVec3, divisor:Int->IVec3; inline)
121 return IVec3(v.x/divisor, v.y/divisor, v.z/divisor)
122 func length(v:IVec3->Num; inline)
123 x := Num(v.x)
124 y := Num(v.y)
125 z := Num(v.z)
126 return Num.sqrt(x*x + y*y + z*z)
127 func dist(a,b:IVec3->Num; inline)
128 return a.minus(b).length()
130 func main()
131 >> Vec2(10, 20)
132 assert Vec2(10, 20) + Vec2(100, 100) == Vec2(x=110, y=120)
133 >> Vec3(10, 20, 30)
134 >> Vec2.ZERO