Round corners between non-axis-aligned connection lines properly
[simantics/platform.git] / bundles / org.simantics.diagram.connection / src / org / simantics / diagram / connection / rendering / BasicConnectionStyle.java
index b2895fb342d60fc1571b7f9b1fe4424c07144203..301cfdf092bdabb5ea368c763ada136908e196c6 100644 (file)
@@ -126,16 +126,18 @@ public class BasicConnectionStyle implements ConnectionStyle, Serializable {
                     double dy1 = curY-oldY;
                     double dx2 = curX-newX;
                     double dy2 = curY-newY;
-                    double maxRadius = 0.5 * Math.min(Math.sqrt(dx1*dx1 + dy1*dy1), Math.sqrt(dx2*dx2 + dy2*dy2));
+                    double r1 = Math.sqrt(dx1*dx1 + dy1*dy1);
+                    double r2 = Math.sqrt(dx2*dx2 + dy2*dy2);
+                    double maxRadius = 0.5 * Math.min(r1, r2);
                     double radius = Math.min(rounding, maxRadius);
-                    newPath.lineTo(curX + radius*Math.signum(oldX-curX), curY + radius*Math.signum(oldY-curY));
+                    double dx1Normalized = r1 > 0 ? dx1 / r1 : 0;
+                    double dy1Normalized = r1 > 0 ? dy1 / r1 : 0;
+                    double dx2Normalized = r2 > 0 ? dx2 / r2 : 0;
+                    double dy2Normalized = r2 > 0 ? dy2 / r2 : 0;
+                    newPath.lineTo(curX - radius*dx1Normalized, curY - radius*dy1Normalized);
                     newPath.curveTo(curX, curY,
                                     curX, curY,
-                                    curX + radius*Math.signum(newX-curX), curY + radius*Math.signum(newY-curY));
-
-                    //newPath.lineTo(curX + round*Math.signum(oldX-curX), curY + round*Math.signum(oldY-curY));
-                    //newPath.lineTo(curX + round*Math.signum(newX-curX), curY + round*Math.signum(newY-curY));
-                    //newPath.lineTo(curX, curY);
+                                    curX - radius*dx2Normalized, curY - radius*dy2Normalized);
                 }
                 else
                     ++state;