1 package net.sf.openrocket.optimization.general;
3 import java.util.Arrays;
5 import net.sf.openrocket.util.MathUtil;
8 * An immutable n-dimensional coordinate point.
10 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12 public final class Point {
14 private final double[] point;
15 private double length = -1;
16 private double length2 = -1;
20 * Create a new point with all values zero.
22 * @param dim the dimensionality of the point
24 public Point(int dim) {
26 throw new IllegalArgumentException("Invalid dimensionality " + dim);
28 point = new double[dim];
33 * Create a new point filled with a specific value.
35 * @param dim the dimensionality of the point
36 * @param value the value for all dimensions
38 public Point(int dim, double value) {
40 Arrays.fill(point, value);
45 * Create a new point with specific values.
47 * @param value the values of the dimensions.
49 public Point(double... value) {
50 if (value.length == 0) {
51 throw new IllegalArgumentException("Zero-dimensional point not allowed");
53 point = value.clone();
58 * Create a copy of a point. Used locally to create copies.
60 * @param p the point to copy.
62 private Point(Point p) {
63 point = p.point.clone();
69 * Return the point dimensionality.
71 * @return the point dimensionality
79 public double get(int i) {
83 public Point set(int i, double v) {
84 Point p = new Point(this);
91 * Return a new point that is the sum of two points.
93 * @param other the point to add to this point.
94 * @return the sum of these points.
96 public Point add(Point other) {
97 Point p = new Point(this);
98 for (int i = 0; i < point.length; i++) {
99 p.point[i] += other.point[i];
106 * Return a new point that is the subtraction of two points.
108 * @param other the point to subtract from this point.
109 * @return the value of this - other.
111 public Point sub(Point other) {
112 Point p = new Point(this);
113 for (int i = 0; i < point.length; i++) {
114 p.point[i] -= other.point[i];
121 * Return this point multiplied by a scalar value.
123 * @param v the scalar to multiply with
124 * @return the scaled point
126 public Point mul(double v) {
127 Point p = new Point(this);
128 for (int i = 0; i < point.length; i++) {
136 * Return the length of this coordinate.
138 * @return the length.
140 public double length() {
142 length = Math.sqrt(length2());
149 * Return the squared length of this coordinate.
151 * @return the square of the length of thie coordinate.
153 public double length2() {
156 for (double p : point) {
166 * Return the point as an array.
168 * @return the point as an array.
170 public double[] asArray() {
171 return point.clone();
175 public boolean equals(Object obj) {
179 if (!(obj instanceof Point))
182 Point other = (Point) obj;
183 if (this.point.length != other.point.length)
186 for (int i = 0; i < point.length; i++) {
187 if (!MathUtil.equals(this.point[i], other.point[i]))
194 public int hashCode() {
196 for (double d : point) {
198 n += (int) (d * 1000);
204 public String toString() {
205 return "Point" + Arrays.toString(point);