inz-00/Assets/Common/Bounding.cs
2019-09-07 15:04:27 +02:00

44 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
namespace Assets.Commo
{
public class Bounding
{
public readonly Point Min;
public readonly Point Max;
public double Width => Max.x - Min.x;
public double Height => Max.y - Min.y;
public Bounding(double x1, double y1, double x2, double y2)
{
if (x1 > x2) (x1, x2) = (x2, x1);
if (y1 > y2) (y1, y2) = (y2, y1);
Min = new Point(x1, y1);
Max = new Point(x2, y2);
}
}
public static class BoundingTools
{
public static Bounding GetBounding(IEnumerable<Point> points)
{
var start = points.First();
double xmin = start.x, xmax = start.x, ymin = start.y, ymax = start.y;
foreach (var point in points.Skip(1))
{
xmin = Math.Min(xmin, point.x);
ymin = Math.Min(ymin, point.y);
xmax = Math.Max(xmax, point.x);
ymax = Math.Max(ymax, point.y);
}
return new Bounding(xmin, ymin, xmax, ymax);
}
}
}