2013-04-02, 14:44
#1
Behöver lite algoritm hjälp. Ska översätta en metod från C#, dock fastnar jag på ett ställe. Här är hela metoden:
Skapandet av variablerna colorA och colorB förvirrar mig. I mitt spel lagras färgerna i en 2d array: int[bredd][höjd]. Hur löser jag detta?
Kod:
static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB) { // Find the bounds of the rectangle intersection int top = Math.Max(rectangleA.Top, rectangleB.Top); int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom); int left = Math.Max(rectangleA.Left, rectangleB.Left); int right = Math.Min(rectangleA.Right, rectangleB.Right); // Check every point within the intersection bounds for (int y = top; y < bottom; y++) { for (int x = left; x < right; x++) { // Get the color of both pixels at this point Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width]; Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width]; // If both pixels are not completely transparent, if (colorA.A != 0 && colorB.A != 0) { // then an intersection has been found return true; } } } // No intersection found return false; }
Skapandet av variablerna colorA och colorB förvirrar mig. I mitt spel lagras färgerna i en 2d array: int[bredd][höjd]. Hur löser jag detta?