RectangleF.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. namespace DataStructures
  17. {
  18. public struct RectangleF
  19. {
  20. public float Left;
  21. public float Top;
  22. public float Right;
  23. public float Bottom;
  24. public float Width
  25. {
  26. get
  27. {
  28. return Right - Left;
  29. }
  30. set
  31. {
  32. if(value >= 0)
  33. Right = Left + value;
  34. else
  35. Left = Right + value;
  36. }
  37. }
  38. public float Height
  39. {
  40. get
  41. {
  42. return Bottom - Top;
  43. }
  44. set
  45. {
  46. if(value >= 0)
  47. Bottom = Top + value;
  48. else
  49. Top = Bottom + value;
  50. }
  51. }
  52. public Vector Center
  53. {
  54. get
  55. {
  56. return new Vector((Left + Right) / 2.0f,
  57. (Top + Bottom) / 2.0f);
  58. }
  59. }
  60. public RectangleF(float Left, float Top, float Width, float Height)
  61. {
  62. this.Top = Top;
  63. this.Left = Left;
  64. Bottom = Top + Height;
  65. Right = Left + Width;
  66. }
  67. public RectangleF(Vector v1, Vector v2)
  68. {
  69. Top = (v1.Y < v2.Y)?v1.Y:v2.Y;
  70. Left = (v1.X < v2.X)?v1.X:v2.X;
  71. Bottom = (v1.Y > v2.Y)?v1.Y:v2.Y;
  72. Right = (v1.X > v2.X)?v1.X:v2.X;
  73. }
  74. public void Move(Vector vec)
  75. {
  76. Left += vec.X;
  77. Right += vec.X;
  78. Top += vec.Y;
  79. Bottom += vec.Y;
  80. }
  81. public void MoveTo(Vector vec)
  82. {
  83. Move(new Vector(vec.X - Left, vec.Y - Top));
  84. }
  85. public bool Contains(Vector v)
  86. {
  87. return v.X >= Left && v.X < Right && v.Y >= Top && v.Y < Bottom;
  88. }
  89. public bool Contains(RectangleF rect)
  90. {
  91. return rect.Left >= Left && rect.Right <= Right && rect.Top >= Top && rect.Bottom <= Bottom;
  92. }
  93. public static bool operator ==(RectangleF r1, RectangleF r2) {
  94. return r1.Left == r2.Left && r1.Top == r2.Top && r1.Right == r2.Right && r1.Bottom == r2.Bottom;
  95. }
  96. public static bool operator !=(RectangleF r1, RectangleF r2) {
  97. return r1.Left != r2.Left || r1.Top != r2.Top || r1.Right != r2.Right || r1.Bottom != r2.Bottom;
  98. }
  99. public static explicit operator Gdk.Rectangle(RectangleF r) {
  100. return new Gdk.Rectangle((int) r.Left, (int) r.Top, (int) r.Width, (int) r.Height);
  101. }
  102. public override bool Equals(object obj) {
  103. if (!(obj is RectangleF))
  104. return false;
  105. RectangleF rect = (RectangleF)obj;
  106. return this == rect;
  107. }
  108. public override int GetHashCode() {
  109. return base.GetHashCode();
  110. }
  111. }
  112. }
  113. /* EOF */