All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jbotsim.core.Point Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2008 - 2020, Arnaud Casteigts and the JBotSim contributors 
 *
 *
 * This file is part of JBotSim.
 *
 * JBotSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JBotSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JBotSim.  If not, see .
 *
 */
package io.jbotsim.core;

import java.io.Serializable;

/**
 * 

The {@link Point} used by JBotSim.

* *

The third dimension is optional. When not provided, it is implicitly set to zero.

*/ public class Point implements Serializable { public double x; public double y; public double z; private int nbDimensions; /** *

Creates a {@link Point} object.

*

The coordinates correspond to the origin of the system.

*/ public Point() { this(0, 0); } /** *

Copy constructor.

* @param p the {@link Point} to copy. */ public Point(Point p) { this(p.x, p.y, p.z); this.nbDimensions = p.nbDimensions; } /** *

Creates a {@link Point} object with the provided coordinates.

* @param x the abscissa, as a double. * @param y the ordinate, as a double. */ public Point(double x, double y) { this(x, y, 0); nbDimensions = 2; } /** *

Creates a {@link Point} object with the provided coordinates.

* @param x the abscissa, as a double. * @param y the ordinate, as a double. * @param z the applicate, as a double. */ public Point(double x, double y, double z) { this.x = x; this.y = y; this.z = z; nbDimensions = 3; } /** *

Computes the distance between two points.

* @param x1 abscissa of the first point, as a double. * @param y1 ordinate of the first point, as a double. * @param x2 abscissa of the second point, as a double. * @param y2 ordinate of the second point, as a double. * @return the distance between the two provided points, as a double. */ public static double distance(double x1, double y1, double x2, double y2) { x1 -= x2; y1 -= y2; return Math.sqrt(x1 * x1 + y1 * y1); } /** *

Computes the distance between the current point and the point represented by the provided coordinates.

* @param x the abscissa, as a double. * @param y the ordinate, as a double. * @param z the applicate, as a double. * @return the distance between the two provided points, as a double. */ public double distance(double x, double y, double z) { double d1 = getX() - x; double d2 = getY() - y; double d3 = getZ() - z; return Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3); } /** *

Gets the current applicate.

* @return the current applicate, as a double. */ public double getZ() { return z; } /** *

Gets the current abscissa.

* @return the current abscissa, as a double. */ public double getX() { return x; } /** *

Gets the current ordinate.

* @return the current ordinate, as a double. */ public double getY() { return y; } /** *

Changes the current coordinates to those of the provided {@link Point}.

* @param p the {@link Point} to copy coordinates from. */ public void setLocation(Point p) { setLocation(p.getX(), p.getY(), p.getZ()); } /** *

Changes the current coordinates to those the provided ones.

* @param x the new abscissa, as a double. * @param y the new ordinate, as a double. */ public void setLocation(double x, double y) { setLocation(x, y, 0); } /** *

Changes the current coordinates to those the provided ones.

* @param x the new abscissa, as a double. * @param y the new ordinate, as a double. * @param z the new applicate, as a double. */ protected void setLocation(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } /** *

Computes the distance between the current point and the point represented by the provided coordinates.

* @param x the abscissa, as a double. * @param y the ordinate, as a double. * @return the distance between the two provided points, as a double. */ public double distance(double x, double y) { x -= getX(); y -= getY(); return Math.sqrt(x * x + y * y); } /** *

Computes the distance between the current point and provided one.

* @param pt the other {@link Point}. * @return the distance between the two provided points, as a double. */ public double distance(Point pt) { double px = pt.getX() - this.getX(); double py = pt.getY() - this.getY(); double pz = pt.getZ() - this.getZ(); return Math.sqrt(px * px + py * py + pz * pz); } @Override public Object clone() { return new Point(this.x, this.y, this.z); } @Override public int hashCode() { long bits = java.lang.Double.doubleToLongBits(getX()); bits ^= java.lang.Double.doubleToLongBits(getY()) * 31; return (((int) bits) ^ ((int) (bits >> 32))); } @Override public boolean equals(Object obj) { if (obj instanceof Point) { Point p2d = (Point) obj; return (getX() == p2d.getX()) && (getY() == p2d.getY()) && (getZ() == p2d.getZ()); } return super.equals(obj); } @Override public String toString() { String s = "Point [x = " + getX() + ", y = " + getY(); s += (nbDimensions == 3) ? ", z = " + getZ() + "]" : "]"; return s; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy