]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/BaseTypes/Pixel.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / BaseTypes / Pixel.js
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. */
4
5 /**
6  * @requires OpenLayers/Console.js
7  */
8
9 /**
10  * Class: OpenLayers.Pixel
11  * This class represents a screen coordinate, in x and y coordinates
12  */
13 OpenLayers.Pixel = OpenLayers.Class({
14     
15     /**
16      * APIProperty: x
17      * {Number} The x coordinate
18      */
19     x: 0.0,
20
21     /**
22      * APIProperty: y
23      * {Number} The y coordinate
24      */
25     y: 0.0,
26     
27     /**
28      * Constructor: OpenLayers.Pixel
29      * Create a new OpenLayers.Pixel instance
30      *
31      * Parameters:
32      * x - {Number} The x coordinate
33      * y - {Number} The y coordinate
34      *
35      * Returns:
36      * An instance of OpenLayers.Pixel
37      */
38     initialize: function(x, y) {
39         this.x = parseFloat(x);
40         this.y = parseFloat(y);
41     },
42     
43     /**
44      * Method: toString
45      * Cast this object into a string
46      *
47      * Returns:
48      * {String} The string representation of Pixel. ex: "x=200.4,y=242.2"
49      */
50     toString:function() {
51         return ("x=" + this.x + ",y=" + this.y);
52     },
53
54     /**
55      * APIMethod: clone
56      * Return a clone of this pixel object
57      *
58      * Returns:
59      * {<OpenLayers.Pixel>} A clone pixel
60      */
61     clone:function() {
62         return new OpenLayers.Pixel(this.x, this.y); 
63     },
64     
65     /**
66      * APIMethod: equals
67      * Determine whether one pixel is equivalent to another
68      *
69      * Parameters:
70      * px - {<OpenLayers.Pixel>}
71      *
72      * Returns:
73      * {Boolean} The point passed in as parameter is equal to this. Note that
74      * if px passed in is null, returns false.
75      */
76     equals:function(px) {
77         var equals = false;
78         if (px != null) {
79             equals = ((this.x == px.x && this.y == px.y) ||
80                       (isNaN(this.x) && isNaN(this.y) && isNaN(px.x) && isNaN(px.y)));
81         }
82         return equals;
83     },
84
85     /**
86      * APIMethod: add
87      *
88      * Parameters:
89      * x - {Integer}
90      * y - {Integer}
91      *
92      * Returns:
93      * {<OpenLayers.Pixel>} A new Pixel with this pixel's x&y augmented by the 
94      * values passed in.
95      */
96     add:function(x, y) {
97         if ( (x == null) || (y == null) ) {
98             var msg = OpenLayers.i18n("pixelAddError");
99             OpenLayers.Console.error(msg);
100             return null;
101         }
102         return new OpenLayers.Pixel(this.x + x, this.y + y);
103     },
104
105     /**
106     * APIMethod: offset
107     * 
108     * Parameters
109     * px - {<OpenLayers.Pixel>}
110     * 
111     * Returns:
112     * {<OpenLayers.Pixel>} A new Pixel with this pixel's x&y augmented by the 
113     *                      x&y values of the pixel passed in.
114     */
115     offset:function(px) {
116         var newPx = this.clone();
117         if (px) {
118             newPx = this.add(px.x, px.y);
119         }
120         return newPx;
121     },
122
123     CLASS_NAME: "OpenLayers.Pixel"
124 });