public double getAngle(Link link) { Point2D fromPoint = new Point2D(link.getFromNode().getCoord().getX(), link.getFromNode().getCoord().getY()); Point2D toPoint = new Point2D(link.getToNode().getCoord().getX(), link.getToNode().getCoord().getY()); Vector2D linkVector = new Vector2D(fromPoint, toPoint); Vector2D shapeVector = getVector(link.getCoord()); return linkVector.getAngleTo(shapeVector); }
/** * @param point * @return the direction vector of the shape related to the point */ public Vector2D getVector(Coord point) { double nearestDistance = Double.POSITIVE_INFINITY; int nearestPointPos = -1; for (int p = 1; p <= points.size(); p++) { double distance = CoordUtils.calcEuclideanDistance(point, points.get(p)); if (distance < nearestDistance) { nearestDistance = distance; nearestPointPos = p; } } Coord prev = nearestPointPos == 1 ? null : points.get(nearestPointPos - 1); Coord next = nearestPointPos == points.size() ? null : points.get(nearestPointPos + 1); Vector2D v1 = prev == null ? new Vector2D() : new Vector2D( new Point2D(prev.getX(), prev.getY()), new Point2D(point.getX(), point.getY())); Vector2D v2 = next == null ? new Vector2D() : new Vector2D( new Point2D(point.getX(), point.getY()), new Point2D(next.getX(), next.getY())); return v1.getSum(v2); }