1 /**********************************************
2 * Copyright (C) 2010 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29
30 package org.vectomatic.dom.svg;
31
32 import com.google.gwt.core.client.JavaScriptException;
33 import com.google.gwt.core.client.JavaScriptObject;
34
35 /**
36 * <p>Many of the SVG DOM interfaces refer to objects of class {@link org.vectomatic.dom.svg.OMSVGPoint}.
37 * An {@link org.vectomatic.dom.svg.OMSVGPoint} is an (x, y) coordinate pair.
38 * When used in matrix operations, an {@link org.vectomatic.dom.svg.OMSVGPoint}
39 * is treated as a vector of the form:</p> <pre>
40 * [x]
41 * [y]
42 * [1]</pre> <p>If an {@link org.vectomatic.dom.svg.OMSVGRect} object is designated
43 * as <em>read only</em>, then attempting to assign to one of its attributes
44 * will result in an exception being thrown.</p>
45 */
46 public class OMSVGPoint extends JavaScriptObject {
47 protected OMSVGPoint() {
48 }
49
50 // Implementation of the svg::SVGPoint W3C IDL interface
51 /**
52 * The x coordinate.
53 */
54 public final native float getX() /*-{
55 return this.x;
56 }-*/;
57 /**
58 * The x coordinate.
59 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised if the {@link
60 * org.vectomatic.dom.svg.OMSVGPoint} object is read only, or corresponds
61 * to a DOM attribute that is read only.
62 */
63 public final native void setX(float value) throws JavaScriptException /*-{
64 this.x = value;
65 }-*/;
66 /**
67 * The y coordinate.
68 */
69 public final native float getY() /*-{
70 return this.y;
71 }-*/;
72 /**
73 * The y coordinate.
74 * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) Raised if the {@link
75 * org.vectomatic.dom.svg.OMSVGPoint} object is read only, or corresponds
76 * to a DOM attribute that is read only.
77 */
78 public final native void setY(float value) throws JavaScriptException /*-{
79 this.y = value;
80 }-*/;
81 /**
82 * <p>Applies a 2x3 matrix transformation on this {@link org.vectomatic.dom.svg.OMSVGPoint}
83 * object and returns a new, transformed {@link org.vectomatic.dom.svg.OMSVGPoint}
84 * object:</p> <pre>newpoint = matrix thispoint</pre>
85 * @param matrix The matrix which is to be applied to this {@link org.vectomatic.dom.svg.OMSVGPoint}
86 * object.
87 * @return A new {@link org.vectomatic.dom.svg.OMSVGPoint} object.
88 */
89 public final native OMSVGPoint matrixTransform(OMSVGMatrix matrix) /*-{
90 return this.matrixTransform(matrix);
91 }-*/;
92
93 // Helper methods
94 /**
95 * Returns a textual description of the point for debugging purposes.
96 * @return a textual description of the point.
97 */
98 public final String getDescription() {
99 StringBuilder builder = new StringBuilder("[");
100 builder.append(getX());
101 builder.append(" ");
102 builder.append(getY());
103 builder.append("]");
104 return builder.toString();
105 }
106
107 /**
108 * Adds the specified point to this point. The points
109 * are treated by this method as 2D vectors.
110 * @param p the point to add.
111 * @return this point.
112 */
113 public final OMSVGPoint add(OMSVGPoint p) {
114 return add(p, this);
115 }
116 /**
117 * Adds the specified point to this point and puts the
118 * result in the specified destination point. The points
119 * are treated by this method as 2D vectors.
120 * @param p the point to add.
121 * @param destination the destination point.
122 * @return the destination point.
123 */
124 public final native OMSVGPoint add(OMSVGPoint p, OMSVGPoint destination) /*-{
125 destination.x = this.x + p.x;
126 destination.y = this.y + p.y;
127 return destination;
128 }-*/;
129 /**
130 * Substracts the specified point from this point. The points
131 * are treated by this method as 2D vectors.
132 * @param p the point to substract.
133 * @return this point.
134 */
135 public final OMSVGPoint substract(OMSVGPoint p) {
136 return substract(p, this);
137 }
138 /**
139 * Substracts the specified point to this point and puts the
140 * result in the specified destination point. The points
141 * are treated by this method as 2D vectors.
142 * @param p the point to substract.
143 * @param destination the destination point.
144 * @return the destination point.
145 */
146 public final native OMSVGPoint substract(OMSVGPoint p, OMSVGPoint destination) /*-{
147 destination.x = this.x - p.x;
148 destination.y = this.y - p.y;
149 return destination;
150 }-*/;
151 /**
152 * Scales this point by the specified factor. The point
153 * is treated by this method as a 2D vector.
154 * @param f scale factor.
155 * @return this point.
156 */
157 public final OMSVGPoint scale(float f) {
158 return scale(f, this);
159 }
160 /**
161 * Scales this point by the specified factor and puts the
162 * result in the specified destination point. The points
163 * are treated by this method as 2D vectors.
164 * @param f scale factor.
165 * @param destination the destination point.
166 * @return the destination point.
167 */
168 public final native OMSVGPoint scale(float f, OMSVGPoint destination) /*-{
169 destination.x = this.x * f;
170 destination.y = this.y * f;
171 return destination;
172 }-*/;
173 /**
174 * Copies this point to the specified destination point.
175 * @param destination the destination point.
176 * @return the destination point.
177 */
178 public final native OMSVGPoint assignTo(OMSVGPoint destination) /*-{
179 destination.x = this.x;
180 destination.y = this.y;
181 return destination;
182 }-*/;
183
184 /**
185 * Multiplies this point by the specified point. The points
186 * are treated by this method as 2D vectors.
187 * @param p the point to multiply by.
188 * @return this point.
189 */
190 public final OMSVGPoint product(OMSVGPoint p) {
191 return product(p, this);
192 }
193 /**
194 * Multiplies this point by the specified point and puts the
195 * result in the specified destination point. The points
196 * are treated by this method as 2D vectors.
197 * @param p the point to multiply by.
198 * @param destination the destination point.
199 * @return the destination point.
200 */
201 public final native OMSVGPoint product(OMSVGPoint p, OMSVGPoint destination) /*-{
202 destination.x = this.x * p.x;
203 destination.y = this.y * p.y;
204 return destination;
205 }-*/;
206 /**
207 * Returns the dot product of this point and the specified point. The points
208 * are treated by this method as 2D vectors.
209 * @param p the second factor of the dot product.
210 * @return the dot product of this point and the specified point
211 */
212 public final native float dotProduct(OMSVGPoint p) /*-{
213 return this.x * p.x + this.y * p.y;
214 }-*/;
215 /**
216 * Returns the cross product of this point and the specified point. The points
217 * are treated by this method as 2D vectors.
218 * @param p the second factor of the cross product.
219 * @return the cross product of this point and the specified point
220 */
221 public final native float crossProduct(OMSVGPoint p) /*-{
222 return this.x * p.y - this.y * p.x;
223 }-*/;
224 /**
225 * Applies the <code>Math.round()</code> to the coordinated of this point.
226 * @return this point.
227 */
228 public final OMSVGPoint round() {
229 return round(this);
230 }
231 /**
232 * Applies the <code>Math.round()</code> to the coordinated of this
233 * point and puts the result in the specified destination point.
234 * @param destination the destination point.
235 * @return the destination point.
236 */
237 public final OMSVGPoint round(OMSVGPoint destination) {
238 destination.setX(Math.round(getX()));
239 destination.setY(Math.round(getY()));
240 return destination;
241 }
242 /**
243 * Applies the <code>Math.floor()</code> to the coordinated of this point.
244 * @return this point.
245 */
246 public final OMSVGPoint floor() {
247 return floor(this);
248 }
249 /**
250 * Applies the <code>Math.floor()</code> to the coordinated of this
251 * point and puts the result in the specified destination point.
252 * @param destination the destination point.
253 * @return the destination point.
254 */
255 public final OMSVGPoint floor(OMSVGPoint destination) {
256 destination.setX((float) Math.floor(getX()));
257 destination.setY((float) Math.floor(getY()));
258 return destination;
259 }
260 /**
261 * Returns the length of this point, treating the point as a 2D vector.
262 * @return the length of this point
263 */
264 public final native float length() /*-{
265 return Math.sqrt(this.x * this.x + this.y * this.y);
266 }-*/;
267 /**
268 * Returns the squared length of this point, treating the point as a 2D vector.
269 * @return the squared length of this point
270 */
271 public final native float length2() /*-{
272 return this.x * this.x + this.y * this.y;
273 }-*/;
274 /**
275 * Returns the euclidian distance from this point to specified point.
276 * @param p the point used to measure the distance
277 * @return the euclidian distance from this point to specified point
278 */
279 public final native float distance(OMSVGPoint p) /*-{
280 return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
281 }-*/;
282 /**
283 * Returns the squared euclidian distance from this point to specified point.
284 * @param p the point used to measure the distance
285 * @return the squared euclidian distance from this point to specified point
286 */
287 public final native float distance2(OMSVGPoint p) /*-{
288 return (this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y);
289 }-*/;
290 }