Line intersections within a Rect

View: New views
2 Messages — Rating Filter:   Alert me  

Line intersections within a Rect

by Eirik Arthur Blekesaune :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi list,

I'm trying to find an elegant solution to the following problem, which  
is probably something I would have managed easily if it weren't for my  
sloppy geometry skills :-|

I have one linesegment, which is made from a simple class called  
Segment. It it is composed of two instances of Point. The linesegment  
is within one Rect.
I want to implement a method for extending my line to the points of  
intersection within the Rect, so that when this method is called, the  
segment is extended to the points of intersection with the borders of  
Rect.
The method could be called Segment.extendWithinRect(aRect)

Does anyone know an elegant solution to this problem? Any help is  
appreciated.

Best wishes,
Eirik Blekesaune


_______________________________________________
sc-users mailing list

info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

Re: Line intersections within a Rect

by Dan Stowell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi -

The equation for a straight line is y = mx + c, and so using your two
points you can solve this to find the values of m and c.

Say that the ponts are x1@y1 and x2@y2. After a bit of rearrangement
you find that

  m = (y1-y2) / (x1-x2)

and then

  c = y1 - (m * x1)

So now you know m and c. You can go back to the original equation and
use it to calculate where the line intersects the lines that define
the sides of the Rect (let's call it r).

  yint1 = m * r.left + c; // Intersection at r.left@yint1
  xint1 = (r.top - c) / m; // Intersection at xint1@...

That tells you two of the points, do a similar thing for r.bottom and
r.right. Two of these points will be genuine intersections on the
edges of the rectangle - the things you're looking for - while the
other two will not touch the edges themselves but intersect with their
projections further out. So you could select the ones you want with
something like

  [r.left@yint1, xint1@..., r.right@yint2,
xint2@...].select{|p| r.containsPoint(p)}

Now I feel like a maths teacher. Hope it helps tho

Dan


2008/7/10, Eirik Blekesaune <blekesaune@...>:

> Hi list,
>
>  I'm trying to find an elegant solution to the following problem, which is
> probably something I would have managed easily if it weren't for my sloppy
> geometry skills :-|
>
>  I have one linesegment, which is made from a simple class called Segment.
> It it is composed of two instances of Point. The linesegment is within one
> Rect.
>  I want to implement a method for extending my line to the points of
> intersection within the Rect, so that when this method is called, the
> segment is extended to the points of intersection with the borders of Rect.
>  The method could be called Segment.extendWithinRect(aRect)
>
>  Does anyone know an elegant solution to this problem? Any help is
> appreciated.
>
>  Best wishes,
>  Eirik Blekesaune
>
>
>  _______________________________________________
>  sc-users mailing list
>
>  info (subscribe and unsubscribe):
> http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
>  archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
>  search:
> http://www.listarc.bham.ac.uk/lists/sc-users/search/
>


--
http://www.mcld.co.uk

_______________________________________________
sc-users mailing list

info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
LightInTheBox - Buy quality products at wholesale price