SRect.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SRect = void 0;
  4. var SPoint_1 = require("./SPoint");
  5. var SSize_1 = require("./SSize");
  6. var SRect = (function () {
  7. function SRect(x, y, width, height) {
  8. if (x == undefined) {
  9. this.leftTop = new SPoint_1.SPoint(0, 0);
  10. this.size = new SSize_1.SSize(0, 0);
  11. }
  12. else if (x instanceof SPoint_1.SPoint && y instanceof SPoint_1.SPoint) {
  13. this.leftTop = new SPoint_1.SPoint(x.x, x.y);
  14. this.size = new SSize_1.SSize(y.x - x.x, y.y - x.y);
  15. }
  16. else if (x instanceof SPoint_1.SPoint && y instanceof SSize_1.SSize) {
  17. this.leftTop = new SPoint_1.SPoint(x.x, x.y);
  18. this.size = new SSize_1.SSize(y.width, y.height);
  19. }
  20. else {
  21. this.leftTop = new SPoint_1.SPoint(x, y);
  22. this.size = new SSize_1.SSize(width, height);
  23. }
  24. }
  25. Object.defineProperty(SRect.prototype, "x", {
  26. get: function () {
  27. return this.leftTop.x;
  28. },
  29. set: function (value) {
  30. this.leftTop.x = value;
  31. },
  32. enumerable: false,
  33. configurable: true
  34. });
  35. Object.defineProperty(SRect.prototype, "y", {
  36. get: function () {
  37. return this.leftTop.y;
  38. },
  39. set: function (value) {
  40. this.leftTop.y = value;
  41. },
  42. enumerable: false,
  43. configurable: true
  44. });
  45. Object.defineProperty(SRect.prototype, "width", {
  46. get: function () {
  47. return this.size.width;
  48. },
  49. set: function (v) {
  50. this.size.width = v;
  51. },
  52. enumerable: false,
  53. configurable: true
  54. });
  55. Object.defineProperty(SRect.prototype, "height", {
  56. get: function () {
  57. return this.size.height;
  58. },
  59. set: function (v) {
  60. this.size.height = v;
  61. },
  62. enumerable: false,
  63. configurable: true
  64. });
  65. Object.defineProperty(SRect.prototype, "left", {
  66. get: function () {
  67. return this.leftTop.x;
  68. },
  69. set: function (v) {
  70. this.leftTop.x = v;
  71. },
  72. enumerable: false,
  73. configurable: true
  74. });
  75. Object.defineProperty(SRect.prototype, "top", {
  76. get: function () {
  77. return this.y;
  78. },
  79. set: function (v) {
  80. this.y = v;
  81. },
  82. enumerable: false,
  83. configurable: true
  84. });
  85. Object.defineProperty(SRect.prototype, "right", {
  86. get: function () {
  87. return this.x + this.width;
  88. },
  89. set: function (right) {
  90. this.width = right - this.x;
  91. },
  92. enumerable: false,
  93. configurable: true
  94. });
  95. Object.defineProperty(SRect.prototype, "bottom", {
  96. get: function () {
  97. return this.y + this.height;
  98. },
  99. set: function (value) {
  100. this.height = value - this.y;
  101. },
  102. enumerable: false,
  103. configurable: true
  104. });
  105. SRect.prototype.isEmpty = function () {
  106. return this.size.isEmpty();
  107. };
  108. SRect.prototype.isNull = function () {
  109. return this.size.isNull();
  110. };
  111. SRect.prototype.isValid = function () {
  112. return this.size.isValid();
  113. };
  114. SRect.prototype.isIn = function (rect) {
  115. return (this.left <= rect.left &&
  116. this.right >= rect.right &&
  117. this.bottom >= rect.bottom &&
  118. this.top <= rect.top);
  119. };
  120. SRect.prototype.contains = function (x, y) {
  121. return (x >= this.left &&
  122. x <= this.right &&
  123. y >= this.top &&
  124. y <= this.bottom);
  125. };
  126. SRect.prototype.center = function () {
  127. return new SPoint_1.SPoint(this.x + this.width / 2.0, this.y + this.height / 2.0);
  128. };
  129. SRect.prototype.translate = function (dx, dy) {
  130. this.x += dx;
  131. this.y += dy;
  132. };
  133. SRect.prototype.translated = function (dx, dy) {
  134. return new SRect(this.x + dx, this.y + dy, this.width, this.height);
  135. };
  136. SRect.prototype.adjust = function (dx, dy, dw, dh) {
  137. this.x += dx;
  138. this.y += dy;
  139. this.width += dw;
  140. this.height += dh;
  141. };
  142. SRect.prototype.adjusted = function (dx, dy, dw, dh) {
  143. return new SRect(this.x + dx, this.y + dy, this.width + dw, this.height + dh);
  144. };
  145. SRect.prototype.union = function (rect) {
  146. var r = this.unioned(rect);
  147. this.x = r.x;
  148. this.y = r.y;
  149. this.width = r.width;
  150. this.height = r.height;
  151. };
  152. SRect.prototype.unioned = function (rect) {
  153. var left = Math.min(this.left, rect.left);
  154. var top = Math.min(this.top, rect.top);
  155. var right = Math.max(this.right, rect.right);
  156. var bottom = Math.max(this.bottom, rect.bottom);
  157. return new SRect(left, top, right - left, bottom - top);
  158. };
  159. SRect.prototype.intersected = function (rect) {
  160. var minX = this.left < rect.left ? this.left : rect.left;
  161. var minY = this.top < rect.top ? this.top : rect.top;
  162. var maxX = this.right > rect.right ? this.right : rect.right;
  163. var maxY = this.bottom > rect.bottom ? this.bottom : rect.bottom;
  164. if (this.width + rect.width > maxX - minX &&
  165. this.height + rect.height > maxY - minY) {
  166. return new SRect();
  167. }
  168. return new SRect();
  169. };
  170. SRect.prototype.intersect = function (rect) {
  171. var left = Math.min(this.left, rect.left);
  172. var top = Math.min(this.top, rect.top);
  173. var right = Math.max(this.right, rect.right);
  174. var bottom = Math.max(this.bottom, rect.bottom);
  175. return new SRect(left, top, right - left, bottom - top);
  176. };
  177. return SRect;
  178. }());
  179. exports.SRect = SRect;