This commit is contained in:
Araq 2013-07-24 23:07:28 +02:00
commit 9acdf94cc0
14 changed files with 663 additions and 1 deletions

View file

@ -0,0 +1,9 @@
import
tri_engine/config,
tri_engine/math/vec
type
TCircle* = tuple[p: TV2[TR], r: TR]
converter toCircle*(o: TR): TCircle =
(newV2(), o)

View file

@ -0,0 +1,8 @@
import
tri_engine/config,
tri_engine/math/vec
type
TRect* = tuple[min, size: TV2[TR]]
proc max*(o: TRect): TV2[TR] = o.min + o.size

View file

@ -0,0 +1,55 @@
import
macros,
tri_engine/config
type
TV2*[T:TNumber=TR] = array[0..1, T]
TV3*[T:TNumber=TR] = array[0..2, T]
TV4*[T:TNumber=TR] = array[0..3, T]
TVT*[T:TNumber=TR] = TV2|TV3|TV4
#TV2* = array[0..1, TR]
#TV3* = array[0..2, TR]
#TV4* = array[0..3, TR]
# TODO: Change to TVT when compiler issue is resolved.
proc `$`*[T](o: TV2[T]): string =
result = "("
for i in 0 .. <o.len:
result &= $o[0]
if i != o.len - 1:
result &= ", "
result & ")"
proc newV2T*[T](x, y: T=0): TV2[T] =
[x, y]
proc newV2*(x, y: TR=0.0): TV2[TR] =
[x, y]
proc newV2xy*(xy: TR): TV2[TR] =
[xy, xy]
proc x*[T](o: TV2[T]): T =
o[0]
proc y*[T](o: TV2[T]): T =
o[1]
proc `*`*(lhs: TV2[TR], rhs: TV2[TR]): TV2[TR] =
[(lhs.x * rhs.x).TR, (lhs.y * rhs.y).TR]
proc `+`*(lhs: TV2[TR], rhs: TV2[TR]): TV2[TR] =
[(lhs.x + rhs.x).TR, (lhs.y + rhs.y).TR]
#proc dotProduct[T](a: TVT[T], b: TVT[T]): T =
# for i in 0 .. a.len - 1:
# result += a[i] * b[i]
proc dot[T](a, b: TV2[T]): T =
for i in 0 .. <a.len:
result += a[i] * b[i]
assert dot(newV2(), newV2()) == 0.0
assert dot(newV2(2, 3), newV2(6, 7)) == 33.0
assert dot([2.0, 3.0], [6.0, 7.0]) == 33.0