/RecycleRobo/RecycleRobo/MiniGame/RotatedRectangle.cs
C# | 101 lines | 68 code | 17 blank | 16 comment | 9 complexity | 994b66f5422901c0d359b7be0774d0df MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- namespace RecycleRobo
- {
- public class RotatedRectangle
- {
- public static bool IntersectPixels(
- Matrix transformA, int widthA, int heightA, Color[] dataA,
- Matrix transformB, int widthB, int heightB, Color[] dataB)
- {
- // Calculate a matrix which transforms from A's local space into
- // world space and then into B's local space
- Matrix transformAToB = transformA * Matrix.Invert(transformB);
- // When a point moves in A's local space, it moves in B's local space with a
- // fixed direction and distance proportional to the movement in A.
- // This algorithm steps through A one pixel at a time along A's X and Y axes
- // Calculate the analogous steps in B
- Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB);
- Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB);
- // Calculate the top left corner of A in B's local space
- // This variable will be reused to keep track of the start of each row
- Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformAToB);
- // For each row of pixels in A
- for (int yA = 0; yA < heightA; yA++)
- {
- // Start at the beginning of the row
- Vector2 posInB = yPosInB;
- // For each pixel in this row
- for (int xA = 0; xA < widthA; xA++)
- {
- // Round to the nearest pixel
- int xB = (int)Math.Round(posInB.X);
- int yB = (int)Math.Round(posInB.Y);
- // If the pixel lies within the bounds of B
- if (0 <= xB && xB < widthB &&
- 0 <= yB && yB < heightB)
- {
- // Get the colors of the overlapping pixels
- Color colorA = dataA[xA + yA * widthA];
- Color colorB = dataB[xB + yB * widthB];
- // If both pixels are not completely transparent,
- if (colorB.A != 0 && colorA.A != 0)
- {
- // then an intersection has been found
- return true;
- }
- }
- posInB += stepX;
- }
- yPosInB += stepY;
- }
- return false;
- }
- public static Matrix GetMatrixTransfrom(Vector2 vector,Vector2 origin, float rotation, float scale)
- {
- Matrix transform =
- Matrix.CreateTranslation(new Vector3(-origin, 0.0f)) *
- Matrix.CreateRotationZ(rotation) *
- Matrix.CreateScale(scale) *
- Matrix.CreateTranslation(new Vector3(vector, 0.0f));
- return transform;
- }
- public static Rectangle CalculateBoundingRectangle(Rectangle rectangle,Matrix transform)
- {
- 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);
- 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);
- 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 new Rectangle((int)min.X, (int)min.Y,
- (int)(max.X - min.X), (int)(max.Y - min.Y));
- }
- }
- }