Rectangle.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Geometry.js
  7. */
  8. /**
  9. * Class: OpenLayers.Geometry.Rectangle
  10. * This class is *not supported*, and probably isn't what you're looking for.
  11. * Instead, most users probably want something like:
  12. * (code)
  13. * var poly = new OpenLayers.Bounds(0,0,10,10).toGeometry();
  14. * (end)
  15. * This will create a rectangular Polygon geometry.
  16. *
  17. * Inherits:
  18. * - <OpenLayers.Geometry>
  19. */
  20. OpenLayers.Geometry.Rectangle = OpenLayers.Class(OpenLayers.Geometry, {
  21. /**
  22. * Property: x
  23. * {Float}
  24. */
  25. x: null,
  26. /**
  27. * Property: y
  28. * {Float}
  29. */
  30. y: null,
  31. /**
  32. * Property: width
  33. * {Float}
  34. */
  35. width: null,
  36. /**
  37. * Property: height
  38. * {Float}
  39. */
  40. height: null,
  41. /**
  42. * Constructor: OpenLayers.Geometry.Rectangle
  43. *
  44. * Parameters:
  45. * points - {Array(<OpenLayers.Geometry.Point>)}
  46. */
  47. initialize: function(x, y, width, height) {
  48. OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
  49. this.x = x;
  50. this.y = y;
  51. this.width = width;
  52. this.height = height;
  53. },
  54. /**
  55. * Method: calculateBounds
  56. * Recalculate the bounds for the geometry.
  57. */
  58. calculateBounds: function() {
  59. this.bounds = new OpenLayers.Bounds(this.x, this.y,
  60. this.x + this.width,
  61. this.y + this.height);
  62. },
  63. /**
  64. * APIMethod: getLength
  65. *
  66. * Returns:
  67. * {Float} The length of the geometry
  68. */
  69. getLength: function() {
  70. var length = (2 * this.width) + (2 * this.height);
  71. return length;
  72. },
  73. /**
  74. * APIMethod: getArea
  75. *
  76. * Returns:
  77. * {Float} The area of the geometry
  78. */
  79. getArea: function() {
  80. var area = this.width * this.height;
  81. return area;
  82. },
  83. CLASS_NAME: "OpenLayers.Geometry.Rectangle"
  84. });