Skip to content

orthogonal proc seems incorrect #88

Description

@ASVIEST

vmath/src/vmath.nim

Lines 1908 to 1922 in a57a0a7

proc orthogonal*[T](v: GVec3[T]): GVec3[T] =
## Returns orthogonal vector to given vector.
let
v = abs(v)
other: type(v) =
if v.x < v.y:
if v.x < v.z:
gvec3(T(1), 0, 0) # X_AXIS
else:
gvec3(T(0), 0, 1) # Z_AXIS
elif v.y < v.z:
gvec3(T(0), 1, 0) # Y_AXIS
else:
gvec3(T(0), 0, 1) # Z_AXIS
return cross(v, other)

I.e., vec3(0, -1, 1).orthogonal is incorrect. This is because it computes orthogonal for vec3(0, -1, 1).abs = vec3(0, 1, 1), so the result is vec3(0, 1, -1)
Correct implementation should be something like this:

proc orthogonal*[T](v: GVec3[T]): GVec3[T] =
  ## Returns orthogonal vector to given vector.
  let
    vAbs = abs(v)
    other: type(v) =
      if vAbs.x < vAbs.y:
        if vAbs.x < vAbs.z:
          gvec3(T(1), 0, 0) # X_AXIS
        else:
          gvec3(T(0), 0, 1) # Z_AXIS
      elif vAbs.y < vAbs.z:
        gvec3(T(0), 1, 0)   # Y_AXIS
      else:
        gvec3(T(0), 0, 1)   # Z_AXIS
  return cross(v, other)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions