1
1

rect.d 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Written in the D programming language.
  2. /**
  3. Copyright: Copyright Felix 'Zoadian' Hufnagel 2014-.
  4. License: $(WEB http://www.gnu.org/licenses/lgpl.html, LGPLv3).
  5. Authors: $(WEB zoadian.de, Felix 'Zoadian' Hufnagel)
  6. */
  7. module three.primitives.rect;
  8. import three.primitives.point;
  9. import std.traits;
  10. /**
  11. +--------> x
  12. |
  13. |
  14. |
  15. |
  16. v
  17. y
  18. */
  19. struct Rect2(T) if(isNumeric!T) {
  20. private:
  21. T[4] data; //l,t,r,b
  22. public:
  23. ///
  24. T x() const @safe nothrow @property {
  25. return this.left;
  26. }
  27. ///
  28. T y() const @safe nothrow @property {
  29. return this.top;
  30. }
  31. public:
  32. ///
  33. T left() const @safe nothrow @property {
  34. return data[0];
  35. }
  36. ///
  37. T top() const @safe nothrow @property {
  38. return data[1];
  39. }
  40. ///
  41. T right() const @safe nothrow @property {
  42. return data[2];
  43. }
  44. ///
  45. T bottom() const @safe nothrow @property {
  46. return data[3];
  47. }
  48. public:
  49. ///
  50. T width() const @safe nothrow @property {
  51. return this.right - this.left;
  52. }
  53. ///
  54. T height() const @safe nothrow @property {
  55. return this.bottom - this.top;
  56. }
  57. public:
  58. ///
  59. Point!(2,T) center() const @safe nothrow @property {
  60. return Point!(2,T)(this.x + this.width / 2, this.y + this.height / 2);
  61. }
  62. ///
  63. Point!(2,T) leftTop() const @safe nothrow @property {
  64. return Point!(2,T)(this.left, this.top);
  65. }
  66. ///
  67. Point!(2,T) rightTop() const @safe nothrow @property {
  68. return Point!(2,T)(this.right, this.top);
  69. }
  70. ///
  71. Point!(2,T) rightBottom() const @safe nothrow @property {
  72. return Point!(2,T)(this.right, this.bottom);
  73. }
  74. ///
  75. Point!(2,T) leftBottom() const @safe nothrow @property {
  76. return Point!(2,T)(this.left, this.bottom);
  77. }
  78. }
  79. ///
  80. struct RectOffset2(T) if(isNumeric!T) {
  81. private:
  82. T[4] data; //l,t,r,b
  83. public:
  84. ///
  85. T left() const @safe nothrow @property {
  86. return data[0];
  87. }
  88. ///
  89. T top() const @safe nothrow @property {
  90. return data[1];
  91. }
  92. ///
  93. T right() const @safe nothrow @property {
  94. return data[2];
  95. }
  96. ///
  97. T bottom() const @safe nothrow @property {
  98. return data[3];
  99. }
  100. public:
  101. /// equal to left + right
  102. T horizontal() const @safe nothrow @property {
  103. return this.left + this.right;
  104. }
  105. /// equal to top + bottom
  106. T vertical() const @safe nothrow @property {
  107. return this.top + this.bottom;
  108. }
  109. }