Geometry.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.Point = function(x, y)
  26. {
  27. this.x = x || 0;
  28. this.y = y || 0;
  29. };
  30. WebInspector.Point.fromEvent = function(event)
  31. {
  32. return new WebInspector.Point(event.pageX, event.pageY);
  33. };
  34. WebInspector.Point.fromEventInElement = function(event, element)
  35. {
  36. var wkPoint = window.webkitConvertPointFromPageToNode(element, new WebKitPoint(event.pageX, event.pageY));
  37. return new WebInspector.Point(wkPoint.x, wkPoint.y);
  38. };
  39. WebInspector.Point.prototype = {
  40. constructor: WebInspector.Point,
  41. toString : function()
  42. {
  43. return "WebInspector.Point[" + this.x + "," + this.y + "]";
  44. },
  45. copy: function()
  46. {
  47. return new WebInspector.Point(this.x, this.y);
  48. },
  49. equals: function(anotherPoint)
  50. {
  51. return (this.x === anotherPoint.x && this.y === anotherPoint.y);
  52. }
  53. };
  54. WebInspector.Size = function(width, height)
  55. {
  56. this.width = width || 0;
  57. this.height = height || 0;
  58. };
  59. WebInspector.Size.prototype = {
  60. constructor: WebInspector.Size,
  61. toString: function()
  62. {
  63. return "WebInspector.Size[" + this.width + "," + this.height + "]";
  64. },
  65. copy: function()
  66. {
  67. return new WebInspector.Size(this.width, this.height);
  68. },
  69. equals: function(anotherSize)
  70. {
  71. return (this.width === anotherSize.width && this.height === anotherSize.height);
  72. }
  73. };
  74. WebInspector.Size.ZERO_SIZE = new WebInspector.Size(0, 0);
  75. WebInspector.Rect = function(x, y, width, height)
  76. {
  77. this.origin = new WebInspector.Point(x || 0, y || 0);
  78. this.size = new WebInspector.Size(width || 0, height || 0);
  79. };
  80. WebInspector.Rect.rectFromClientRect = function(clientRect)
  81. {
  82. return new WebInspector.Rect(clientRect.left, clientRect.top, clientRect.width, clientRect.height);
  83. };
  84. WebInspector.Rect.prototype = {
  85. constructor: WebInspector.Rect,
  86. toString: function()
  87. {
  88. return "WebInspector.Rect[" + [this.origin.x, this.origin.y, this.size.width, this.size.height].join(", ") + "]";
  89. },
  90. copy: function()
  91. {
  92. return new WebInspector.Rect(this.origin.x, this.origin.y, this.size.width, this.size.height);
  93. },
  94. equals: function(anotherRect)
  95. {
  96. return (this.origin.equals(anotherRect.origin) && this.size.equals(anotherRect.size));
  97. },
  98. inset: function(insets)
  99. {
  100. return new WebInspector.Rect(
  101. this.origin.x + insets.left,
  102. this.origin.y + insets.top,
  103. this.size.width - insets.left - insets.right,
  104. this.size.height - insets.top - insets.bottom
  105. );
  106. },
  107. minX: function()
  108. {
  109. return this.origin.x;
  110. },
  111. minY: function()
  112. {
  113. return this.origin.y;
  114. },
  115. midX: function()
  116. {
  117. return this.origin.x + (this.size.width / 2);
  118. },
  119. midY: function()
  120. {
  121. return this.origin.y + (this.size.height / 2);
  122. },
  123. maxX: function()
  124. {
  125. return this.origin.x + this.size.width;
  126. },
  127. maxY: function()
  128. {
  129. return this.origin.y + this.size.height;
  130. },
  131. intersectionWithRect: function(rect)
  132. {
  133. var x1 = Math.max(this.minX(), rect.minX());
  134. var x2 = Math.min(this.maxX(), rect.maxX());
  135. if (x1 > x2)
  136. return WebInspector.Rect.ZERO_RECT;
  137. var intersection = new WebInspector.Rect;
  138. intersection.origin.x = x1;
  139. intersection.size.width = x2 - x1;
  140. var y1 = Math.max(this.minY(), rect.minY());
  141. var y2 = Math.min(this.maxY(), rect.maxY());
  142. if (y1 > y2)
  143. return WebInspector.Rect.ZERO_RECT;
  144. intersection.origin.y = y1;
  145. intersection.size.height = y2 - y1;
  146. return intersection;
  147. }
  148. };
  149. WebInspector.Rect.ZERO_RECT = new WebInspector.Rect(0, 0, 0, 0);
  150. WebInspector.EdgeInsets = function(top, right, bottom, left)
  151. {
  152. console.assert(arguments.length === 1 || arguments.length === 4);
  153. if (arguments.length === 1) {
  154. this.top = top;
  155. this.right = top;
  156. this.bottom = top;
  157. this.left = top;
  158. } else if (arguments.length === 4) {
  159. this.top = top;
  160. this.right = right;
  161. this.bottom = bottom;
  162. this.left = left;
  163. }
  164. };
  165. WebInspector.EdgeInsets.prototype = {
  166. constructor: WebInspector.EdgeInsets,
  167. equals: function(anotherInset)
  168. {
  169. return (this.top === anotherInset.top && this.right === anotherInset.right &&
  170. this.bottom === anotherInset.bottom && this.left === anotherInset.left);
  171. },
  172. copy: function()
  173. {
  174. return new WebInspector.EdgeInsets(this.top, this.right, this.bottom, this.left);
  175. }
  176. };
  177. WebInspector.RectEdge = {
  178. MIN_X : 0,
  179. MIN_Y : 1,
  180. MAX_X : 2,
  181. MAX_Y : 3
  182. };
  183. WebInspector.Quad = function(quad)
  184. {
  185. this.points = [
  186. new WebInspector.Point(quad[0], quad[1]), // top left
  187. new WebInspector.Point(quad[2], quad[3]), // top right
  188. new WebInspector.Point(quad[4], quad[5]), // bottom right
  189. new WebInspector.Point(quad[6], quad[7]) // bottom left
  190. ];
  191. this.width = Math.round(Math.sqrt(Math.pow(quad[0] - quad[2], 2) + Math.pow(quad[1] - quad[3], 2)));
  192. this.height = Math.round(Math.sqrt(Math.pow(quad[0] - quad[6], 2) + Math.pow(quad[1] - quad[7], 2)));
  193. };
  194. WebInspector.Quad.prototype = {
  195. constructor: WebInspector.Quad,
  196. toProtocol: function()
  197. {
  198. return [
  199. this.points[0].x, this.points[0].y,
  200. this.points[1].x, this.points[1].y,
  201. this.points[2].x, this.points[2].y,
  202. this.points[3].x, this.points[3].y
  203. ];
  204. }
  205. };