Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
Added RectangleExtentions class
Browse files Browse the repository at this point in the history
  • Loading branch information
craftersmine committed Apr 19, 2019
1 parent e850a45 commit a24933c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions craftersmine.EtherEngine.Utilities/RectangleExtentions.cs
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));
}
}
}

0 comments on commit a24933c

Please sign in to comment.