这一个:
return new Rectangle(
position_.X - origin_.X,
position_.Y - origin_.Y,
frame_width_,
frame_height_);
这一个:
Matrix Transformation = Matrix.CreateTranslation(new Vector3(-origin_, 0.0f)) *
Matrix.CreateTranslation(new Vector3(position_, 0.0f));
Rectangle blockRectangle = CalculateBoundingRectangle(
new Rectangle(0, 0, frame_width_, frame_height_),
Transformation);
return new Rectangle(blockRectangle.X, blockRectangle.Y, blockRectangle.Width, blockRectangle.Height);
CalculateBoundingRectangle
的位置:
/// <summary>
/// Calculates an axis aligned rectangle which fully contains an arbitrarily
/// transformed axis aligned rectangle.
/// </summary>
/// <param name="rectangle">Original bounding rectangle.</param>
/// <param name="transform">World transform of the rectangle.</param>
/// <returns>A new rectangle which contains the trasnformed rectangle.</returns>
public static Rectangle CalculateBoundingRectangle(Rectangle rectangle,
Matrix transform)
{
// Get all four corners in local space
Vector2 leftTop = new Vector2(rectangle.Left, rectangle.Top);
Vector2 rightTop = new Vector2(rectangle.Right, rectangle.Top);
Vector2 leftBottom = new Vector2(rectangle.Left, rectangle.Bottom);
Vector2 rightBottom = new Vector2(rectangle.Right, rectangle.Bottom);
// Transform all four corners into work space
Vector2.Transform(ref leftTop, ref transform, out leftTop);
Vector2.Transform(ref rightTop, ref transform, out rightTop);
Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
Vector2.Transform(ref rightBottom, ref transform, out rightBottom);
// Find the minimum and maximum extents of the rectangle in world space
Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
Vector2.Min(leftBottom, rightBottom));
Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
Vector2.Max(leftBottom, rightBottom));
// Return that as a rectangle
return new Rectangle((int)min.X, (int)min.Y,
(int)(max.X - min.X), (int)(max.Y - min.Y));
}
答案 0 :(得分:1)
假设代码写得正确,两者都应该给出相同的结果。
当位置/角度已经作为变换矩阵出现时,可能会使用第二种方法。