X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=sixth-3d.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgeometry%2FPoint2D.java;h=a6bb6a173929d1c1b9dc81c4b6ce8b4eeb910a6e;hp=a74baeeb22f8274ab537cabf315cb2768cafae8d;hb=f3da0bbe2c31d899156d410f8e75dabb0255efdf;hpb=03447008b8ee26a6463d2cd03005dc26464863db diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java index a74baee..a6bb6a1 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java @@ -1,14 +1,16 @@ /* - * Sixth 3D engine. Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - * + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.sixth.e3d.geometry; +import static java.lang.Math.sqrt; + +/** + * Used to represent point in a 2D space or vector. + * + * @see Point3D + */ public class Point2D implements Cloneable { public double x, y; @@ -21,28 +23,51 @@ public class Point2D implements Cloneable { this.y = y; } - public Point2D(final Point2D source) { - x = source.x; - y = source.y; + public Point2D(final Point2D parent) { + x = parent.x; + y = parent.y; } - public Point2D add(final Point2D direction) { - x += direction.x; - y += direction.y; + + /** + * Add other point to current point. Value of other point will not be changed. + * + * @return current point. + */ + public Point2D add(final Point2D otherPoint) { + x += otherPoint.x; + y += otherPoint.y; return this; } + /** + * @return true if current point coordinates are equal to zero. + */ + public boolean isZero() { + return (x == 0) && (y == 0); + } + @Override public Point2D clone() { return new Point2D(this); } - public void clone(final Point2D source) { - x = source.x; - y = source.y; + /** + * Copy coordinates from other point to current point. Value of other point will not be changed. + */ + public void clone(final Point2D otherPoint) { + x = otherPoint.x; + y = otherPoint.y; } - public Point2D computeMiddlePoint(final Point2D p1, final Point2D p2) { + /** + * Set current point to middle of two other points. + * + * @param p1 first point. + * @param p2 second point. + * @return current point. + */ + public Point2D setToMiddle(final Point2D p1, final Point2D p2) { x = (p1.x + p2.x) / 2d; y = (p1.y + p2.y) / 2d; return this; @@ -52,38 +77,84 @@ public class Point2D implements Cloneable { return Math.atan2(x - anotherPoint.x, y - anotherPoint.y); } + /** + * Compute distance to another point. + * + * @param anotherPoint point to compute distance to. + * @return distance from current point to another point. + */ public double getDistanceTo(final Point2D anotherPoint) { final double xDiff = x - anotherPoint.x; final double yDiff = y - anotherPoint.y; - return Math.sqrt(((xDiff * xDiff) + (yDiff * yDiff))); + return sqrt(((xDiff * xDiff) + (yDiff * yDiff))); + } + + /** + * Calculate length of vector. + * + * @return length of vector. + */ + public double getVectorLength() { + return sqrt(((x * x) + (y * y))); } + /** + * Invert current point. + * + * @return current point. + */ public Point2D invert() { x = -x; y = -y; return this; } + /** + * Round current point coordinates to integer. + */ public void roundToInteger() { x = (int) x; y = (int) y; } - public Point2D subtract(final Point2D direction) { - x -= direction.x; - y -= direction.y; + /** + * Subtract other point from current point. Value of other point will not be changed. + * + * @return current point. + */ + public Point2D subtract(final Point2D otherPoint) { + x -= otherPoint.x; + y -= otherPoint.y; return this; } + /** + * Convert current point to 3D point. + * Value of the z coordinate will be set to zero. + * + * @return 3D point. + */ public Point3D to3D() { return new Point3D(x, y, 0); } + /** + * Set current point to zero. + * + * @return current point. + */ public Point2D zero() { x = 0; y = 0; return this; } + @Override + public String toString() { + return "Point2D{" + + "x=" + x + + ", y=" + y + + '}'; + } }