Point.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Point
  10. * Point geometry class.
  11. *
  12. * Inherits from:
  13. * - <OpenLayers.Geometry>
  14. */
  15. OpenLayers.Geometry.Point = OpenLayers.Class(OpenLayers.Geometry, {
  16. /**
  17. * APIProperty: x
  18. * {float}
  19. */
  20. x: null,
  21. /**
  22. * APIProperty: y
  23. * {float}
  24. */
  25. y: null,
  26. /**
  27. * Constructor: OpenLayers.Geometry.Point
  28. * Construct a point geometry.
  29. *
  30. * Parameters:
  31. * x - {float}
  32. * y - {float}
  33. *
  34. */
  35. initialize: function(x, y) {
  36. OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
  37. this.x = parseFloat(x);
  38. this.y = parseFloat(y);
  39. },
  40. /**
  41. * APIMethod: clone
  42. *
  43. * Returns:
  44. * {<OpenLayers.Geometry.Point>} An exact clone of this OpenLayers.Geometry.Point
  45. */
  46. clone: function(obj) {
  47. if (obj == null) {
  48. obj = new OpenLayers.Geometry.Point(this.x, this.y);
  49. }
  50. // catch any randomly tagged-on properties
  51. OpenLayers.Util.applyDefaults(obj, this);
  52. return obj;
  53. },
  54. /**
  55. * Method: calculateBounds
  56. * Create a new Bounds based on the lon/lat
  57. */
  58. calculateBounds: function () {
  59. this.bounds = new OpenLayers.Bounds(this.x, this.y,
  60. this.x, this.y);
  61. },
  62. /**
  63. * APIMethod: distanceTo
  64. * Calculate the closest distance between two geometries (on the x-y plane).
  65. *
  66. * Parameters:
  67. * geometry - {<OpenLayers.Geometry>} The target geometry.
  68. * options - {Object} Optional properties for configuring the distance
  69. * calculation.
  70. *
  71. * Valid options:
  72. * details - {Boolean} Return details from the distance calculation.
  73. * Default is false.
  74. * edge - {Boolean} Calculate the distance from this geometry to the
  75. * nearest edge of the target geometry. Default is true. If true,
  76. * calling distanceTo from a geometry that is wholly contained within
  77. * the target will result in a non-zero distance. If false, whenever
  78. * geometries intersect, calling distanceTo will return 0. If false,
  79. * details cannot be returned.
  80. *
  81. * Returns:
  82. * {Number | Object} The distance between this geometry and the target.
  83. * If details is true, the return will be an object with distance,
  84. * x0, y0, x1, and x2 properties. The x0 and y0 properties represent
  85. * the coordinates of the closest point on this geometry. The x1 and y1
  86. * properties represent the coordinates of the closest point on the
  87. * target geometry.
  88. */
  89. distanceTo: function(geometry, options) {
  90. var edge = !(options && options.edge === false);
  91. var details = edge && options && options.details;
  92. var distance, x0, y0, x1, y1, result;
  93. if(geometry instanceof OpenLayers.Geometry.Point) {
  94. x0 = this.x;
  95. y0 = this.y;
  96. x1 = geometry.x;
  97. y1 = geometry.y;
  98. distance = Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
  99. result = !details ?
  100. distance : {x0: x0, y0: y0, x1: x1, y1: y1, distance: distance};
  101. } else {
  102. result = geometry.distanceTo(this, options);
  103. if(details) {
  104. // switch coord order since this geom is target
  105. result = {
  106. x0: result.x1, y0: result.y1,
  107. x1: result.x0, y1: result.y0,
  108. distance: result.distance
  109. };
  110. }
  111. }
  112. return result;
  113. },
  114. /**
  115. * APIMethod: equals
  116. * Determine whether another geometry is equivalent to this one. Geometries
  117. * are considered equivalent if all components have the same coordinates.
  118. *
  119. * Parameters:
  120. * geom - {<OpenLayers.Geometry.Point>} The geometry to test.
  121. *
  122. * Returns:
  123. * {Boolean} The supplied geometry is equivalent to this geometry.
  124. */
  125. equals: function(geom) {
  126. var equals = false;
  127. if (geom != null) {
  128. equals = ((this.x == geom.x && this.y == geom.y) ||
  129. (isNaN(this.x) && isNaN(this.y) && isNaN(geom.x) && isNaN(geom.y)));
  130. }
  131. return equals;
  132. },
  133. /**
  134. * Method: toShortString
  135. *
  136. * Returns:
  137. * {String} Shortened String representation of Point object.
  138. * (ex. <i>"5, 42"</i>)
  139. */
  140. toShortString: function() {
  141. return (this.x + ", " + this.y);
  142. },
  143. /**
  144. * APIMethod: move
  145. * Moves a geometry by the given displacement along positive x and y axes.
  146. * This modifies the position of the geometry and clears the cached
  147. * bounds.
  148. *
  149. * Parameters:
  150. * x - {Float} Distance to move geometry in positive x direction.
  151. * y - {Float} Distance to move geometry in positive y direction.
  152. */
  153. move: function(x, y) {
  154. this.x = this.x + x;
  155. this.y = this.y + y;
  156. this.clearBounds();
  157. },
  158. /**
  159. * APIMethod: rotate
  160. * Rotate a point around another.
  161. *
  162. * Parameters:
  163. * angle - {Float} Rotation angle in degrees (measured counterclockwise
  164. * from the positive x-axis)
  165. * origin - {<OpenLayers.Geometry.Point>} Center point for the rotation
  166. */
  167. rotate: function(angle, origin) {
  168. angle *= Math.PI / 180;
  169. var radius = this.distanceTo(origin);
  170. var theta = angle + Math.atan2(this.y - origin.y, this.x - origin.x);
  171. this.x = origin.x + (radius * Math.cos(theta));
  172. this.y = origin.y + (radius * Math.sin(theta));
  173. this.clearBounds();
  174. },
  175. /**
  176. * APIMethod: getCentroid
  177. *
  178. * Returns:
  179. * {<OpenLayers.Geometry.Point>} The centroid of the collection
  180. */
  181. getCentroid: function() {
  182. return new OpenLayers.Geometry.Point(this.x, this.y);
  183. },
  184. /**
  185. * APIMethod: resize
  186. * Resize a point relative to some origin. For points, this has the effect
  187. * of scaling a vector (from the origin to the point). This method is
  188. * more useful on geometry collection subclasses.
  189. *
  190. * Parameters:
  191. * scale - {Float} Ratio of the new distance from the origin to the old
  192. * distance from the origin. A scale of 2 doubles the
  193. * distance between the point and origin.
  194. * origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
  195. * ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
  196. *
  197. * Returns:
  198. * {OpenLayers.Geometry} - The current geometry.
  199. */
  200. resize: function(scale, origin, ratio) {
  201. ratio = (ratio == undefined) ? 1 : ratio;
  202. this.x = origin.x + (scale * ratio * (this.x - origin.x));
  203. this.y = origin.y + (scale * (this.y - origin.y));
  204. this.clearBounds();
  205. return this;
  206. },
  207. /**
  208. * APIMethod: intersects
  209. * Determine if the input geometry intersects this one.
  210. *
  211. * Parameters:
  212. * geometry - {<OpenLayers.Geometry>} Any type of geometry.
  213. *
  214. * Returns:
  215. * {Boolean} The input geometry intersects this one.
  216. */
  217. intersects: function(geometry) {
  218. var intersect = false;
  219. if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
  220. intersect = this.equals(geometry);
  221. } else {
  222. intersect = geometry.intersects(this);
  223. }
  224. return intersect;
  225. },
  226. /**
  227. * APIMethod: transform
  228. * Translate the x,y properties of the point from source to dest.
  229. *
  230. * Parameters:
  231. * source - {<OpenLayers.Projection>}
  232. * dest - {<OpenLayers.Projection>}
  233. *
  234. * Returns:
  235. * {<OpenLayers.Geometry>}
  236. */
  237. transform: function(source, dest) {
  238. if ((source && dest)) {
  239. OpenLayers.Projection.transform(
  240. this, source, dest);
  241. this.bounds = null;
  242. }
  243. return this;
  244. },
  245. /**
  246. * APIMethod: getVertices
  247. * Return a list of all points in this geometry.
  248. *
  249. * Parameters:
  250. * nodes - {Boolean} For lines, only return vertices that are
  251. * endpoints. If false, for lines, only vertices that are not
  252. * endpoints will be returned. If not provided, all vertices will
  253. * be returned.
  254. *
  255. * Returns:
  256. * {Array} A list of all vertices in the geometry.
  257. */
  258. getVertices: function(nodes) {
  259. return [this];
  260. },
  261. CLASS_NAME: "OpenLayers.Geometry.Point"
  262. });