Are these two pieces of code representing the same thing? Is there any performance differencies? [closed]

StackOverflow https://stackoverflow.com/questions/21355106

  •  02-10-2022
  •  | 
  •  

Domanda

This one:

return new Rectangle(
   position_.X - origin_.X,
   position_.Y - origin_.Y,
   frame_width_,
   frame_height_);

And this one:

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);

Where CalculateBoundingRectangle is:

/// <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));
        }
È stato utile?

Soluzione

Assuming code is written correctly both should give the same result.

  • First - creates bounding rectangle with hardcoded computation for given position/origin and rotation of 0.
  • Second - is generic way to compute bounding rectangle for given position/origin/rotation (rotation in the sample implied to be 0).

Second approach likely to be used when position/angle already present as transformation matrix.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top