vector-light.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. --[[
  2. Copyright (c) 2012 Matthias Richter
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. Except as contained in this notice, the name(s) of the above copyright holders
  12. shall not be used in advertising or otherwise to promote the sale, use or
  13. other dealings in this Software without prior written authorization.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ]]--
  22. local sqrt, cos, sin = math.sqrt, math.cos, math.sin
  23. local function str(x,y)
  24. return "("..tonumber(x)..","..tonumber(y)..")"
  25. end
  26. local function mul(s, x,y)
  27. return s*x, s*y
  28. end
  29. local function div(s, x,y)
  30. return x/s, y/s
  31. end
  32. local function add(x1,y1, x2,y2)
  33. return x1+x2, y1+y2
  34. end
  35. local function sub(x1,y1, x2,y2)
  36. return x1-x2, y1-y2
  37. end
  38. local function permul(x1,y1, x2,y2)
  39. return x1*x2, y1*y2
  40. end
  41. local function dot(x1,y1, x2,y2)
  42. return x1*x2 + y1*y2
  43. end
  44. local function det(x1,y1, x2,y2)
  45. return x1*y2 - y1*x2
  46. end
  47. local function eq(x1,y1, x2,y2)
  48. return x1 == x2 and y1 == y2
  49. end
  50. local function lt(x1,y1, x2,y2)
  51. return x1 < x2 or (x1 == x2 and y1 < y2)
  52. end
  53. local function le(x1,y1, x2,y2)
  54. return x1 <= x2 and y1 <= y2
  55. end
  56. local function len2(x,y)
  57. return x*x + y*y
  58. end
  59. local function len(x,y)
  60. return sqrt(x*x + y*y)
  61. end
  62. local function dist(x1,y1, x2,y2)
  63. return len(x1-x2, y1-y2)
  64. end
  65. local function normalize(x,y)
  66. local l = len(x,y)
  67. return x/l, y/l
  68. end
  69. local function rotate(phi, x,y)
  70. local c, s = cos(phi), sin(phi)
  71. return c*x - s*y, s*x + c*y
  72. end
  73. local function perpendicular(x,y)
  74. return -y, x
  75. end
  76. local function project(x,y, u,v)
  77. local s = (x*u + y*v) / (u*u + v*v)
  78. return s*u, s*v
  79. end
  80. local function mirror(x,y, u,v)
  81. local s = 2 * (x*u + y*v) / (u*u + v*v)
  82. return s*u - x, s*v - y
  83. end
  84. -- the module
  85. return {
  86. str = str,
  87. -- arithmetic
  88. mul = mul,
  89. div = div,
  90. add = add,
  91. sub = sub,
  92. permul = permul,
  93. dot = dot,
  94. det = det,
  95. cross = det,
  96. -- relation
  97. eq = eq,
  98. lt = lt,
  99. le = le,
  100. -- misc operations
  101. len2 = len2,
  102. len = len,
  103. dist = dist,
  104. normalize = normalize,
  105. rotate = rotate,
  106. perpendicular = perpendicular,
  107. project = project,
  108. mirror = mirror,
  109. }