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