Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Added a class that calculates how a bounding box locks
Browse files Browse the repository at this point in the history
Issue #29
  • Loading branch information
Timo-Weike committed Apr 13, 2020
1 parent d7893c2 commit 0afec1f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Pirat/Other/NullCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Pirat.Other
{
public static class NullCheck
{
public static void ThrowIfNull<T>(object argument)
public static void ThrowIfNull<T>(T argument)
{
if (argument == null) throw new ArgumentNullException($"Argument of type {typeof(T)} is null");
}
Expand Down
87 changes: 87 additions & 0 deletions Pirat/Services/Helper/BoundingBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Pirat.Other;

namespace Pirat.Services.Helper
{
public enum BoundingBoxType
{
Normal = 0,
WrappsSouthPole = 1,
WrappsNorthPole = 2,
WrappsHorizontalNorth = 4,
WrappsHorizontalSouth = 8,
}

public class BoundingBox
{
public BoundingBox(Location center, double radius)
{
this.Center = center;
this.Radius = radius;
SouthWestCorner = center.Move(-radius, -radius);
SouthEastCorner = center.Move(radius, -radius);
NorthWestCorner = center.Move(-radius, radius);
NorthEastCorner = center.Move(radius, radius);

this.Type = DetermineBoxType();
}

public Location Center { get; }
public double Radius { get; }
public Location NorthEastCorner { get; }

public Location NorthWestCorner { get; }

public Location SouthEastCorner { get; }

public Location SouthWestCorner { get; }
public BoundingBoxType Type { get; }

/// <summary>
/// Determines how a BoundingBox behaves at the edges.
/// </summary>
/// <returns>The type of the bounding box</returns>
private BoundingBoxType DetermineBoxType()
{
var wrappingSouth
= SouthEastCorner.Longitude < SouthWestCorner.Longitude;
var wrappingNorth
= NorthEastCorner.Longitude < NorthWestCorner.Longitude;

var distanceToNorth = Location.NorthPole.Distance(Center);
var distanceToSouth = Location.SouthPole.Distance(Center);

var wrappingNorthPole = distanceToNorth < Radius;
var wrappingSouthPole = distanceToSouth < Radius;

var type = BoundingBoxType.Normal;
if (wrappingSouth)
{
type |= BoundingBoxType.WrappsHorizontalSouth;
}

if (wrappingNorth)
{
type |= BoundingBoxType.WrappsHorizontalNorth;
}

if (wrappingNorthPole)
{
type |= BoundingBoxType.WrappsNorthPole;
}

if (wrappingSouthPole)
{
type |= BoundingBoxType.WrappsSouthPole;
}

return type;
}

public bool IsInRadius(Location other)
{
NullCheck.ThrowIfNull(other);

return this.Center.Distance(other) <= this.Radius;
}
}
}

0 comments on commit 0afec1f

Please sign in to comment.