-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
39 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace PolyZone.Shapes.Interfaces; | ||
|
||
/// <summary> | ||
/// A 2d rectangle / box | ||
/// </summary> | ||
public interface IRectangle : ISpatial2dShape; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using CitizenFX.Core; | ||
using PolyZone.Shapes.Interfaces; | ||
|
||
namespace PolyZone.Shapes; | ||
|
||
public class Rectangle(in Vector2 upperLeft, in Vector2 bottomRight) : IRectangle | ||
{ | ||
public Vector2 UpperLeft { get; } = upperLeft; | ||
public Vector2 BottomRight { get; } = bottomRight; | ||
|
||
/// <inheritdoc cref="ISpatial2dShape.Contains"/> | ||
public bool Contains(in Vector2 point) | ||
{ | ||
return point.X >= UpperLeft.X && point.X <= BottomRight.X && | ||
point.Y >= UpperLeft.Y && point.Y <= BottomRight.Y; | ||
} | ||
|
||
/// <inheritdoc cref="ISpatial2dShape.DistanceFrom"/> | ||
public float DistanceFrom(in Vector2 point) | ||
{ | ||
// Calculate the distance using Euclidean distance formula | ||
var dx = Math.Max(Math.Max(UpperLeft.X - point.X, 0), point.X - BottomRight.X); | ||
var dy = Math.Max(Math.Max(UpperLeft.Y - point.Y, 0), point.Y - BottomRight.Y); | ||
|
||
return (float) Math.Sqrt(dx * dx + dy * dy); | ||
} | ||
} |