Skip to content

Commit

Permalink
Merge pull request #5 from dotnet-campus/t/wzc/remove_left_top
Browse files Browse the repository at this point in the history
BoundingBox2d 中删除与左右、上下有关的代码
  • Loading branch information
walterlv authored Nov 5, 2024
2 parents 06d467b + 38226b2 commit f8fc62f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
42 changes: 32 additions & 10 deletions DotNetCampus.Numerics.Geometry/BoundingBox2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,36 @@ public static BoundingBox2D Create(double minX, double minY, double maxX, double
}

/// <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.");
}

return Create(minPoint.X, minPoint.Y, minPoint.X + size.Width, minPoint.Y + size.Height);
}

/// <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.");
}

var halfSize = size / 2;
return new BoundingBox2D(center.X - halfSize.Width, center.Y - halfSize.Height, center.X + halfSize.Width, center.Y + halfSize.Height);
}

/// <summary>
Expand Down Expand Up @@ -164,6 +186,11 @@ public static BoundingBox2D TryCreate(double minX, double minY, double maxX, dou
/// </summary>
public double Height => MaxY - MinY;

/// <summary>
/// 边界框的大小。
/// </summary>
public Size2D Size => new(Width, Height);

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

_isNotEmpty = false;
}

Expand Down Expand Up @@ -323,4 +345,4 @@ public BoundingBox2D Intersect(BoundingBox2D other)
}

#endregion
}
}
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 f8fc62f

Please sign in to comment.