COTSRect.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OTSMeasureApp._0_OTSModel.OTSDataType
  8. {
  9. public class COTSRect
  10. {
  11. private float m_left;
  12. private float m_top;
  13. private float m_right;
  14. private float m_bottom;
  15. public COTSRect()
  16. {
  17. m_left = 0;
  18. m_top = 0;
  19. m_right = 0;
  20. m_bottom = 0;
  21. }
  22. public COTSRect(float left, float top, float right, float bottom)
  23. {
  24. m_left = left;
  25. m_top = top;
  26. m_right = right;
  27. m_bottom = bottom;
  28. }
  29. public COTSRect(PointF leftTop, PointF bottomRight)
  30. {
  31. m_left = leftTop.X;
  32. m_top = leftTop.Y;
  33. m_right = bottomRight.X;
  34. m_bottom = bottomRight.Y;
  35. }
  36. public void SetRectData(float left, float top, float right, float bottom)
  37. {
  38. m_left = left;
  39. m_top = top;
  40. m_right = right;
  41. m_bottom = bottom;
  42. }
  43. public bool PointInRect(PointF p)
  44. {
  45. if (p.X > m_left && p.X < m_right && p.Y > m_bottom && p.Y < m_top)
  46. {
  47. return true;
  48. }
  49. else
  50. {
  51. return false;
  52. }
  53. }
  54. public PointF GetTopLeft() { return new PointF(m_left, m_top); }
  55. public PointF GetBottomRight() { return new PointF(m_right, m_bottom); }
  56. public float GetWidth() { return (m_right - m_left); }
  57. public float GetHeight() { return (m_top - m_bottom); }
  58. public PointF GetCenterPoint() { double w = GetWidth(); double h = GetHeight(); PointF lt = GetTopLeft(); return new PointF((float)(lt.X + w / 2), (float)(lt.Y - h / 2)); }
  59. public bool IntersectOtherRect(COTSRect r)
  60. {
  61. PointF leftTop = r.GetTopLeft();
  62. PointF rightBottom = r.GetBottomRight();
  63. PointF rightTop =new PointF(rightBottom.X, leftTop.Y);
  64. PointF leftBottom =new PointF(leftTop.X, rightBottom.Y);
  65. if (PointInRect(leftTop))
  66. {
  67. return true;
  68. }
  69. else if (PointInRect(rightBottom))
  70. {
  71. return true;
  72. }
  73. else if (PointInRect(rightTop))
  74. {
  75. return true;
  76. }
  77. else if (PointInRect(leftBottom))
  78. {
  79. return true;
  80. }
  81. else if (r.PointInRect(new PointF(m_left, m_top)) || r.PointInRect(new PointF(m_right, m_bottom)))
  82. {
  83. return true;
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. }