Skip to content

Commit ceba215

Browse files
committed
introduced norm and metric space
1 parent fb8baba commit ceba215

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

src/main/java/net/itarray/automotion/internal/UIElement.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.itarray.automotion.internal.geometry.Direction;
44
import net.itarray.automotion.internal.geometry.ExtendGiving;
5-
import net.itarray.automotion.internal.geometry.GroupElement;
5+
import net.itarray.automotion.internal.geometry.MetricSpace;
66
import net.itarray.automotion.internal.geometry.Interval;
77
import net.itarray.automotion.internal.geometry.Rectangle;
88
import net.itarray.automotion.internal.geometry.Scalar;
@@ -93,7 +93,7 @@ public Scalar getEnd(Direction direction) {
9393
return direction.end(rectangle);
9494
}
9595

96-
public <V extends GroupElement<V>> V getExtend(ExtendGiving<V> direction) {
96+
public <V extends MetricSpace<V>> V getExtend(ExtendGiving<V> direction) {
9797
return direction.extend(rectangle);
9898
}
9999

@@ -149,7 +149,7 @@ public boolean hasEqualBottomOffsetAs(UIElement other) {
149149
return hasEqualBegin(other, UP);
150150
}
151151

152-
private <V extends GroupElement<V>> boolean hasEqualExtendAs(UIElement other, ExtendGiving<V> direction) {
152+
private <V extends MetricSpace<V>> boolean hasEqualExtendAs(UIElement other, ExtendGiving<V> direction) {
153153
return getExtend(direction).equals(other.getExtend(direction));
154154
}
155155

@@ -285,7 +285,7 @@ public void validateSameWidth(UIElement element, Errors errors) {
285285
validateSameExtend(RIGHT, element, errors);
286286
}
287287

288-
public <V extends GroupElement<V>> void validateSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
288+
public <V extends MetricSpace<V>> void validateSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
289289
if (!hasEqualExtendAs(element, direction)) {
290290
errors.add(
291291
String.format("Element %s has not the same %s as element %s. %s of %s is %s. %s of element is %s",
@@ -301,7 +301,7 @@ public <V extends GroupElement<V>> void validateSameExtend(ExtendGiving<V> direc
301301
}
302302
}
303303

304-
public <V extends GroupElement<V>> void validateNotSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
304+
public <V extends MetricSpace<V>> void validateNotSameExtend(ExtendGiving<V> direction, UIElement element, Errors errors) {
305305
if (hasEqualExtendAs(element, direction)) {
306306
errors.add(
307307
String.format("Element %s has the same %s as element %s. %s of %s is %s. %s of element is %s",

src/main/java/net/itarray/automotion/internal/geometry/ExtendGiving.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.itarray.automotion.internal.geometry;
22

3-
public interface ExtendGiving<V extends GroupElement<V>> {
3+
public interface ExtendGiving<V extends MetricSpace<V>> {
44
String extendName();
55
V begin(Rectangle rectangle);
66
V end(Rectangle rectangle);

src/main/java/net/itarray/automotion/internal/geometry/GroupElement.java renamed to src/main/java/net/itarray/automotion/internal/geometry/MetricSpace.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package net.itarray.automotion.internal.geometry;
22

3-
public interface GroupElement<V> {
3+
public interface MetricSpace<V> {
44
V plus(V addend);
55
V minus(V subtrahend);
66
String toStringWithUnits(String units);
7+
Scalar norm();
78
}

src/main/java/net/itarray/automotion/internal/geometry/Scalar.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import net.itarray.automotion.validation.properties.Condition;
55
import org.apache.commons.math3.fraction.Fraction;
66

7-
public class Scalar implements GroupElement<Scalar>, Comparable<Scalar> {
7+
public class Scalar implements MetricSpace<Scalar>, Comparable<Scalar> {
88
private final Fraction fraction;
99

1010
private Scalar(int value) {
@@ -50,6 +50,10 @@ public int intValue() { // todo: remove usages, this is introspection
5050
return fraction.intValue();
5151
}
5252

53+
public Fraction fractionValue() {
54+
return fraction;
55+
}
56+
5357
public Scalar plus(int addend) {
5458
return plus(scalar(addend));
5559
}
@@ -114,4 +118,8 @@ public Scalar min(Scalar other) {
114118
public Scalar max(Scalar other) {
115119
return isGreaterOrEqualTo(other) ? this : other;
116120
}
121+
122+
public Scalar norm() {
123+
return abs();
124+
}
117125
}

src/main/java/net/itarray/automotion/internal/geometry/Vector.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.itarray.automotion.internal.geometry;
22

3+
import org.apache.commons.math3.fraction.Fraction;
4+
35
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
46

5-
public class Vector implements GroupElement<Vector> {
7+
public class Vector implements MetricSpace<Vector> {
68
private final Scalar x;
79
private final Scalar y;
810

@@ -56,4 +58,10 @@ public Vector minus(Vector subtrahend) {
5658
public Vector plus(Vector addend) {
5759
return new Vector(x.plus(addend.getX()), y.plus(addend.getY()));
5860
}
61+
62+
public Scalar norm() {
63+
Fraction sumOfCoordinateSquares = x.times(x).plus(y.times(y)).fractionValue();
64+
double distance = Math.sqrt(sumOfCoordinateSquares.doubleValue());
65+
return scalar(new Fraction(distance));
66+
}
5967
}

src/main/java/net/itarray/automotion/internal/properties/ConditionedExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package net.itarray.automotion.internal.properties;
22

33
import net.itarray.automotion.internal.geometry.Direction;
4-
import net.itarray.automotion.internal.geometry.GroupElement;
4+
import net.itarray.automotion.internal.geometry.MetricSpace;
55
import net.itarray.automotion.validation.properties.Condition;
66
import net.itarray.automotion.validation.properties.Expression;
77

@@ -27,6 +27,6 @@ public String getDescription(Context context, Direction direction) {
2727
toBeConditioned.getDescription(context, direction),
2828
toBeApplied.getDescription(context, direction),
2929
toBeConditioned.getRepeatedDescription(context, direction),
30-
(t instanceof GroupElement) ? ((GroupElement) t).toStringWithUnits("px") : t);
30+
(t instanceof MetricSpace) ? ((MetricSpace) t).toStringWithUnits("px") : t);
3131
}
3232
}

src/main/java/net/itarray/automotion/internal/properties/ElementPropertyExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.itarray.automotion.internal.UIElement;
44
import net.itarray.automotion.internal.geometry.Direction;
55
import net.itarray.automotion.internal.geometry.ExtendGiving;
6-
import net.itarray.automotion.internal.geometry.GroupElement;
6+
import net.itarray.automotion.internal.geometry.MetricSpace;
77
import net.itarray.automotion.validation.properties.Expression;
88

99
import java.util.function.Function;
@@ -18,7 +18,7 @@ private ElementPropertyExpression(UIElement element, Function<UIElement, T> prop
1818
this.property = new ElementProperty<>(property, descriptionFormat, name);
1919
}
2020

21-
public static <V extends GroupElement<V>> ElementPropertyExpression<V> extend(ExtendGiving<V> direction, UIElement element) {
21+
public static <V extends MetricSpace<V>> ElementPropertyExpression<V> extend(ExtendGiving<V> direction, UIElement element) {
2222
return new ElementPropertyExpression<>(element, e -> e.getExtend(direction), direction.extendName() +
2323
" of element %s", direction.extendName());
2424
}

src/test/java/net/itarray/automotion/tests/geometry/ScalarTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,11 @@ public void satisfies() {
168168
assertThat(scalar.satisfies(Condition.greaterOrEqualTo(value), context, direction)).isTrue();
169169
assertThat(scalar.satisfies(Condition.greaterOrEqualTo(value+1), context, direction)).isFalse();
170170
}
171+
172+
@Test
173+
public void norm() {
174+
assertThat(scalar.norm()).isEqualTo(scalar);
175+
assertThat(scalar.negated().norm()).isEqualTo(scalar);
176+
}
171177
}
172178

src/test/java/net/itarray/automotion/tests/geometry/VectorTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class VectorTest {
1616

1717
@Before
1818
public void createVector() {
19-
x = scalar(10);
20-
y = scalar(23);
19+
x = scalar(5);
20+
y = scalar(12);
2121
vector = new Vector(x, y);
2222
}
2323

@@ -55,7 +55,7 @@ public void getYReturnsTheFirstConstructorArgument() {
5555

5656
@Test
5757
public void toStringWithUnitsAppendsTheUnitsToEachCoordinate() {
58-
assertThat(vector.toStringWithUnits("px")).isEqualTo("10px x 23px");
58+
assertThat(vector.toStringWithUnits("px")).isEqualTo("5px x 12px");
5959
}
6060

6161
@Test
@@ -69,4 +69,9 @@ public void plusReturnsAVectorWithValueEqualToTheSumOfValueAndTheAddendValueInBo
6969
Vector addend = new Vector(2, 3);
7070
assertThat(vector.plus(addend)).isEqualTo(new Vector(x.plus(2), y.plus(3)));
7171
}
72+
73+
@Test
74+
public void normReturnsTheEuklideanDistanceToTheOrigin() {
75+
assertThat(vector.norm()).isEqualTo(scalar(13));
76+
}
7277
}

0 commit comments

Comments
 (0)