Commit 345e23ab authored by Piotr Gawron's avatar Piotr Gawron
Browse files

method computing point laying on the line implemented (it can be anywheere on...

method computing point laying on the line implemented (it can be anywheere on the line between two points)
parent 6068057b
......@@ -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);
}
}
......@@ -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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment