2
0

Geom.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var Geom = {
  2. getRectCenter: function(rect) {
  3. return this.buildPoint(
  4. rect.left + rect.width / 2,
  5. rect.top + rect.height / 2
  6. );
  7. },
  8. intersectRects: function(rect0, rect1) {
  9. return this.buildRectViaEdges(
  10. Math.max(rect0.left, rect1.left),
  11. Math.max(rect0.top, rect1.top),
  12. Math.min(rect0.right, rect1.right),
  13. Math.min(rect0.bottom, rect1.bottom)
  14. );
  15. },
  16. buildRectViaDims: function(left, top, width, height) {
  17. return {
  18. left: left,
  19. top: top,
  20. width: width,
  21. height: height,
  22. right: x + width,
  23. bottom: y + height
  24. };
  25. },
  26. buildRectViaEdges: function(left, top, right, bottom) {
  27. return {
  28. left: left,
  29. top: top,
  30. width: right - left,
  31. height: bottom - top,
  32. right: right,
  33. bottom: bottom
  34. };
  35. },
  36. buildPoint: function(left, top) {
  37. return {
  38. left: left,
  39. top: top
  40. };
  41. },
  42. subtractPoints: function(point1, point0) {
  43. return this.buildPoint(
  44. point1.left - point0.left,
  45. point1.top - point0.top
  46. );
  47. },
  48. addPoints: function(point0, point1) {
  49. return this.buildPoint(
  50. point0.left + point1.left,
  51. point0.top + point1.top
  52. );
  53. },
  54. getRectTopLeft: function(rect) {
  55. return this.buildPoint(rect.left, rect.top);
  56. }
  57. };