this is a function i changed from c# not sure whats wrong with it but it returns false when the line originates under or to the left of the box
Code: Select all
function SegmentIntersectRectangle(a_rectangleMinX,a_rectangleMinY,a_rectangleMaxX,a_rectangleMaxY,a_p1x,a_p1y,a_p2x,a_p2y)
-- Find min and max X for the segment
local minX = a_p1x
local maxX = a_p2x
if a_p1x > a_p2x then
minX = a_p2x
maxX = a_p1x
end
-- Find the intersection of the segment's and rectangle's x-projections
if maxX > a_rectangleMaxX then
maxX = a_rectangleMaxX
end
if minX < a_rectangleMinX then
minX = a_rectangleMinX
end
if minX > maxX then-- If their projections do not intersect return false
return false
end
-- Find corresponding min and max Y for min and max X we found before
local minY = a_p1y
local maxY = a_p2y
local dx = a_p2x - a_p1x
if math.abs(dx) > 0.0000001 then
a = (a_p2y - a_p1y) / dx
b = a_p1y - a * a_p1x
minY = a * minX + b
maxY = a * maxX + b
end
if minY > maxY then
local tmp = maxY
maxY = minY
minY = tmp
end
-- Find the intersection of the segment's and rectangle's y-projections
if maxY > a_rectangleMaxY then
maxY = a_rectangleMaxY
end
if minY < a_rectangleMinY then
minY = a_rectangleMinY
end
if minY > maxY then -- If Y-projections do not intersect return false
return false
end
return true
end
found the code here if that helps
http://stackoverflow.com/questions/9935 ... ange-in-2d