IsRectangleCross.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace IntelligentControlForsx.Service
  7. {
  8. public class IsRectangleCross
  9. {
  10. //判断矩形是否相交
  11. public static bool JudgeRectangleIntersect(int RecAleftX, int RecAleftY, int RecArightX, int RecArightY,
  12. int RecBleftX, int RecBleftY, int RecBrightX, int RecBrightY)
  13. {
  14. bool isIntersect = false;
  15. try
  16. {
  17. int zx = getAbsluteValue(RecAleftX + RecArightX - RecBleftX - RecBrightX);
  18. int x = getAbsluteValue(RecAleftX - RecArightX) + getAbsluteValue(RecBleftX - RecBrightX);
  19. int zy = getAbsluteValue(RecAleftY + RecArightY - RecBleftY - RecBrightY);
  20. int y = getAbsluteValue(RecAleftY - RecArightY) + getAbsluteValue(RecBleftY - RecBrightY);
  21. if (zx <= x && zy <= y) //需要确认两矩形边线重合是否定义为相交 此处定义为相交。
  22. {
  23. isIntersect = true;
  24. }
  25. }
  26. catch (Exception ex)
  27. {
  28. string str = ex.Message;
  29. }
  30. return isIntersect;
  31. }
  32. private static int getAbsluteValue(int douValue)
  33. {
  34. if (douValue >= 0)
  35. {
  36. return douValue;
  37. }
  38. else
  39. {
  40. return douValue * -1;
  41. }
  42. }
  43. }
  44. }