Here’s something related with code translated from this answer on StackOverflow.
local function getSide(x1, y1, x2, y2, pointX, pointY) if x1 == x2 and y1 ~= y2 then if pointX \< x2 then return y2 \> y1 and 1 or -1 elseif pointX \> x2 then return y2 \> y1 and -1 or 1 end elseif y1 == y2 and x1 ~= x2 then if pointY \< y2 then return x2 \> x1 and -1 or 1 elseif pointY \> y2 then return x2 \> x1 and 1 or -1 end end local slope = (y2 - y1) / (x2 - x1) local yIntercept = y1 - x1 \* slope local solution = (slope \* pointX) + yIntercept if pointY \> solution then return x2 \> x1 and 1 or -1 elseif pointY \< solution then return x2 \> x1 and -1 or 1 end return 1 end local l1, l2 = display.newCircle(math.random(0, display.contentWidth), math.random(0, display.contentHeight), 10), display.newCircle(math.random(0, display.contentWidth), math.random(0, display.contentHeight), 10) local p = display.newCircle(display.contentCenterX, display.contentCenterY, 20) display.getCurrentStage():setFocus(p) local previousSide = getSide(l1.x, l1.y, l2.x, l2.y, p.x, p.y) function p:touch(event) p.x, p.y = event.x, event.y local side = getSide(l1.x, l1.y, l2.x, l2.y, p.x, p.y) if side ~= previousSide then print("Passed!") p.xScale, p.yScale = 1.5, 1.5 if p.transition then transition.cancel(p.transition) end p.transition = transition.to(p, { xScale = 1, yScale = 1, transition = easing.outElastic, time = 500 }) end previousSide = side if side == 1 then p:setFillColor(0.8, 0.8, 1) else p:setFillColor(1, 1, 0) end end p:addEventListener("touch") if previousSide == 1 then p:setFillColor(0.8, 0.8, 1) else p:setFillColor(1, 1, 0) end
To recap the code, basically all you need to see if you’ve passed a line is a function to tell you which side of the line you’re on. Then, store the previous side and check for if the current side is different from the previous side. When it changes, you’ll know you passed it.
This which-side-of-line can also be expanded to solve your original problem in a more robust way (probably a little slow, though): Calculate the perpendiculars to your two lines and see if you’re on the [x] side of one line and the [y] side of the other.
Something to note is that this passing code doesn’t tell you whether you’re between the two points - all it does is tell when you’ve passed a line from P1 to P2. This isn’t the same as a line segment : using this approach, when you pass outside the points you’ll still trigger the pass routine.