about summary refs log tree commit diff stats
path: root/geom.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-05-18 15:29:18 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-05-18 15:29:18 -0700
commite27165cb9f0f0635f0290cb345442292faeba7fd (patch)
tree8eaa39a70e9f068e0a6b1acca79bf30fd7c903d2 /geom.lua
parent28c29eecdeacf51a262f598ddbc6a3d8131549eb (diff)
downloadtext.love-e27165cb9f0f0635f0290cb345442292faeba7fd.tar.gz
rectangle and square shapes
Diffstat (limited to 'geom.lua')
-rw-r--r--geom.lua24
1 files changed, 23 insertions, 1 deletions
diff --git a/geom.lua b/geom.lua
index 20cc5dd..7dd6628 100644
--- a/geom.lua
+++ b/geom.lua
@@ -7,7 +7,7 @@ function geom.on_shape(x,y, drawing, shape)
     return geom.on_line(x,y, drawing, shape)
   elseif shape.mode == 'manhattan' then
     return x == drawing.points[shape.p1].x or y == drawing.points[shape.p1].y
-  elseif shape.mode == 'polygon' then
+  elseif shape.mode == 'polygon' or shape.mode == 'rectangle' or shape.mode == 'square' then
     return geom.on_polygon(x,y, drawing, shape)
   elseif shape.mode == 'circle' then
     local center = drawing.points[shape.center]
@@ -82,6 +82,28 @@ function geom.on_polygon(x,y, drawing, shape)
   return geom.on_line(x,y, drawing, {p1=shape.vertices[1], p2=shape.vertices[#shape.vertices]})
 end
 
+-- are (x3,y3) and (x4,y4) on the same side of the line between (x1,y1) and (x2,y2)
+function geom.same_side(x1,y1, x2,y2, x3,y3, x4,y4)
+  if x1 == x2 then
+    return math.sign(x3-x1) == math.sign(x4-x1)
+  end
+  if y1 == y2 then
+    return math.sign(y3-y1) == math.sign(y4-y1)
+  end
+  local m = (y2-y1)/(x2-x1)
+  return math.sign(m*(x3-x1) + y1-y3) == math.sign(m*(x4-x1) + y1-y4)
+end
+
+function math.sign(x)
+  if x > 0 then
+    return 1
+  elseif x == 0 then
+    return 0
+  elseif x < 0 then
+    return -1
+  end
+end
+
 function geom.angle_with_hint(x1, y1, x2, y2, hint)
   local result = geom.angle(x1,y1, x2,y2)
   if hint then