svn commit: r16191 - trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: dthompson
Date: 2008-11-26 11:21:37-0800
New Revision: 16191

Modified:
   trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java

Log:
Issue 5517: Improved path painting so to stop line from being drawn inside box.

Modified: trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java?view=diff&rev=16191&p1=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java&p2=trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java&r1=16190&r2=16191
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/diagram/ui/PathItemPlacement.java	2008-11-26 11:21:37-0800
@@ -451,7 +451,7 @@
      */
     private double getSlope() {
 
-        final int slopeSegLen = 10; // segment size for computing slope
+        final int slopeSegLen = 40; // segment size for computing slope
 
         int pathLength = _pathFigure.getPerimeterLength();
         int pathDistance = getPathDistance();
@@ -591,7 +591,8 @@
         final float[] dashes = {1.0f, 4.0f};
         final float phase = 5f;
         final Point p1 = getAnchorPosition();
-        final Point p2 = getPoint();
+        Point p2 = getPoint();
+        Rectangle r = itemFig.getBounds();
         g.setColor(Color.RED);
         if (g instanceof Graphics2D) {
             final Graphics2D g2d = (Graphics2D) g;
@@ -604,6 +605,50 @@
                     phase);
             g2d.setStroke(dashedStroke);
         }
+        if (r.contains(p2)) {
+            g.drawRect(r.x, r.y, r.width, r.height);
+            p2 = getRectLineIntersection(r, p1, p2);     
+        }
         g.drawLine(p1.x, p1.y, p2.x, p2.y);
     }
+    
+    /**
+     * Finds the point where a rectangle and line intersect.
+     * Finds the intersection point between the border of a Rectangle r and 
+     * a line drawn between two Points pOut (outside the rectangle) and pIn 
+     * (inside the rectangle).
+     * Actually, we just do a cheat and find the midpoint of the edge which
+     * the the line crosses.
+     * If the pIn is not inside the rectangle, or if any other problem occurs,
+     * pIn is returned. 
+     * @param r Rectangle to find the intersection of.
+     * @param pOut Point outside the rectangle.
+     * @param pIn Point inside the rectangle.
+     * @return The intersection between Line(pOut, pIn) and Rectangle r.
+     */
+    private Point getRectLineIntersection(Rectangle r, Point pOut, Point pIn) {
+        Line2D.Double m, n;
+        m = new Line2D.Double(pOut, pIn);
+        n = new Line2D.Double(r.x, r.y, r.x + r.width, r.y);
+        if (m.intersectsLine(n)) {
+            return new Point(r.x + r.width / 2, r.y);
+        }
+        n = new Line2D.Double(r.x + r.width, r.y, r.x + r.width, 
+                r.y + r.height);
+        if (m.intersectsLine(n)) {
+            return new Point(r.x + r.width, r.y + r.height / 2);
+        }
+        n = new Line2D.Double(r.x, r.y + r.height, r.x + r.width, 
+                r.y + r.height);
+        if (m.intersectsLine(n)) {
+            return new Point(r.x + r.width / 2, r.y + r.height);
+        }
+        n = new Line2D.Double(r.x, r.y, r.x, r.y + r.width);
+        if (m.intersectsLine(n)) {
+            return new Point(r.x, r.y + r.height / 2);
+        }
+        // Should never get here.  If we do, return the inner point.
+        LOG.warn("Could not find rectangle intersection, using inner point.");
+        return pIn;
+    }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.