Re: finding if point is inside irregular rectangle

new topic     » goto parent     » topic index » view thread      » older message » newer message

Chris wrote:

> What happens if both l[1][2] and l[2][2] < p[2]? It's still gonna check
> if p[1] is in bounds.

If both the l[1][y] and l[2][y] are both < p[y], then the line lies 
underneath the point.

But I see the bug in my code. If you have l1 above and to the left of the 
point, and l2 below and to the right, my code assumes that the point crosses 
the line. that's not the case - you can only assume that if both y values are 
above the point. otherwise, you *do* have to figure out the point of 
intersection, and see if the point lies below.

something like:

function crosses( sequence p, sequence l )
  atom x, y, x1, y1, x2, y2, t

  x = p[1] 
  y = p[2]

  x1 = l[1][1]
  y1 = l[1][2]

  x2 = l[2][1]
  y2 = l[2][2]

  -- is the point above the line?
  if y > y1 and y > y2 then
    -- the point is above, so it doesn't cross
    return 0
  end if

  -- is the point below the line?
  if y < y1 and y < y2 then
    -- point crosses line
    return 1
  end if

  -- does the line run left to right?
  if x2 < x1 then
    -- swap points
    t = x2
    x2 = x1
    x1 = t

    t = y2
    y2 = y1
    y1   = t
  end if

  -- point to left of line?
  if x <  x1 then
    return 0
  end if

  -- point to right of line?
  if x > x2 then
    return 0
  end if

 -- now we have to do math...

end function

It's too late at night for my brain to think this hard... At the 'we do math' 
part, you plug in the line equation:

   y = mx + b

and determine the (x',y') value where x' =  x. if y < y', that means that the 
point crosses. Otherwise, it doesn't.

I *think* that should cover it.

Sorry about that!

-- David Cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu