Skip to content

Commit

Permalink
Replaced usage of Ray with Vector #21
Browse files Browse the repository at this point in the history
Draw2d's Ray class is deprecated. This commit removes all its usage and
replaces it with the Vector class. To make migration easier two new
constructors where added to Vector.

Addresses #21
  • Loading branch information
azoitl authored and ptziegler committed Oct 6, 2024
1 parent 815dc0d commit c4dc821
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
FigureUtilitiesTest.class,
RectangleTest.class,
ColorConstantTest.class,
RayTest.class,
VectorTest.class,
StraightTest.class,
RelativeBendpointTest.class,
Expand Down
48 changes: 0 additions & 48 deletions org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/RayTest.java

This file was deleted.

10 changes: 5 additions & 5 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/FanRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -33,14 +33,14 @@ public final class ManhattanConnectionRouter extends AbstractRouter {
private final Map<Connection, ReservedInfo> reservedInfo = new HashMap<>();

private class ReservedInfo {
public List<Integer> reservedRows = new ArrayList<>(2);
public List<Integer> reservedCols = new ArrayList<>(2);
public final List<Integer> reservedRows = new ArrayList<>(2);
public final List<Integer> 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)
Expand All @@ -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);
Expand Down Expand Up @@ -100,9 +101,9 @@ private int getColumnNear(Connection connection, int r, int n, int x) {
* @return the direction from <i>r</i> to <i>p</i>
*/
@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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -193,29 +194,30 @@ protected Ray getStartDirection(Connection conn) {
return getDirection(rect, p);
}

protected void processPositions(Ray start, Ray end, List<Integer> positions, boolean horizontal, Connection conn) {
protected void processPositions(Vector start, Vector end, List<Double> 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++) {
pos[i + 1] = positions.get(i).intValue();
}
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++) {
Expand All @@ -241,7 +243,7 @@ protected void processPositions(Ray start, Ray end, List<Integer> 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);
}

Expand Down Expand Up @@ -286,73 +288,73 @@ 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<Integer> positions = new ArrayList<>(5);
List<Double> positions = new ArrayList<>(5);
boolean horizontal = startNormal.isHorizontal();
if (horizontal) {
positions.add(Integer.valueOf(start.y));
positions.add(start.y);
} else {
positions.add(Integer.valueOf(start.x));
positions.add(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;
} else {
i = average.x;
}
}
positions.add(Integer.valueOf(i));
positions.add(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;
} else {
i = average.x;
}
}
positions.add(Integer.valueOf(i));
positions.add(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(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(i);
horizontal = !horizontal;
}

Expand All @@ -361,20 +363,20 @@ public void route(Connection conn) {
} else {
i = average.x;
}
positions.add(Integer.valueOf(i));
positions.add(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(i);
horizontal = !horizontal;
}
}
}
if (horizontal) {
positions.add(Integer.valueOf(end.y));
positions.add(end.y);
} else {
positions.add(Integer.valueOf(end.x));
positions.add(end.x);
}

processPositions(start, end, positions, startNormal.isHorizontal(), conn);
Expand Down
30 changes: 27 additions & 3 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Vector.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -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 PrecisionPoint.
*
* @param start starting point
* @param end End Point
Expand All @@ -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.
*
Expand Down

0 comments on commit c4dc821

Please sign in to comment.