This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
e850a45
commit a24933c
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
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,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace craftersmine.EtherEngine.Utilities | ||
{ | ||
public static class RectangleExtentions | ||
{ | ||
public static bool IsLineIntersects(this Rectangle r, int x1, int y1, int x2, int y2) | ||
{ | ||
return IsLineIntersects(r, new Point(x1, y1), new Point(x2, y2)); | ||
} | ||
|
||
public static bool IsLineIntersects(this Rectangle r, Point p1, Point p2) | ||
{ | ||
if (r.Left > p1.X || r.Right < p2.X) | ||
{ | ||
return false; | ||
} | ||
|
||
if (r.Top < p1.Y || r.Bottom > p2.Y) | ||
{ | ||
return false; | ||
} | ||
|
||
var yAtRectLeft = CalculateYForX(r.Left, p1, p2); | ||
var yAtRectRight = CalculateYForX(r.Right, p1, p2); | ||
|
||
if (r.Bottom > yAtRectLeft && r.Bottom > yAtRectRight) | ||
{ | ||
return false; | ||
} | ||
|
||
if (r.Top < yAtRectLeft && r.Top < yAtRectRight) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static float CalculateYForX(float x, Point p1, Point p2) | ||
{ | ||
return p1.Y - (x - p1.X) * ((p1.Y - p2.Y) / (p2.X - p1.X)); | ||
} | ||
} | ||
} |