Skip to content

Commit

Permalink
BoundingBox2d 中删除与左右、上下有关的代码,修改为以中心点以及大小为基础
Browse files Browse the repository at this point in the history
  • Loading branch information
SeWZC committed Oct 22, 2024
1 parent 06d467b commit 36114f7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 38 deletions.
115 changes: 78 additions & 37 deletions DotNetCampus.Numerics.Geometry/BoundingBox2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public readonly record struct BoundingBox2D

#region 静态方法

private static BoundingBox2D CreateInternal(double minX, double minY, double maxX, double maxY)
{
return new BoundingBox2D(
new Point2D((minX + maxX) / 2, (minY + maxY) / 2),
new Size2D((maxX - minX) / 2, (maxY - minY) / 2));
}

/// <summary>
/// 创建一个 2 维边界框。
/// </summary>
Expand All @@ -31,18 +38,40 @@ public static BoundingBox2D Create(double minX, double minY, double maxX, double
throw new ArgumentException("The minimum value of the bounding box must be less than or equal to the maximum value.");
}

return new BoundingBox2D(minX, minY, maxX, maxY);
return CreateInternal(minX, minY, maxX, maxY);
}

/// <summary>
/// 通过左上角位置和大小创建 2 维边界框。
/// 通过最小坐标位置和大小创建 2 维边界框。
/// </summary>
/// <param name="location">左上角位置。</param>
/// <param name="minPoint">最小坐标位置。</param>
/// <param name="size">大小。</param>
/// <returns>创建的 2 维边界框。</returns>
public static BoundingBox2D CreateByLocationSize(Point2D location, Size2D size)
public static BoundingBox2D CreateByMinPointSize(Point2D minPoint, Size2D size)
{
return Create(location.X, location.Y, location.X + size.Width, location.Y + size.Height);
if (size.Width < 0 || size.Height < 0)
{
throw new ArgumentException("The size of the bounding box cannot be negative.");
}

var halfSize = size / 2;
return new BoundingBox2D(minPoint + new Vector2D(halfSize.Width, halfSize.Height), halfSize);
}

/// <summary>
/// 通过中心点和大小创建 2 维边界框。
/// </summary>
/// <param name="center"></param>
/// <param name="size"></param>
/// <returns></returns>
public static BoundingBox2D CreateByCenterSize(Point2D center, Size2D size)
{
if (size.Width < 0 || size.Height < 0)
{
throw new ArgumentException("The size of the bounding box cannot be negative.");
}

return new BoundingBox2D(center, size / 2);
}

/// <summary>
Expand All @@ -52,7 +81,7 @@ public static BoundingBox2D CreateByLocationSize(Point2D location, Size2D size)
/// <returns>宽高为 0 但不为空的 2 维边界框。</returns>
public static BoundingBox2D Create(Point2D point)
{
return new BoundingBox2D(point.X, point.Y, point.X, point.Y);
return new BoundingBox2D(point, new Size2D());
}

/// <summary>
Expand All @@ -63,7 +92,7 @@ public static BoundingBox2D Create(Point2D point)
/// <returns>包含两个点的最小 2 维边界框。</returns>
public static BoundingBox2D Create(Point2D point1, Point2D point2)
{
return new BoundingBox2D(
return CreateInternal(
Math.Min(point1.X, point2.X),
Math.Min(point1.Y, point2.Y),
Math.Max(point1.X, point2.X),
Expand Down Expand Up @@ -95,7 +124,7 @@ public static BoundingBox2D Create(IReadOnlyCollection<Point2D> points)
maxY = Math.Max(maxY, point.Y);
}

return new BoundingBox2D(minX, minY, maxX, maxY);
return CreateInternal(minX, minY, maxX, maxY);
}

/// <summary>
Expand All @@ -113,7 +142,7 @@ public static BoundingBox2D TryCreate(double minX, double minY, double maxX, dou
return Empty;
}

return new BoundingBox2D(minX, minY, maxX, maxY);
return CreateInternal(minX, minY, maxX, maxY);
}

#endregion
Expand All @@ -130,39 +159,64 @@ public static BoundingBox2D TryCreate(double minX, double minY, double maxX, dou
#region 属性

/// <summary>
/// 最小 X 值
/// 中心点
/// </summary>
public double MinX { get; }
public Point2D Center { get; }

/// <summary>
/// 最小 Y 值
/// 边界框的大小的一半
/// </summary>
public double MinY { get; }
public Size2D HalfSize { get; }

/// <summary>
/// 最大 X 值
/// 半宽度
/// </summary>
public double MaxX { get; }
public double HalfWidth => HalfSize.Width;

/// <summary>
/// 最大 Y 值
/// 半高度
/// </summary>
public double MaxY { get; }
public double HalfHeight => HalfSize.Height;

/// <summary>
/// 边界框是否为空。宽高为 0 的边界框不一定为空
/// 边界框的大小
/// </summary>
public bool IsEmpty => !_isNotEmpty;
public Size2D Size => new(Width, Height);

/// <summary>
/// 宽度。
/// </summary>
public double Width => MaxX - MinX;
public double Width => HalfWidth * 2;

/// <summary>
/// 高度。
/// </summary>
public double Height => MaxY - MinY;
public double Height => HalfHeight * 2;

/// <summary>
/// 最小 X 值。
/// </summary>
public double MinX => Math.Min(Center.X - HalfWidth, Center.X + HalfWidth);

/// <summary>
/// 最小 Y 值。
/// </summary>
public double MinY => Math.Min(Center.Y - HalfHeight, Center.Y + HalfHeight);

/// <summary>
/// 最大 X 值。
/// </summary>
public double MaxX => Math.Max(Center.X - HalfWidth, Center.X + HalfWidth);

/// <summary>
/// 最大 Y 值。
/// </summary>
public double MaxY => Math.Max(Center.Y - HalfHeight, Center.Y + HalfHeight);

/// <summary>
/// 边界框是否为空。宽高为 0 的边界框不一定为空。
/// </summary>
public bool IsEmpty => !_isNotEmpty;

/// <summary>
/// 边界框坐标值最小的点(MinX, MinY)。
Expand All @@ -174,11 +228,6 @@ public static BoundingBox2D TryCreate(double minX, double minY, double maxX, dou
/// </summary>
public Point2D MaxPoint => new(MaxX, MaxY);

/// <summary>
/// 中心点。
/// </summary>
public Point2D Center => new((MinX + MaxX) / 2, (MinY + MaxY) / 2);

#endregion

#region 构造函数
Expand All @@ -188,21 +237,13 @@ public static BoundingBox2D TryCreate(double minX, double minY, double maxX, dou
/// </summary>
public BoundingBox2D()
{
MinX = 0;
MinY = 0;
MaxX = 0;
MaxY = 0;

_isNotEmpty = false;
}

private BoundingBox2D(double minX, double minY, double maxX, double maxY)
private BoundingBox2D(Point2D center, Size2D halfSize)
{
MinX = minX;
MinY = minY;
MaxX = maxX;
MaxY = maxY;

Center = center;
HalfSize = halfSize;
_isNotEmpty = true;
}

Expand Down
29 changes: 28 additions & 1 deletion DotNetCampus.Numerics.Geometry/Size2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,31 @@ namespace DotNetCampus.Numerics.Geometry;
/// </summary>
/// <param name="Width">宽度。</param>
/// <param name="Height">高度。</param>
public readonly record struct Size2D(double Width, double Height);
public readonly record struct Size2D(double Width, double Height)
{
#region 运算符重载

/// <summary>
/// 将大小按指定倍数进行缩放。
/// </summary>
/// <param name="size"></param>
/// <param name="scalar">缩放倍数。大于 1 为放大,小于 1 为缩小。</param>
/// <returns></returns>
public static Size2D operator *(Size2D size, double scalar)
{
return new Size2D(size.Width * scalar, size.Height * scalar);
}

/// <summary>
/// 将大小按指定倍数进行反向缩放。
/// </summary>
/// <param name="size"></param>
/// <param name="scalar">反向缩放倍数。大于 1 为缩小,小于 1 为放大。</param>
/// <returns></returns>
public static Size2D operator /(Size2D size, double scalar)
{
return new Size2D(size.Width / scalar, size.Height / scalar);
}

#endregion
}

0 comments on commit 36114f7

Please sign in to comment.