mirror of
https://github.com/apache/commons-math.git
synced 2025-02-06 18:18:56 +00:00
MATH-1284: Restore Vector2D class as an abstract implementation of Vector<Euclidean2D> and now Cartesian2D extends Vector2D.
Restore the public interface of Vector2DFormat to act on Vector2D.
This commit is contained in:
parent
9be91f380c
commit
09c55eb812
@ -36,7 +36,7 @@ import org.apache.commons.math4.util.MathUtils;
|
|||||||
* <p>Instances of this class are guaranteed to be immutable.</p>
|
* <p>Instances of this class are guaranteed to be immutable.</p>
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public class Cartesian2D implements Point<Euclidean2D>, Vector<Euclidean2D> {
|
public class Cartesian2D extends Vector2D implements Point<Euclidean2D> {
|
||||||
|
|
||||||
/** Origin (coordinates: 0, 0). */
|
/** Origin (coordinates: 0, 0). */
|
||||||
public static final Cartesian2D ZERO = new Cartesian2D(0, 0);
|
public static final Cartesian2D ZERO = new Cartesian2D(0, 0);
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.commons.math4.geometry.euclidean.twod;
|
||||||
|
|
||||||
|
import org.apache.commons.math4.geometry.Vector;
|
||||||
|
|
||||||
|
/** This class represents a 2D vector.
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public abstract class Vector2D implements Vector<Euclidean2D> {
|
||||||
|
|
||||||
|
/** Get the abscissa of the vector.
|
||||||
|
* @return abscissa of the vector
|
||||||
|
* @see #Vector2D(double, double)
|
||||||
|
*/
|
||||||
|
public abstract double getX();
|
||||||
|
|
||||||
|
/** Get the ordinate of the vector.
|
||||||
|
* @return ordinate of the vector
|
||||||
|
* @see #Vector2D(double, double)
|
||||||
|
*/
|
||||||
|
public abstract double getY();
|
||||||
|
|
||||||
|
}
|
@ -108,26 +108,26 @@ public class Vector2DFormat extends VectorFormat<Euclidean2D> {
|
|||||||
@Override
|
@Override
|
||||||
public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo,
|
public StringBuffer format(final Vector<Euclidean2D> vector, final StringBuffer toAppendTo,
|
||||||
final FieldPosition pos) {
|
final FieldPosition pos) {
|
||||||
final Cartesian2D p2 = (Cartesian2D) vector;
|
final Vector2D p2 = (Vector2D) vector;
|
||||||
return format(toAppendTo, pos, p2.getX(), p2.getY());
|
return format(toAppendTo, pos, p2.getX(), p2.getY());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Cartesian2D parse(final String source) throws MathParseException {
|
public Vector2D parse(final String source) throws MathParseException {
|
||||||
ParsePosition parsePosition = new ParsePosition(0);
|
ParsePosition parsePosition = new ParsePosition(0);
|
||||||
Cartesian2D result = parse(source, parsePosition);
|
Vector2D result = parse(source, parsePosition);
|
||||||
if (parsePosition.getIndex() == 0) {
|
if (parsePosition.getIndex() == 0) {
|
||||||
throw new MathParseException(source,
|
throw new MathParseException(source,
|
||||||
parsePosition.getErrorIndex(),
|
parsePosition.getErrorIndex(),
|
||||||
Cartesian2D.class);
|
Vector2D.class);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Cartesian2D parse(final String source, final ParsePosition pos) {
|
public Vector2D parse(final String source, final ParsePosition pos) {
|
||||||
final double[] coordinates = parseCoordinates(2, source, pos);
|
final double[] coordinates = parseCoordinates(2, source, pos);
|
||||||
if (coordinates == null) {
|
if (coordinates == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -157,8 +157,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testParseSimpleNoDecimals() throws MathParseException {
|
public void testParseSimpleNoDecimals() throws MathParseException {
|
||||||
String source = "{1; 1}";
|
String source = "{1; 1}";
|
||||||
Cartesian2D expected = new Cartesian2D(1, 1);
|
Vector2D expected = new Cartesian2D(1, 1);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,8 +181,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{1" + getDecimalCharacter() +
|
"{1" + getDecimalCharacter() +
|
||||||
"23; 1" + getDecimalCharacter() +
|
"23; 1" + getDecimalCharacter() +
|
||||||
"43}";
|
"43}";
|
||||||
Cartesian2D expected = new Cartesian2D(1.23, 1.43);
|
Vector2D expected = new Cartesian2D(1.23, 1.43);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,8 +192,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{1" + getDecimalCharacter() +
|
"{1" + getDecimalCharacter() +
|
||||||
"2323; 1" + getDecimalCharacter() +
|
"2323; 1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
|
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,8 +203,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{-1" + getDecimalCharacter() +
|
"{-1" + getDecimalCharacter() +
|
||||||
"2323; 1" + getDecimalCharacter() +
|
"2323; 1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(-1.2323, 1.4343);
|
Vector2D expected = new Cartesian2D(-1.2323, 1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,8 +214,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{1" + getDecimalCharacter() +
|
"{1" + getDecimalCharacter() +
|
||||||
"2323; -1" + getDecimalCharacter() +
|
"2323; -1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(1.2323, -1.4343);
|
Vector2D expected = new Cartesian2D(1.2323, -1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,8 +225,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{1" + getDecimalCharacter() +
|
"{1" + getDecimalCharacter() +
|
||||||
"2323; 1" + getDecimalCharacter() +
|
"2323; 1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
|
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,8 +236,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{-1" + getDecimalCharacter() +
|
"{-1" + getDecimalCharacter() +
|
||||||
"2323; -1" + getDecimalCharacter() +
|
"2323; -1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(-1.2323, -1.4343);
|
Vector2D expected = new Cartesian2D(-1.2323, -1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,8 +247,8 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"{0" + getDecimalCharacter() +
|
"{0" + getDecimalCharacter() +
|
||||||
"0; -1" + getDecimalCharacter() +
|
"0; -1" + getDecimalCharacter() +
|
||||||
"4343}";
|
"4343}";
|
||||||
Cartesian2D expected = new Cartesian2D(0.0, -1.4343);
|
Vector2D expected = new Cartesian2D(0.0, -1.4343);
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,29 +258,29 @@ public abstract class Vector2DFormatAbstractTest {
|
|||||||
"[1" + getDecimalCharacter() +
|
"[1" + getDecimalCharacter() +
|
||||||
"2323 : 1" + getDecimalCharacter() +
|
"2323 : 1" + getDecimalCharacter() +
|
||||||
"4343]";
|
"4343]";
|
||||||
Cartesian2D expected = new Cartesian2D(1.2323, 1.4343);
|
Vector2D expected = new Cartesian2D(1.2323, 1.4343);
|
||||||
Cartesian2D actual = vector2DFormatSquare.parse(source);
|
Vector2D actual = vector2DFormatSquare.parse(source);
|
||||||
Assert.assertEquals(expected, actual);
|
Assert.assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseNan() throws MathParseException {
|
public void testParseNan() throws MathParseException {
|
||||||
String source = "{(NaN); (NaN)}";
|
String source = "{(NaN); (NaN)}";
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(Cartesian2D.NaN, actual);
|
Assert.assertEquals(Cartesian2D.NaN, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParsePositiveInfinity() throws MathParseException {
|
public void testParsePositiveInfinity() throws MathParseException {
|
||||||
String source = "{(Infinity); (Infinity)}";
|
String source = "{(Infinity); (Infinity)}";
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(Cartesian2D.POSITIVE_INFINITY, actual);
|
Assert.assertEquals(Cartesian2D.POSITIVE_INFINITY, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseNegativeInfinity() throws MathParseException {
|
public void testParseNegativeInfinity() throws MathParseException {
|
||||||
String source = "{(-Infinity); (-Infinity)}";
|
String source = "{(-Infinity); (-Infinity)}";
|
||||||
Cartesian2D actual = vector2DFormat.parse(source);
|
Vector2D actual = vector2DFormat.parse(source);
|
||||||
Assert.assertEquals(Cartesian2D.NEGATIVE_INFINITY, actual);
|
Assert.assertEquals(Cartesian2D.NEGATIVE_INFINITY, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user