Skip to content

Commit

Permalink
Resolve "Consider potential future ThreatRating in the defense"
Browse files Browse the repository at this point in the history
Closes #1966

See merge request main/Sumatra!1844

sumatra-commit: c19d7306b8a73e15903a8500b2261e545906e4b0
  • Loading branch information
g3force authored and TIGERs GitLab committed Jul 3, 2024
1 parent ea776ac commit 3f4930a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,12 @@ public IVector2 farthestTo(IVector2... points)
{
return VectorMath.farthestTo(this, Arrays.asList(points));
}


@Override
public Vector2 projectOntoThis(IVector2 other)
{
var normalized = normalizeNew();
return normalized.multiply(normalized.scalarProduct(other));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ default int getNumDimensions()
{
return 2;
}


/**
* Adds 'this' and 'vector' and returns the result in a new object, while 'this' and 'vector' stay unaffected.
*
* @param vector
* @return The result as a new object
*/
Vector2 addNew(IVector2 vector);


/**
* Subtracts the given 'vector' from 'this' and returns the result in a new object, while 'this' and 'vector' stay
* unaffected.
Expand All @@ -40,8 +40,8 @@ default int getNumDimensions()
* @return The result as a new object
*/
Vector2 subtractNew(IVector2 vector);


/**
* Multiplies each element of the given 'vector' with the corresponding element of 'this' and returns the result in a
* new object, while 'this' and 'vector' stay
Expand All @@ -51,88 +51,85 @@ default int getNumDimensions()
* @return The result as a new object
*/
Vector2 multiplyNew(IVector2 vector);


@Override
Vector2 multiplyNew(double f);


@Override
Vector2 normalizeNew();


@Override
Vector2 absNew();


@Override
Vector2 applyNew(Function<Double, Double> function);


/**
* @param point some point
* @return the squared distance to the point
*/
double distanceToSqr(IVector2 point);


/**
* @return a new deep copy
*/
IVector2 copy();


/**
* Scales the length of 'this' to the given 'length', and returns the result in a new object, while 'this' stays
* uneffected.<br>
*
* <pre>
* l
* ---- * v
* | v |
*
* <pre>
*
* @param newLength the new length of the vector
* @return the scaled vector
*/
Vector2 scaleToNew(double newLength);


/**
* Turns 'this' with the given 'angle' and returns the result in a new object,
* while 'this' stay unaffected. The new vector has the same length as the old one.
* The angle is added anti-clockwise.
*
*
* @param angle angle step to turn
* @return the turned angle
*/
Vector2 turnNew(double angle);


/**
* Returns a new vector with the given absolute 'angle' with respect to the x-axis.
* Input and output vector are of equal length.
*
*
* @param angle the new angle
* @return a vector with given angle and same length as current vector
* @see Vector2#turn(double) turn
*/
Vector2 turnToNew(double angle);


/**
* @return
*/
boolean isVertical();


/**
* @return
*/
boolean isHorizontal();


/**
* Calculates the angle between x-Axis and the given vector.<br>
* Angle is calculated anti-clockwise in range (-pi to pi).
Expand All @@ -144,27 +141,27 @@ default int getNumDimensions()
* @throws IllegalArgumentException if vector is a zero-vector
*/
double getAngle();


/**
* Same as {@link IVector2#getAngle()}, but if vector has zero length, return
* the given default value.
*
*
* @param defAngle the default angle value
* @return the angle of this vector
*/
double getAngle(double defAngle);


/**
* Returns the scalar product of 'this' and the given vector.
*
*
* @param v some vector
* @return this * v
*/
double scalarProduct(IVector2 v);


/**
* Returns the surface normal to the given vector.
* It is turned by 90 degree clockwise.
Expand All @@ -173,17 +170,17 @@ default int getNumDimensions()
* @return the normal vector of this vector
*/
Vector2 getNormalVector();


/**
* Check if this vector is parallel to the other giver vector.
*
*
* @param vector some other vector
* @return true, if both vectors are parallel
*/
boolean isParallelTo(IVector2 vector);


/**
* Angle difference between this toVector and the given one, so that:
* <br>
Expand All @@ -196,27 +193,27 @@ default int getNumDimensions()
* @return angle difference from this toVector to the given one in [-PI, PI]
*/
Optional<Double> angleTo(IVector2 toVector);


/**
* Absolute angle difference between this toVector and the given one.
*
* @see IVector2#angleTo(IVector2)
* @param toVector towards this toVector
* @return absolute angle difference from this toVector to the given one in [0, PI]
* @see IVector2#angleTo(IVector2)
*/
Optional<Double> angleToAbs(IVector2 toVector);


/**
* Calculate the nearest point in the given list to this vector.
*
* @param points list of points to compare
* @return nearest point in list to point p
*/
IVector2 nearestTo(Collection<IVector2> points);


/**
* Calculate the nearest point in the given list to this vector.
* The list can be empty. An empty Optional will be returned in this case.
Expand All @@ -225,26 +222,26 @@ default int getNumDimensions()
* @return nearest point in list to point p, or empty if list is empty
*/
Optional<IVector2> nearestToOpt(Collection<IVector2> points);


/**
* Calculate the nearest point in the given list to this vector.
*
* @param points list of points to compare
* @return nearest point in list to point p
*/
IVector2 nearestTo(IVector2... points);


/**
* Calculate the farthest point in the given list to this vector.
*
* @param points list of points to compare
* @return farthest point in list to point p
*/
IVector2 farthestTo(Collection<IVector2> points);


/**
* Calculate the farthest point in the given list to this vector.
* The list can be empty. An empty Optional will be returned in this case.
Expand All @@ -253,13 +250,24 @@ default int getNumDimensions()
* @return farthest point in list to point p, or empty if list is empty
*/
Optional<IVector2> farthestToOpt(Collection<IVector2> points);


/**
* Calculate the farthest point in the given list to this vector.
*
* @param points list of points to compare
* @return farthest point in list to point p
*/
IVector2 farthestTo(IVector2... points);


/**
* Project vector onto the direction of this vector
* <p>
* So the part of the other vector in the direction of this vector will be returned
*
* @param other
* @return
*/
Vector2 projectOntoThis(IVector2 other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

package edu.tigers.sumatra.math.vector;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import edu.tigers.sumatra.math.AngleMath;
import edu.tigers.sumatra.math.StatisticsMath;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.RealVector;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.RealVector;
import org.junit.Test;

import edu.tigers.sumatra.math.AngleMath;
import edu.tigers.sumatra.math.StatisticsMath;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


/**
Expand Down Expand Up @@ -669,4 +668,34 @@ public void testNearestTo()
assertThat(Vector2.zero().nearestToOpt(Collections.emptyList()).isPresent()).isFalse();
assertThat(Vector2.zero().nearestTo(Vector2.fromX(1), Vector2.fromX(2))).isEqualTo(Vector2.fromX(1));
}


@Test
public void testProjectOntoThis()
{
var nonZeroVector = Vector2.fromX(10);
var zeroVector = Vector2.zero();

var test = Vector2.fromY(2);
assertThat(nonZeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());
assertThat(zeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());

test = Vector2.fromX(2);
assertThat(nonZeroVector.projectOntoThis(test)).isEqualTo(Vector2.fromX(2));
assertThat(zeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());

test = Vector2.fromXY(2, 3);
assertThat(nonZeroVector.projectOntoThis(test)).isEqualTo(Vector2.fromX(2));
assertThat(zeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());

nonZeroVector = Vector2.fromXY(10, 10);

test = Vector2.fromXY(2, -2);
assertThat(nonZeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());
assertThat(zeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());

test = Vector2.fromXY(2, 2);
assertThat(nonZeroVector.projectOntoThis(test)).isEqualTo(Vector2.fromXY(2, 2));
assertThat(zeroVector.projectOntoThis(test)).isEqualTo(Vector2.zero());
}
}

0 comments on commit 3f4930a

Please sign in to comment.