Page 1 of 1

line intersection with box

Posted: Sat Mar 30, 2013 3:55 pm
by raymond
im trying to find out if a line goes over or is in a box and return false

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
could someone help me fix this or give me a better way of doing this thanks in advance

found the code here if that helps
http://stackoverflow.com/questions/9935 ... ange-in-2d

Re: line intersection with box

Posted: Sun Mar 31, 2013 5:44 pm
by kikito
Take a look at my snippet for Line-segment intersection. It's tested and works very well.

Re: line intersection with box

Posted: Mon Apr 01, 2013 3:20 pm
by raymond
thank you, lol i couldn't find that when i searched for it.

one thing though I only want it to return a boolean true if it intersects with the box false if it doesn't ive tried to convert it but im still having the same problem :death:

Code: Select all

 function boxSegmentIntersection(l,t,w,h, x1,y1,x2,y2)
  local dx, dy  = x2-x1, y2-y1

  local t0, t1  = 0, 1
  local p, q, r

  for side = 1,4 do
    if     side == 1 then p,q = -dx, x1 - l
    elseif side == 2 then p,q =  dx, l + w - x1
    elseif side == 3 then p,q = -dy, y1 - t
    else                  p,q =  dy, t + h - y1
    end

    if p == 0 then
      if q < 0 then return false end  -- Segment is parallel and outside the bbox
    else
      r = q / p
      if p < 0 then
        if     r > t1 then return false 
        elseif r > t0 then t0 = r
        end
      else -- p > 0
        if     r < t0 then return false 
        elseif r < t1 then t1 = r
        end
      end
    end
  end
  return true
end
pls tell me whats wrong here :) thank you for all the help