Files
Pilz/Pilz.Drawing/HelpfulDrawingFunctions.cs
2020-09-24 11:21:53 +02:00

47 lines
1.9 KiB
C#

using System.Collections.Generic;
using System.Data;
using global::System.Drawing;
using System.Linq;
namespace Pilz.Drawing
{
public static class HelpfulDrawingFunctions
{
public static bool IsPointInRectangle(PointF p, RectangleF rect)
{
var bList = new List<bool>();
bList.Add(p.X > rect.Left);
bList.Add(p.X < rect.Right);
bList.Add(p.Y > rect.Top);
bList.Add(p.Y < rect.Bottom);
return !bList.Contains(false);
}
public static bool OverlapsTwoRectangles(RectangleF a, RectangleF b)
{
return a.IntersectsWith(b); // RectangleF.Intersect(a, b) <> RectangleF.Empty
}
public static bool RectangleContainsRectangle(RectangleF parent, RectangleF child)
{
return parent.Contains(child);
// Return _
// IsPointInRectangle(New PointF(child.Top, child.Left), parent) AndAlso
// IsPointInRectangle(New PointF(child.Top, child.Right), parent) AndAlso
// IsPointInRectangle(New PointF(child.Bottom, child.Left), parent) AndAlso
// IsPointInRectangle(New PointF(child.Bottom, child.Right), parent)
}
public static RectangleF GetRectangle(PointF p1, PointF p2)
{
var rect = new RectangleF();
bool startIsEnd = p1.X > p2.X && p1.Y > p2.Y;
var xValues = new int[] { (int)p1.X, (int)p2.X };
var yValues = new int[] { (int)p1.Y, (int)p2.Y };
rect.Location = new PointF(xValues.OrderBy(n => n).First(), yValues.OrderBy(n => n).First());
rect.Size = new SizeF(xValues.OrderByDescending(n => n).First(), yValues.OrderByDescending(n => n).First());
rect.Size = new SizeF(rect.Size.Width - rect.Location.X, rect.Size.Height - rect.Location.Y);
return rect;
}
}
}