diff --git a/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java b/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java index 01480e7b3fbb254ac5ab73d4ba6fe434115bf158..45c960554c1783062f4b99cc5147082b8664c14e 100644 --- a/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java +++ b/commons/src/main/java/lcsb/mapviewer/common/geometry/PointTransformation.java @@ -3,7 +3,7 @@ package lcsb.mapviewer.common.geometry; import java.awt.geom.Point2D; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.*; +import org.apache.logging.log4j.Logger; /** * Class for basic point transformations. @@ -12,53 +12,53 @@ import org.apache.logging.log4j.*; * */ public class PointTransformation { - /** - * Default class logger. - */ - @SuppressWarnings("unused") - private final Logger logger = LogManager.getLogger(PointTransformation.class.getName()); + /** + * Default class logger. + */ + @SuppressWarnings("unused") + private final Logger logger = LogManager.getLogger(PointTransformation.class.getName()); - /** - * Rotates point around center using the angle. - * - * @param point - * object that we want to rotate - * @param angle - * angle by which we want to rotate - * @param center - * central point around which we rotate the object - * @return the same object rotated by the appropriate angle - */ - public Point2D rotatePoint(final Point2D point, final double angle, final Point2D center) { - double s = Math.sin(angle); - double c = Math.cos(angle); + /** + * Rotates point around center using the angle. + * + * @param point + * object that we want to rotate + * @param angle + * angle by which we want to rotate + * @param center + * central point around which we rotate the object + * @return the same object rotated by the appropriate angle + */ + public Point2D rotatePoint(final Point2D point, final double angle, final Point2D center) { + double s = Math.sin(angle); + double c = Math.cos(angle); - // translate point back to origin: + // translate point back to origin: - point.setLocation(point.getX() - center.getX(), point.getY() - center.getY()); + point.setLocation(point.getX() - center.getX(), point.getY() - center.getY()); - // rotate point - double xnew = point.getX() * c - point.getY() * s; - double ynew = point.getX() * s + point.getY() * c; + // rotate point + double xnew = point.getX() * c - point.getY() * s; + double ynew = point.getX() * s + point.getY() * c; - // translate point back: - point.setLocation(xnew + center.getX(), ynew + center.getY()); - return point; - } + // translate point back: + point.setLocation(xnew + center.getX(), ynew + center.getY()); + return point; + } - /** - * Checks if a point given in the parameter is valid (can be used for - * drawing). The point is considered as valid if coordinates are finite (NaN - * and Infinity are invalid - they cannot be drawn). - * - * @param point - * point to check - * @return <code>true</code> if coordinates are normal real numbers, - * <code>false</code> otherwise (NaN, infinity) - */ - public boolean isValidPoint(Point2D point) { - return Double.isFinite(point.getX()) && Double.isFinite(point.getY()); - } + /** + * Checks if a point given in the parameter is valid (can be used for drawing). + * The point is considered as valid if coordinates are finite (NaN and Infinity + * are invalid - they cannot be drawn). + * + * @param point + * point to check + * @return <code>true</code> if coordinates are normal real numbers, + * <code>false</code> otherwise (NaN, infinity) + */ + public boolean isValidPoint(Point2D point) { + return Double.isFinite(point.getX()) && Double.isFinite(point.getY()); + } /** * Creates a copy of the point. @@ -71,4 +71,18 @@ public class PointTransformation { return new Point2D.Double(point.getX(), point.getY()); } + /** + * Returns a point on line. + * + * @param start + * @param end + * @param coef + * @return {@link Point2D} on line defined by input points + */ + public Point2D getPointOnLine(Point2D start, Point2D end, double coef) { + double x = start.getX()+(end.getX()-start.getX())*coef; + double y = start.getY()+(end.getY()-start.getY())*coef; + return new Point2D.Double(x, y); + } + } diff --git a/commons/src/test/java/lcsb/mapviewer/common/geometry/PointTransformationTest.java b/commons/src/test/java/lcsb/mapviewer/common/geometry/PointTransformationTest.java index 872d50bce88b05f5f9e1b570fa19a51824247176..f9bb7c1b83392cb26874ec19f4146b3cb214cedc 100644 --- a/commons/src/test/java/lcsb/mapviewer/common/geometry/PointTransformationTest.java +++ b/commons/src/test/java/lcsb/mapviewer/common/geometry/PointTransformationTest.java @@ -52,4 +52,44 @@ public class PointTransformationTest { assertEquals(0, p1.distance(p2), Configuration.EPSILON); } + @Test + public void testPointOnLineOnStart() { + PointTransformation pt = new PointTransformation(); + Point2D p1 = new Point2D.Double(1, 0); + Point2D p2 = new Point2D.Double(12, 38); + Point2D result = pt.getPointOnLine(p1, p2, 0); + + assertEquals(p1, result); + } + + @Test + public void testPointOnLineOnEnd() { + PointTransformation pt = new PointTransformation(); + Point2D p1 = new Point2D.Double(1, 0); + Point2D p2 = new Point2D.Double(12, 38); + Point2D result = pt.getPointOnLine(p1, p2, 1); + + assertEquals(p2, result); + } + + @Test + public void testPointOnLineOnCenter() { + PointTransformation pt = new PointTransformation(); + Point2D p1 = new Point2D.Double(1, 0); + Point2D p2 = new Point2D.Double(12, 38); + Point2D result = pt.getPointOnLine(p1, p2, 0.5); + + assertEquals(new Point2D.Double(6.5, 19), result); + } + + @Test + public void testPointOnLineInTheMiddle() { + PointTransformation pt = new PointTransformation(); + Point2D p1 = new Point2D.Double(2, 0); + Point2D p2 = new Point2D.Double(12, 20); + Point2D result = pt.getPointOnLine(p1, p2, 0.4); + + assertEquals(new Point2D.Double(6, 8), result); + } + }