Last active
November 2, 2018 21:17
-
-
Save WennderSantos/5430b741669737a8c5a2ca5455a5af5b to your computer and use it in GitHub Desktop.
Check if a position is inside the bounds of a rectangle. O(1)
This file contains hidden or 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
public class Rectangle | |
{ | |
public Rectangle(int width, int heigth, (int x, int y) lowestPosition) | |
{ | |
Width = width; | |
Heigth = heigth; | |
LowestPosition = lowestPosition; | |
} | |
public int Width { get; } | |
public int Heigth { get; } | |
public (int x, int y) LowestPosition { get; } | |
public bool IsThisAValidPosition((int x, int y) position) => | |
position.x >= LowestPosition.x && | |
position.x <= LowestPosition.x + Width && | |
position.y >= LowestPosition.y && | |
position.y <= LowestPosition.y + Heigth; | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var rec = new Rectangle(3, 3, (1, 1)); | |
var position1 = (4, 4); | |
var position2 = (0, 0); | |
var position3 = (1, 1); | |
var position4 = (2, 3); | |
var position5 = (3, 5); | |
Console.WriteLine(rec.IsThisAValidPosition(position1)); //True | |
Console.WriteLine(rec.IsThisAValidPosition(position2)); //False | |
Console.WriteLine(rec.IsThisAValidPosition(position3)); //True | |
Console.WriteLine(rec.IsThisAValidPosition(position4)); //True | |
Console.WriteLine(rec.IsThisAValidPosition(position5)); //False | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment