Line intersection with Rect

  • Hi,

    does anybody know how Omnigraffle maintains the association line
    between two shapes always pointing towards the center of the shape? I
    guess I'll have to calculate the intersection between the line and the
    rect. Am I right? Or is there any code that already does this?

    Thanks

    ----------------------------------------------------------------------
    Pedro F. Campos
    Teaching Assistant
    University of Madeira
    http://dme.uma.pt/pcampos
    ----------------------------------------------------------------------

    _______________________________________________
    MacOSX-dev mailing list
    <MacOSX-dev...>
    http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • On 27 Apr 2004, at 09:08, Pedro Campos wrote:

    > Hi,
    >
    > does anybody know how Omnigraffle maintains the association line
    > between two shapes always pointing towards the center of the shape? I
    > guess I'll have to calculate the intersection between the line and the
    > rect. Am I right? Or is there any code that already does this?

    Here's Pattern Builder's line trimming routine.  If anyone's got a
    better algorithm...

    -(NSPoint)trimLineFromNSPoint:(NSPoint)from toNSPoint:(NSPoint)to
    toRect:(NSRect)rect
    {
      float mu;
      float cx = to.x;
      float cy = to.y;

      //NSLog(@"trimLineFromPoint:(%f,%f) toPoint:(%f,%f)
    toRect:(%f,%f,%f,%f)", from.x, from.y, to.x, to.y, rect.origin.x,
    rect.origin.y, rect.size.width, rect.size.height);

      if ( cx < rect.origin.x ) {
      //NSLog(@"to.x < rect.origin.x");
      cx = rect.origin.x;
      mu = ( cx - from.x ) / ( to.x - from.x );
      cy = from.y + ( mu * ( to.y - from.y ) );
      //NSLog(@"cx,cy(%f,%f)", cx,cy);
      }
      if ( cy > ( rect.origin.y + rect.size.height ) ) {
      //NSLog(@"to.y > ( rect.origin.y + rect.size.height )");
      cy = rect.origin.y + rect.size.height;
      mu = ( cy - from.y ) / ( to.y - from.y );
      cx = from.x + ( mu * ( to.x - from.x ) );
      //NSLog(@"cx,cy(%f,%f)", cx,cy);
      }
      if ( cx > ( rect.origin.x + rect.size.width ) ) {
      //NSLog(@"to.x > ( rect.origin.x + rect.size.width )");
      cx = rect.origin.x + rect.size.width;
      mu = ( cx - from.x ) / ( to.x - from.x );
      cy = from.y + ( mu * ( to.y - from.y ) );
      //NSLog(@"cx,cy(%f,%f)", cx,cy);
      }
      if ( cy < rect.origin.y ) {
      //NSLog(@"to.y < rect.origin.y");
      cy = rect.origin.y;
      mu = ( cy - from.y ) / ( to.y - from.y );
      cx = from.x + ( mu * ( to.x - from.x ) );
      //NSLog(@"cx,cy(%f,%f)", cx,cy);
      }
      //NSLog(@" trimmed to:(%f,%f)", cx, cy);
      return NSMakePoint(cx, cy);
    }

    -Neil

    --

    Neil Earnshaw
    Consultant Software Engineer
    Object Software Engineers Ltd
    <neil...>
    Tel  : 01747 850 676
    Mbl : 07793 084 161

    _______________________________________________
    MacOSX-dev mailing list
    <MacOSX-dev...>
    http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • On Apr 27, 2004, at 1:24 AM, Neil Earnshaw wrote:

    >
    > On 27 Apr 2004, at 09:08, Pedro Campos wrote:
    >
    >> Hi,
    >>
    >> does anybody know how Omnigraffle maintains the association line
    >> between two shapes always pointing towards the center of the shape? I
    >> guess I'll have to calculate the intersection between the line and
    >> the rect. Am I right? Or is there any code that already does this?
    >
    > Here's Pattern Builder's line trimming routine.  If anyone's got a
    > better algorithm...
    >
    > -(NSPoint)trimLineFromNSPoint:(NSPoint)from toNSPoint:(NSPoint)to
    > toRect:(NSRect)rect
    > {
    *snip*
    > }
    Neil's method is going to be most efficient if you are always
    dealing with rectangles.

    Alternatively, the easiest way to draw what you want is to always draw
    the line first, and go all the way to the center of the shape, then
    fill the shape afterward (thus drawing over the portion of the line
    inside the shape). That method won't let you find the intersection
    point for other purposes, of course. (If you wanted to do arrowheads or
    something, say...)

    Code to find the intersection of a line and an arbitrary bezier path
    (the equivalent of Neil's code but for any shape) is in OmniAppKit's
    NSBezierPath-OAExtensions. The method is -
    (BOOL)intersectionWithLine:(NSPoint *)result
    lineStart:(NSPoint)lineStart lineEnd:(NSPoint)lineEnd;

    Hope this helps,
    - Greg

    _______________________________________________
    MacOSX-dev mailing list
    <MacOSX-dev...>
    http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • On 27 Apr 2004, at 15:37, Greg Titus wrote:

    > Code to find the intersection of a line and an arbitrary bezier path
    > (the equivalent of Neil's code but for any shape) is in OmniAppKit's
    > NSBezierPath-OAExtensions. The method is -
    > (BOOL)intersectionWithLine:(NSPoint *)result
    > lineStart:(NSPoint)lineStart lineEnd:(NSPoint)lineEnd;

    Bugger.  You're telling me there was a method there all along?  I
    wracked my brain for hours trying to remember my school geometry form
    20 years ago to solve that problem.

    And yeah, I should have said, if you don't care about locating the
    intersection point and your shape is opaque, then you can just draw the
    shapes over the lines.  That's what I did with one of my early
    iterations.

    -Neil

    --

    Neil Earnshaw
    Consultant Software Engineer
    Object Software Engineers Ltd
    <neil...>
    Tel  : 01747 850 676
    Mbl : 07793 084 161

    _______________________________________________
    MacOSX-dev mailing list
    <MacOSX-dev...>
    http://www.omnigroup.com/mailman/listinfo/macosx-dev
previous month april 2004 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today