Skip to content

Commit

Permalink
Resolve "Simplify Kick Ins Blaue"
Browse files Browse the repository at this point in the history
Closes #1549

See merge request main/Sumatra!1298

sumatra-commit: 87485b239bdd7a59840b4bbe5e106b9282fb6b5d
  • Loading branch information
g3force authored and TIGERs GitLab committed Mar 9, 2021
1 parent 6a98383 commit 360d5bb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,90 +1,86 @@
/*
* Copyright (c) 2009 - 2018, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
*/

package edu.tigers.sumatra.math.line.v2;

import java.util.Optional;

import edu.tigers.sumatra.math.vector.IVector2;
import edu.tigers.sumatra.math.vector.Vector2;

import java.util.Optional;


/**
* Implementation of the {@link IHalfLine} interface. It represents a half-line which extends indefinitely from its
* starting point. The class provide a static factory method to create new instances.
*
*
* @author Lukas Magel
*/
final class HalfLine extends AUnboundedLine implements IHalfLine
{

private HalfLine(final IVector2 supportVector, final IVector2 directionVector)
{
super(supportVector, directionVector);
}


/**
* Create a new {@link IHalfLine} instance which extends from the specified {@code supportVector} in the direction of
* {@code directionVector} indefinitely.
*
* @param supportVector
* The support vector which defines the starting point of the created half-line
* @param directionVector
* The direction vector which defines the direction in which the half-line extends
* @return
* A new {@code IHalfLine} instance
* @throws IllegalArgumentException
* If the {@code directionVector} has a length of zero. Please perform a check in your code before you
* call this method!
*
* @param supportVector The support vector which defines the starting point of the created half-line
* @param directionVector The direction vector which defines the direction in which the half-line extends
* @return A new {@code IHalfLine} instance
* @throws IllegalArgumentException If the {@code directionVector} has a length of zero. Please perform a check in your code before you
* call this method!
*/
public static IHalfLine fromDirection(final IVector2 supportVector, final IVector2 directionVector)
{
IVector2 sV = Vector2.copy(supportVector);
IVector2 dV = directionVector.normalizeNew();
return createNewWithoutCopy(sV, dV);
}


/**
* Create a new instance without copying the vector parameters. Only use this method if you're sure
* that the two vector parameters will not be modified through side-effects.
*
* @param supportVector The support vector of the new half-line instance
*
* @param supportVector The support vector of the new half-line instance
* @param directionVector The direction vector of the new half-line instance
* @return A new half-line instance which uses the two vector parameters
*/
static IHalfLine createNewWithoutCopy(final IVector2 supportVector, final IVector2 directionVector)
{
return new HalfLine(supportVector, directionVector);
}


@Override
public boolean equals(final Object other)
{
if (this == other)
return true;
if (other == null || !(other instanceof IHalfLine))
if (!(other instanceof IHalfLine))
return false;

final IHalfLine that = (IHalfLine) other;

return this.supportVector().equals(that.supportVector())
&& this.directionVector().equals(that.directionVector());
}


@Override
public int hashCode()
{
int result = supportVector().hashCode();
result = 31 * result + directionVector().hashCode();
return result;
}


@Override
public IVector2 closestPointOnLine(final IVector2 point)
{
Expand All @@ -94,31 +90,31 @@ public IVector2 closestPointOnLine(final IVector2 point)
}
return LineMath.closestPointOnHalfLine(this, point);
}


@Override
public ILine toLine()
{
return Line.createNewWithoutCopy(supportVector(), directionVector());
}


@Override
public IHalfLine copy()
{
IVector2 supportVector = supportVector().copy();
IVector2 directionVector = directionVector().copy();
return new HalfLine(supportVector, directionVector);
}


@Override
public Optional<IVector2> intersectLine(final ILine line)
{
return line.intersectHalfLine(this);
}


@Override
public Optional<IVector2> intersectHalfLine(final IHalfLine other)
{
Expand All @@ -128,8 +124,8 @@ public Optional<IVector2> intersectHalfLine(final IHalfLine other)
}
return Optional.empty();
}


@Override
public Optional<IVector2> intersectSegment(final ILineSegment segment)
{
Expand All @@ -139,15 +135,22 @@ public Optional<IVector2> intersectSegment(final ILineSegment segment)
}
return Optional.empty();
}


@Override
public boolean isPointInFront(final IVector2 point)
{
return isValid() && LineMath.isPointInFront(this, point);
}




@Override
public ILineSegment toLineSegment(double length)
{
return LineSegment.fromOffset(supportVector(), directionVector().scaleToNew(length));
}


@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2018, DHBW Mannheim - TIGERs Mannheim
* Copyright (c) 2009 - 2020, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.sumatra.math.line.v2;

Expand Down Expand Up @@ -29,4 +29,12 @@ public interface IHalfLine extends ILineBase
* direction of this half-line.
*/
boolean isPointInFront(IVector2 point);

/**
* Returns a new line segment using the same support vector of the half line with a specified length
*
* @param length of the new line segment
* @return
*/
ILineSegment toLineSegment(double length);
}

0 comments on commit 360d5bb

Please sign in to comment.