From d85e2e74268a045e4cf49a899891b0f77fd8cec3 Mon Sep 17 00:00:00 2001 From: neviyn Date: Sun, 8 Nov 2020 20:41:23 +0000 Subject: [PATCH] Added simplified Tuple4 constructor --- src/tuple.nim | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/tuple.nim b/src/tuple.nim index f20a7bb..c829baa 100644 --- a/src/tuple.nim +++ b/src/tuple.nim @@ -12,23 +12,25 @@ type proc `==`*(lhs, rhs: Tuple4): bool = lhs.x =~ rhs.x and lhs.y =~ rhs.y and lhs.z =~ rhs.z and lhs.w =~ rhs.w -proc point*(x: float, y: float, z: float): Tuple4 = Tuple4(x: x, y: y, z: z, w: 1) +proc tuple4*(x: float, y: float, z: float, w: float): Tuple4 = Tuple4(x: x, y: y, z: z, w: w) -proc vector*(x: float, y: float, z: float): Tuple4 = Tuple4(x: x, y: y, z: z, w: 0) +proc point*(x: float, y: float, z: float): Tuple4 = tuple4(x, y, z, 1) + +proc vector*(x: float, y: float, z: float): Tuple4 = tuple4(x, y, z, 0) proc isPoint*(target: Tuple4): bool = target.w == 1 proc isVector*(target: Tuple4): bool = target.w == 0 -proc `+`*(lhs, rhs: Tuple4): Tuple4 = Tuple4(x: lhs.x + rhs.x, y: lhs.y + rhs.y, z: lhs.z + rhs.z, w: lhs.w + rhs.w) -proc `-`*(lhs, rhs: Tuple4): Tuple4 = Tuple4(x: lhs.x - rhs.x, y: lhs.y - rhs.y, z: lhs.z - rhs.z, w: lhs.w - rhs.w) -proc `-`*(tup: Tuple4): Tuple4 = Tuple4(x: -tup.x, y: -tup.y, z: -tup.z, w: -tup.w) -proc `*`*(tup: Tuple4, mag: float): Tuple4 = Tuple4(x: tup.x * mag, y: tup.y * mag, z: tup.z * mag, w: tup.w * mag) -proc `/`*(tup: Tuple4, mag: float): Tuple4 = Tuple4(x: tup.x / mag, y: tup.y / mag, z: tup.z / mag, w: tup.w / mag) +proc `+`*(lhs, rhs: Tuple4): Tuple4 = tuple4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w) +proc `-`*(lhs, rhs: Tuple4): Tuple4 = tuple4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w) +proc `-`*(tup: Tuple4): Tuple4 = tuple4(-tup.x, -tup.y, -tup.z, -tup.w) +proc `*`*(tup: Tuple4, mag: float): Tuple4 = tuple4(tup.x * mag, tup.y * mag, tup.z * mag, tup.w * mag) +proc `/`*(tup: Tuple4, mag: float): Tuple4 = tuple4(tup.x / mag, tup.y / mag, tup.z / mag, tup.w / mag) proc magnitude*(tup: Tuple4): float = sqrt(tup.x * tup.x + tup.y * tup.y + tup.z * tup.z + tup.w * tup.w) proc normalize*(tup: Tuple4): Tuple4 = let mag = tup.magnitude() - Tuple4(x: tup.x / mag, y: tup.y / mag, z: tup.z / mag, w: tup.w / mag) + tuple4(tup.x / mag, tup.y / mag, tup.z / mag, tup.w / mag) proc dot*(lhs, rhs: Tuple4): float = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w proc cross*(lhs, rhs: Tuple4): Tuple4 = vector(lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x) @@ -36,25 +38,25 @@ when isMainModule: import unittest suite "tuple": test "Manual Tuple4 is point": - let target = Tuple4(x: 2, y: 3, z: 4, w: 1) + let target = tuple4(2, 3, 4, 1) check(target.isPoint()) test "Manual Tuple4 is vector": - let target = Tuple4(x: 2, y: 3, z: 4, w: 0) + let target = tuple4(2, 3, 4, 0) check(target.isVector()) test "Point creation": let target = point(2, 3, 4) - check(target == Tuple4(x: 2, y: 3, z: 4, w: 1)) + check(target == tuple4(2, 3, 4, 1)) test "Vector creation": let target = vector(2, 3, 4) - check(target == Tuple4(x: 2, y: 3, z: 4, w: 0)) + check(target == tuple4(2, 3, 4, 0)) test "Tuple addition": - let t1 = Tuple4(x: 3, y: -2, z: 5, w: 1) - let t2 = Tuple4(x: -2, y: 3, z: 1, w: 0) - let result = Tuple4(x: 1, y: 1, z: 6, w: 1) + let t1 = tuple4(3, -2, 5, 1) + let t2 = tuple4(-2, 3, 1, 0) + let result = tuple4(1, 1, 6, 1) check(t1 + t2 == result) test "Point subtraction": @@ -73,20 +75,20 @@ when isMainModule: check(t1 - t2 == vector(-2, -4, -6)) test "Negation": - let t1 = Tuple4(x: 1, y: -2, z: 3, w: -4) - check(-t1 == Tuple4(x: -1, y: 2, z: -3, w: 4)) + let t1 = tuple4(1, -2, 3, -4) + check(-t1 == tuple4(-1, 2, -3, 4)) test "Multiply tuple by scalar": - let t1 = Tuple4(x: 1, y: -2, z: 3, w: -4) - check(t1 * 3.5 == Tuple4(x: 3.5, y: -7, z: 10.5, w: -14)) + let t1 = tuple4(1, -2, 3, -4) + check(t1 * 3.5 == tuple4(3.5, -7, 10.5, -14)) test "Multiple tuple by fraction": - let t1 = Tuple4(x: 1, y: -2, z: 3, w: -4) - check(t1 * 0.5 == Tuple4(x: 0.5, y: -1, z: 1.5, w: -2)) + let t1 = tuple4(1, -2, 3, -4) + check(t1 * 0.5 == tuple4(0.5, -1, 1.5, -2)) test "Divide tuple by scalar": - let t1 = Tuple4(x: 1, y: -2, z: 3, w: -4) - check(t1 / 2 == Tuple4(x: 0.5, y: -1, z: 1.5, w: -2)) + let t1 = tuple4(1, -2, 3, -4) + check(t1 / 2 == tuple4(0.5, -1, 1.5, -2)) test "Magnitude of vector(1, 0, 0)": check(vector(1, 0, 0).magnitude() == 1)