1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Geometry.js
10 * Class: OpenLayers.Geometry.Point
11 * Point geometry class.
14 * - <OpenLayers.Geometry>
16 OpenLayers.Geometry.Point = OpenLayers.Class(OpenLayers.Geometry, {
31 * Constructor: OpenLayers.Geometry.Point
32 * Construct a point geometry.
39 initialize: function(x, y) {
40 OpenLayers.Geometry.prototype.initialize.apply(this, arguments);
42 this.x = parseFloat(x);
43 this.y = parseFloat(y);
50 * {<OpenLayers.Geometry.Point>} An exact clone of this OpenLayers.Geometry.Point
52 clone: function(obj) {
54 obj = new OpenLayers.Geometry.Point(this.x, this.y);
57 // catch any randomly tagged-on properties
58 OpenLayers.Util.applyDefaults(obj, this);
64 * Method: calculateBounds
65 * Create a new Bounds based on the lon/lat
67 calculateBounds: function () {
68 this.bounds = new OpenLayers.Bounds(this.x, this.y,
73 * APIMethod: distanceTo
74 * Calculate the closest distance between two geometries (on the x-y plane).
77 * geometry - {<OpenLayers.Geometry>} The target geometry.
78 * options - {Object} Optional properties for configuring the distance
82 * details - {Boolean} Return details from the distance calculation.
84 * edge - {Boolean} Calculate the distance from this geometry to the
85 * nearest edge of the target geometry. Default is true. If true,
86 * calling distanceTo from a geometry that is wholly contained within
87 * the target will result in a non-zero distance. If false, whenever
88 * geometries intersect, calling distanceTo will return 0. If false,
89 * details cannot be returned.
92 * {Number | Object} The distance between this geometry and the target.
93 * If details is true, the return will be an object with distance,
94 * x0, y0, x1, and x2 properties. The x0 and y0 properties represent
95 * the coordinates of the closest point on this geometry. The x1 and y1
96 * properties represent the coordinates of the closest point on the
99 distanceTo: function(geometry, options) {
100 var edge = !(options && options.edge === false);
101 var details = edge && options && options.details;
102 var distance, x0, y0, x1, y1, result;
103 if(geometry instanceof OpenLayers.Geometry.Point) {
108 distance = Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
110 distance : {x0: x0, y0: y0, x1: x1, y1: y1, distance: distance};
112 result = geometry.distanceTo(this, options);
114 // switch coord order since this geom is target
116 x0: result.x1, y0: result.y1,
117 x1: result.x0, y1: result.y0,
118 distance: result.distance
127 * Determine whether another geometry is equivalent to this one. Geometries
128 * are considered equivalent if all components have the same coordinates.
131 * geom - {<OpenLayers.Geometry.Point>} The geometry to test.
134 * {Boolean} The supplied geometry is equivalent to this geometry.
136 equals: function(geom) {
139 equals = ((this.x == geom.x && this.y == geom.y) ||
140 (isNaN(this.x) && isNaN(this.y) && isNaN(geom.x) && isNaN(geom.y)));
146 * Method: toShortString
149 * {String} Shortened String representation of Point object.
150 * (ex. <i>"5, 42"</i>)
152 toShortString: function() {
153 return (this.x + ", " + this.y);
158 * Moves a geometry by the given displacement along positive x and y axes.
159 * This modifies the position of the geometry and clears the cached
163 * x - {Float} Distance to move geometry in positive x direction.
164 * y - {Float} Distance to move geometry in positive y direction.
166 move: function(x, y) {
174 * Rotate a point around another.
177 * angle - {Float} Rotation angle in degrees (measured counterclockwise
178 * from the positive x-axis)
179 * origin - {<OpenLayers.Geometry.Point>} Center point for the rotation
181 rotate: function(angle, origin) {
182 angle *= Math.PI / 180;
183 var radius = this.distanceTo(origin);
184 var theta = angle + Math.atan2(this.y - origin.y, this.x - origin.x);
185 this.x = origin.x + (radius * Math.cos(theta));
186 this.y = origin.y + (radius * Math.sin(theta));
191 * APIMethod: getCentroid
194 * {<OpenLayers.Geometry.Point>} The centroid of the collection
196 getCentroid: function() {
197 return new OpenLayers.Geometry.Point(this.x, this.y);
202 * Resize a point relative to some origin. For points, this has the effect
203 * of scaling a vector (from the origin to the point). This method is
204 * more useful on geometry collection subclasses.
207 * scale - {Float} Ratio of the new distance from the origin to the old
208 * distance from the origin. A scale of 2 doubles the
209 * distance between the point and origin.
210 * origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
211 * ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
214 * {OpenLayers.Geometry} - The current geometry.
216 resize: function(scale, origin, ratio) {
217 ratio = (ratio == undefined) ? 1 : ratio;
218 this.x = origin.x + (scale * ratio * (this.x - origin.x));
219 this.y = origin.y + (scale * (this.y - origin.y));
225 * APIMethod: intersects
226 * Determine if the input geometry intersects this one.
229 * geometry - {<OpenLayers.Geometry>} Any type of geometry.
232 * {Boolean} The input geometry intersects this one.
234 intersects: function(geometry) {
235 var intersect = false;
236 if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") {
237 intersect = this.equals(geometry);
239 intersect = geometry.intersects(this);
245 * APIMethod: transform
246 * Translate the x,y properties of the point from source to dest.
249 * source - {<OpenLayers.Projection>}
250 * dest - {<OpenLayers.Projection>}
253 * {<OpenLayers.Geometry>}
255 transform: function(source, dest) {
256 if ((source && dest)) {
257 OpenLayers.Projection.transform(
265 * APIMethod: getVertices
266 * Return a list of all points in this geometry.
269 * nodes - {Boolean} For lines, only return vertices that are
270 * endpoints. If false, for lines, only vertices that are not
271 * endpoints will be returned. If not provided, all vertices will
275 * {Array} A list of all vertices in the geometry.
277 getVertices: function(nodes) {
281 CLASS_NAME: "OpenLayers.Geometry.Point"