diff --git a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/Draw2dTestSuite.java b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/Draw2dTestSuite.java index 513d6793d..5e80d7edb 100644 --- a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/Draw2dTestSuite.java +++ b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/Draw2dTestSuite.java @@ -50,7 +50,6 @@ FigureUtilitiesTest.class, RectangleTest.class, ColorConstantTest.class, - RayTest.class, VectorTest.class, StraightTest.class, RelativeBendpointTest.class, diff --git a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/RayTest.java b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/RayTest.java deleted file mode 100644 index 397d3617f..000000000 --- a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/RayTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.draw2d.test; - -import org.eclipse.draw2d.geometry.Ray; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Ray's tests - * - * @author aboyko - * - */ -public class RayTest extends Assert { - - @Test - public void test_length() { - testLengthValues(3, 4, 5); - testLengthValues(0, Integer.MAX_VALUE, Integer.MAX_VALUE); - } - - @SuppressWarnings("static-method") - @Test - public void test_getScalarProduct() { - Ray a = new Ray(3, 2); - Ray b = new Ray(2, -2); - assertTrue(a.dotProduct(b) == 2); - } - - @SuppressWarnings("static-method") - private void testLengthValues(int x, int y, double expectedLength) { - Ray ray = new Ray(x, y); - assertEquals(expectedLength, ray.length(), 0); - } - -} diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/FanRouter.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/FanRouter.java index 2caf5c67b..09fe8fcb0 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/FanRouter.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/FanRouter.java @@ -15,7 +15,7 @@ import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.PointList; import org.eclipse.draw2d.geometry.PrecisionPoint; -import org.eclipse.draw2d.geometry.Ray; +import org.eclipse.draw2d.geometry.Vector; /** * Automatic router that spreads its {@link Connection Connections} in a @@ -54,13 +54,13 @@ protected void handleCollision(PointList points, int index) { Point midPoint = new Point((end.x + start.x) / 2, (end.y + start.y) / 2); int position = end.getPosition(start); - Ray ray; + Vector ray; if (position == PositionConstants.SOUTH || position == PositionConstants.EAST) { - ray = new Ray(start, end); + ray = new Vector(start, end); } else { - ray = new Ray(end, start); + ray = new Vector(end, start); } - double length = ray.length(); + double length = ray.getLength(); double xSeparation = separation * ray.x / length * (index / 2); double ySeparation = separation * ray.y / length * (index / 2); diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/ManhattanConnectionRouter.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/ManhattanConnectionRouter.java index 23e5e6981..e1766c2cf 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/ManhattanConnectionRouter.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/ManhattanConnectionRouter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2023 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -19,8 +19,8 @@ import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.PointList; -import org.eclipse.draw2d.geometry.Ray; import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.draw2d.geometry.Vector; /** * Provides a {@link Connection} with an orthogonal route between the @@ -33,14 +33,14 @@ public final class ManhattanConnectionRouter extends AbstractRouter { private final Map reservedInfo = new HashMap<>(); private class ReservedInfo { - public List reservedRows = new ArrayList<>(2); - public List reservedCols = new ArrayList<>(2); + public final List reservedRows = new ArrayList<>(2); + public final List reservedCols = new ArrayList<>(2); } - private static final Ray UP = new Ray(0, -1); - private static final Ray DOWN = new Ray(0, 1); - private static final Ray LEFT = new Ray(-1, 0); - private static final Ray RIGHT = new Ray(1, 0); + private static final Vector UP = new Vector(0, -1); + private static final Vector DOWN = new Vector(0, 1); + private static final Vector LEFT = new Vector(-1, 0); + private static final Vector RIGHT = new Vector(1, 0); /** * @see ConnectionRouter#invalidate(Connection) @@ -51,7 +51,8 @@ public void invalidate(Connection connection) { } private int getColumnNear(Connection connection, int r, int n, int x) { - int min = Math.min(n, x), max = Math.max(n, x); + int min = Math.min(n, x); + int max = Math.max(n, x); if (min > r) { max = min; min = r - (min - r); @@ -100,9 +101,9 @@ private int getColumnNear(Connection connection, int r, int n, int x) { * @return the direction from r to p */ @SuppressWarnings("static-method") - protected Ray getDirection(Rectangle r, Point p) { + protected Vector getDirection(Rectangle r, Point p) { int distance = Math.abs(r.x - p.x); - Ray direction; + Vector direction; direction = LEFT; @@ -126,7 +127,7 @@ protected Ray getDirection(Rectangle r, Point p) { return direction; } - protected Ray getEndDirection(Connection conn) { + protected Vector getEndDirection(Connection conn) { ConnectionAnchor anchor = conn.getTargetAnchor(); Point p = getEndPoint(conn); Rectangle rect; @@ -180,7 +181,7 @@ protected int getRowNear(Connection connection, int r, int n, int x) { return r; } - protected Ray getStartDirection(Connection conn) { + protected Vector getStartDirection(Connection conn) { ConnectionAnchor anchor = conn.getSourceAnchor(); Point p = getStartPoint(conn); Rectangle rect; @@ -193,14 +194,15 @@ protected Ray getStartDirection(Connection conn) { return getDirection(rect, p); } - protected void processPositions(Ray start, Ray end, List positions, boolean horizontal, Connection conn) { + protected void processPositions(Vector start, Vector end, List positions, boolean horizontal, + Connection conn) { removeReservedLines(conn); int[] pos = new int[positions.size() + 2]; if (horizontal) { - pos[0] = start.x; + pos[0] = (int) start.x; } else { - pos[0] = start.y; + pos[0] = (int) start.y; } int i; for (i = 0; i < positions.size(); i++) { @@ -208,14 +210,14 @@ protected void processPositions(Ray start, Ray end, List positions, boo } if (horizontal == (positions.size() % 2 == 1)) { i++; - pos[i] = end.x; + pos[i] = (int) end.x; } else { i++; - pos[i] = end.y; + pos[i] = (int) end.y; } PointList points = new PointList(); - points.addPoint(new Point(start.x, start.y)); + points.addPoint(new Point((int) start.x, (int) start.y)); Point p; boolean adjust; for (i = 2; i < pos.length - 1; i++) { @@ -241,7 +243,7 @@ protected void processPositions(Ray start, Ray end, List positions, boo } points.addPoint(p); } - points.addPoint(new Point(end.x, end.y)); + points.addPoint(new Point((int) end.x, (int) end.y)); conn.setPoints(points); } @@ -286,36 +288,36 @@ public void route(Connection conn) { if ((conn.getSourceAnchor() == null) || (conn.getTargetAnchor() == null)) { return; } - int i; + double i; Point startPoint = getStartPoint(conn); conn.translateToRelative(startPoint); Point endPoint = getEndPoint(conn); conn.translateToRelative(endPoint); - Ray start = new Ray(startPoint); - Ray end = new Ray(endPoint); - Ray average = start.getAveraged(end); + Vector start = new Vector(startPoint); + Vector end = new Vector(endPoint); + Vector average = start.getAveraged(end); - Ray direction = new Ray(start, end); - Ray startNormal = getStartDirection(conn); - Ray endNormal = getEndDirection(conn); + Vector direction = new Vector(start, end); + Vector startNormal = getStartDirection(conn); + Vector endNormal = getEndDirection(conn); - List positions = new ArrayList<>(5); + List positions = new ArrayList<>(5); boolean horizontal = startNormal.isHorizontal(); if (horizontal) { - positions.add(Integer.valueOf(start.y)); + positions.add(Double.valueOf(start.y)); } else { - positions.add(Integer.valueOf(start.x)); + positions.add(Double.valueOf(start.x)); } horizontal = !horizontal; - if (startNormal.dotProduct(endNormal) == 0) { - if ((startNormal.dotProduct(direction) >= 0) && (endNormal.dotProduct(direction) <= 0)) { + if (startNormal.getDotProduct(endNormal) == 0) { + if ((startNormal.getDotProduct(direction) >= 0) && (endNormal.getDotProduct(direction) <= 0)) { // 0 } else { // 2 - if (startNormal.dotProduct(direction) < 0) { - i = startNormal.similarity(start.getAdded(startNormal.getScaled(10))); + if (startNormal.getDotProduct(direction) < 0) { + i = startNormal.getSimilarity(start.getAdded(startNormal.getMultiplied(10))); } else { if (horizontal) { i = average.y; @@ -323,11 +325,11 @@ public void route(Connection conn) { i = average.x; } } - positions.add(Integer.valueOf(i)); + positions.add(Double.valueOf(i)); horizontal = !horizontal; - if (endNormal.dotProduct(direction) > 0) { - i = endNormal.similarity(end.getAdded(endNormal.getScaled(10))); + if (endNormal.getDotProduct(direction) > 0) { + i = endNormal.getSimilarity(end.getAdded(endNormal.getMultiplied(10))); } else { if (horizontal) { i = average.y; @@ -335,24 +337,24 @@ public void route(Connection conn) { i = average.x; } } - positions.add(Integer.valueOf(i)); + positions.add(Double.valueOf(i)); horizontal = !horizontal; } } else { - if (startNormal.dotProduct(endNormal) > 0) { + if (startNormal.getDotProduct(endNormal) > 0) { // 1 - if (startNormal.dotProduct(direction) >= 0) { - i = startNormal.similarity(start.getAdded(startNormal.getScaled(10))); + if (startNormal.getDotProduct(direction) >= 0) { + i = startNormal.getSimilarity(start.getAdded(startNormal.getMultiplied(10))); } else { - i = endNormal.similarity(end.getAdded(endNormal.getScaled(10))); + i = endNormal.getSimilarity(end.getAdded(endNormal.getMultiplied(10))); } - positions.add(Integer.valueOf(i)); + positions.add(Double.valueOf(i)); horizontal = !horizontal; } else { // 3 or 1 - if (startNormal.dotProduct(direction) < 0) { - i = startNormal.similarity(start.getAdded(startNormal.getScaled(10))); - positions.add(Integer.valueOf(i)); + if (startNormal.getDotProduct(direction) < 0) { + i = startNormal.getSimilarity(start.getAdded(startNormal.getMultiplied(10))); + positions.add(Double.valueOf(i)); horizontal = !horizontal; } @@ -361,20 +363,20 @@ public void route(Connection conn) { } else { i = average.x; } - positions.add(Integer.valueOf(i)); + positions.add(Double.valueOf(i)); horizontal = !horizontal; - if (startNormal.dotProduct(direction) < 0) { - i = endNormal.similarity(end.getAdded(endNormal.getScaled(10))); - positions.add(Integer.valueOf(i)); + if (startNormal.getDotProduct(direction) < 0) { + i = endNormal.getSimilarity(end.getAdded(endNormal.getMultiplied(10))); + positions.add(Double.valueOf(i)); horizontal = !horizontal; } } } if (horizontal) { - positions.add(Integer.valueOf(end.y)); + positions.add(Double.valueOf(end.y)); } else { - positions.add(Integer.valueOf(end.x)); + positions.add(Double.valueOf(end.x)); } processPositions(start, end, positions, startNormal.isHorizontal(), conn); diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Vector.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Vector.java index 7669c2b1d..4a1ef1e4e 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Vector.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Vector.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -41,7 +41,7 @@ public Vector(double x, double y) { } /** - * Constructs a Vector pointed in the direction specified by a Point. + * Constructs a Vector pointed in the direction specified by a PrecisionPoint. * * @param p the point */ @@ -50,9 +50,20 @@ public Vector(PrecisionPoint p) { y = p.preciseY(); } + /** + * Constructs a Vector pointed in the direction specified by a Point. + * + * @param p the point + * @since 3.18 + */ + public Vector(Point p) { + x = p.preciseX(); + y = p.preciseY(); + } + /** * Constructs a Vector representing the direction and magnitude between to - * provided Points. + * provided PrecisioinPoints. * * @param start starting point * @param end End Point @@ -62,6 +73,19 @@ public Vector(PrecisionPoint start, PrecisionPoint end) { y = end.preciseY() - start.preciseY(); } + /** + * Constructs a Vector representing the direction and magnitude between to + * provided Points. + * + * @param start starting point + * @param end End Point + * @since 3.18 + */ + public Vector(Point start, Point end) { + x = end.preciseX() - start.preciseX(); + y = end.preciseY() - start.preciseY(); + } + /** * Constructs a Vector representing the difference between two provided Vectors. *