mxRectangle.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxRectangle
  7. *
  8. * Extends <mxPoint> to implement a 2-dimensional rectangle with double
  9. * precision coordinates.
  10. *
  11. * Constructor: mxRectangle
  12. *
  13. * Constructs a new rectangle for the optional parameters. If no parameters
  14. * are given then the respective default values are used.
  15. */
  16. function mxRectangle(x, y, width, height)
  17. {
  18. mxPoint.call(this, x, y);
  19. this.width = (width != null) ? width : 0;
  20. this.height = (height != null) ? height : 0;
  21. };
  22. /**
  23. * Extends mxPoint.
  24. */
  25. mxRectangle.prototype = new mxPoint();
  26. mxRectangle.prototype.constructor = mxRectangle;
  27. /**
  28. * Variable: width
  29. *
  30. * Holds the width of the rectangle. Default is 0.
  31. */
  32. mxRectangle.prototype.width = null;
  33. /**
  34. * Variable: height
  35. *
  36. * Holds the height of the rectangle. Default is 0.
  37. */
  38. mxRectangle.prototype.height = null;
  39. /**
  40. * Function: setRect
  41. *
  42. * Sets this rectangle to the specified values
  43. */
  44. mxRectangle.prototype.setRect = function(x, y, w, h)
  45. {
  46. this.x = x;
  47. this.y = y;
  48. this.width = w;
  49. this.height = h;
  50. };
  51. /**
  52. * Function: getCenterX
  53. *
  54. * Returns the x-coordinate of the center point.
  55. */
  56. mxRectangle.prototype.getCenterX = function ()
  57. {
  58. return this.x + this.width/2;
  59. };
  60. /**
  61. * Function: getCenterY
  62. *
  63. * Returns the y-coordinate of the center point.
  64. */
  65. mxRectangle.prototype.getCenterY = function ()
  66. {
  67. return this.y + this.height/2;
  68. };
  69. /**
  70. * Function: add
  71. *
  72. * Adds the given rectangle to this rectangle.
  73. */
  74. mxRectangle.prototype.add = function(rect)
  75. {
  76. if (rect != null)
  77. {
  78. var minX = Math.min(this.x, rect.x);
  79. var minY = Math.min(this.y, rect.y);
  80. var maxX = Math.max(this.x + this.width, rect.x + rect.width);
  81. var maxY = Math.max(this.y + this.height, rect.y + rect.height);
  82. this.x = minX;
  83. this.y = minY;
  84. this.width = maxX - minX;
  85. this.height = maxY - minY;
  86. }
  87. };
  88. /**
  89. * Function: intersect
  90. *
  91. * Changes this rectangle to where it overlaps with the given rectangle.
  92. */
  93. mxRectangle.prototype.intersect = function(rect)
  94. {
  95. if (rect != null)
  96. {
  97. var r1 = this.x + this.width;
  98. var r2 = rect.x + rect.width;
  99. var b1 = this.y + this.height;
  100. var b2 = rect.y + rect.height;
  101. this.x = Math.max(this.x, rect.x);
  102. this.y = Math.max(this.y, rect.y);
  103. this.width = Math.min(r1, r2) - this.x;
  104. this.height = Math.min(b1, b2) - this.y;
  105. }
  106. };
  107. /**
  108. * Function: grow
  109. *
  110. * Grows the rectangle by the given amount, that is, this method subtracts
  111. * the given amount from the x- and y-coordinates and adds twice the amount
  112. * to the width and height.
  113. */
  114. mxRectangle.prototype.grow = function(amount)
  115. {
  116. this.x -= amount;
  117. this.y -= amount;
  118. this.width += 2 * amount;
  119. this.height += 2 * amount;
  120. return this;
  121. };
  122. /**
  123. * Function: getPoint
  124. *
  125. * Returns the top, left corner as a new <mxPoint>.
  126. */
  127. mxRectangle.prototype.getPoint = function()
  128. {
  129. return new mxPoint(this.x, this.y);
  130. };
  131. /**
  132. * Function: rotate90
  133. *
  134. * Rotates this rectangle by 90 degree around its center point.
  135. */
  136. mxRectangle.prototype.rotate90 = function()
  137. {
  138. var t = (this.width - this.height) / 2;
  139. this.x += t;
  140. this.y -= t;
  141. var tmp = this.width;
  142. this.width = this.height;
  143. this.height = tmp;
  144. };
  145. /**
  146. * Function: equals
  147. *
  148. * Returns true if the given object equals this rectangle.
  149. */
  150. mxRectangle.prototype.equals = function(obj)
  151. {
  152. return obj != null && obj.x == this.x && obj.y == this.y &&
  153. obj.width == this.width && obj.height == this.height;
  154. };
  155. /**
  156. * Function: fromRectangle
  157. *
  158. * Returns a new <mxRectangle> which is a copy of the given rectangle.
  159. */
  160. mxRectangle.fromRectangle = function(rect)
  161. {
  162. return new mxRectangle(rect.x, rect.y, rect.width, rect.height);
  163. };
  164. __mxOutput.mxRectangle = typeof mxRectangle !== 'undefined' ? mxRectangle : undefined;