Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. static bool IntersectPixels(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB)
  2. {
  3. // Find the bounds of the rectangle intersection
  4. int top = Math.Max(rectangleA.Top, rectangleB.Top);
  5. int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
  6. int left = Math.Max(rectangleA.Left, rectangleB.Left);
  7. int right = Math.Min(rectangleA.Right, rectangleB.Right);
  8.  
  9. // Check every point within the intersection bounds
  10. for (int y = top; y < bottom; y++)
  11. {
  12. for (int x = left; x < right; x++)
  13. {
  14. // Get the color of both pixels at this point
  15. Color colorA = dataA[(x - rectangleA.Left) +
  16. (y - rectangleA.Top) * rectangleA.Width];
  17. Color colorB = dataB[(x - rectangleB.Left) +
  18. (y - rectangleB.Top) * rectangleB.Width];
  19.  
  20. // If both pixels are not completely transparent,
  21. if (colorA.A != 0 && colorB.A != 0)
  22. {
  23. // then an intersection has been found
  24. return true;
  25. }
  26. }
  27. }
  28.  
  29. // No intersection found
  30. return false;
  31. }
  32.  
  33. bool intersected = false;
  34. intersected = IntersectPixels(rectMissile, missileShipTextureData, rectInvader, invaderTextureData);
  35. if (intersected)
  36. {
  37. explosion1Sound.Play();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement