1 | /*
|
---|
2 | * MoveTo.java
|
---|
3 | *
|
---|
4 | *
|
---|
5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
---|
6 | * Copyright (C) 2004 Mark McKay
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
23 | * projects can be found at http://www.kitfox.com
|
---|
24 | *
|
---|
25 | * Created on January 26, 2004, 8:40 PM
|
---|
26 | */
|
---|
27 |
|
---|
28 | package com.kitfox.svg.pathcmd;
|
---|
29 |
|
---|
30 | //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
|
---|
31 | import java.awt.*;
|
---|
32 | import java.awt.geom.*;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This is a little used SVG function, as most editors will save curves as
|
---|
36 | * Beziers. To reduce the need to rely on the Batik library, this functionallity
|
---|
37 | * is being bypassed for the time being. In the future, it would be nice to
|
---|
38 | * extend the GeneralPath command to include the arcTo ability provided by Batik.
|
---|
39 | *
|
---|
40 | * @author Mark McKay
|
---|
41 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
42 | */
|
---|
43 | public class Arc extends PathCommand
|
---|
44 | {
|
---|
45 |
|
---|
46 | public float rx = 0f;
|
---|
47 | public float ry = 0f;
|
---|
48 | public float xAxisRot = 0f;
|
---|
49 | public boolean largeArc = false;
|
---|
50 | public boolean sweep = false;
|
---|
51 | public float x = 0f;
|
---|
52 | public float y = 0f;
|
---|
53 |
|
---|
54 | /** Creates a new instance of MoveTo */
|
---|
55 | public Arc() {
|
---|
56 | }
|
---|
57 |
|
---|
58 | public Arc(boolean isRelative, float rx, float ry, float xAxisRot, boolean largeArc, boolean sweep, float x, float y) {
|
---|
59 | super(isRelative);
|
---|
60 | this.rx = rx;
|
---|
61 | this.ry = ry;
|
---|
62 | this.xAxisRot = xAxisRot;
|
---|
63 | this.largeArc = largeArc;
|
---|
64 | this.sweep = sweep;
|
---|
65 | this.x = x;
|
---|
66 | this.y = y;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // public void appendPath(ExtendedGeneralPath path, BuildHistory hist)
|
---|
70 | public void appendPath(GeneralPath path, BuildHistory hist)
|
---|
71 | {
|
---|
72 | float offx = isRelative ? hist.history[0].x : 0f;
|
---|
73 | float offy = isRelative ? hist.history[0].y : 0f;
|
---|
74 |
|
---|
75 | arcTo(path, rx, ry, xAxisRot, largeArc, sweep, x + offx, y + offy, hist.history[0].x, hist.history[0].y);
|
---|
76 | // path.lineTo(x + offx, y + offy);
|
---|
77 | hist.setPoint(x + offx, y + offy);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int getNumKnotsAdded()
|
---|
81 | {
|
---|
82 | return 6;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Adds an elliptical arc, defined by two radii, an angle from the
|
---|
87 | * x-axis, a flag to choose the large arc or not, a flag to
|
---|
88 | * indicate if we increase or decrease the angles and the final
|
---|
89 | * point of the arc.
|
---|
90 | *
|
---|
91 | * @param rx the x radius of the ellipse
|
---|
92 | * @param ry the y radius of the ellipse
|
---|
93 | *
|
---|
94 | * @param angle the angle from the x-axis of the current
|
---|
95 | * coordinate system to the x-axis of the ellipse in degrees.
|
---|
96 | *
|
---|
97 | * @param largeArcFlag the large arc flag. If true the arc
|
---|
98 | * spanning less than or equal to 180 degrees is chosen, otherwise
|
---|
99 | * the arc spanning greater than 180 degrees is chosen
|
---|
100 | *
|
---|
101 | * @param sweepFlag the sweep flag. If true the line joining
|
---|
102 | * center to arc sweeps through decreasing angles otherwise it
|
---|
103 | * sweeps through increasing angles
|
---|
104 | *
|
---|
105 | * @param x the absolute x coordinate of the final point of the arc.
|
---|
106 | * @param y the absolute y coordinate of the final point of the arc.
|
---|
107 | * @param x0 - The absolute x coordinate of the initial point of the arc.
|
---|
108 | * @param y0 - The absolute y coordinate of the initial point of the arc.
|
---|
109 | */
|
---|
110 | public void arcTo(GeneralPath path, float rx, float ry,
|
---|
111 | float angle,
|
---|
112 | boolean largeArcFlag,
|
---|
113 | boolean sweepFlag,
|
---|
114 | float x, float y, float x0, float y0)
|
---|
115 | {
|
---|
116 |
|
---|
117 | // Ensure radii are valid
|
---|
118 | if (rx == 0 || ry == 0) {
|
---|
119 | path.lineTo((float) x, (float) y);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (x0 == x && y0 == y) {
|
---|
124 | // If the endpoints (x, y) and (x0, y0) are identical, then this
|
---|
125 | // is equivalent to omitting the elliptical arc segment entirely.
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | Arc2D arc = computeArc(x0, y0, rx, ry, angle,
|
---|
130 | largeArcFlag, sweepFlag, x, y);
|
---|
131 | if (arc == null) return;
|
---|
132 |
|
---|
133 | AffineTransform t = AffineTransform.getRotateInstance
|
---|
134 | (Math.toRadians(angle), arc.getCenterX(), arc.getCenterY());
|
---|
135 | Shape s = t.createTransformedShape(arc);
|
---|
136 | path.append(s, true);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * This constructs an unrotated Arc2D from the SVG specification of an
|
---|
142 | * Elliptical arc. To get the final arc you need to apply a rotation
|
---|
143 | * transform such as:
|
---|
144 | *
|
---|
145 | * AffineTransform.getRotateInstance
|
---|
146 | * (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2);
|
---|
147 | */
|
---|
148 | public static Arc2D computeArc(double x0, double y0,
|
---|
149 | double rx, double ry,
|
---|
150 | double angle,
|
---|
151 | boolean largeArcFlag,
|
---|
152 | boolean sweepFlag,
|
---|
153 | double x, double y) {
|
---|
154 | //
|
---|
155 | // Elliptical arc implementation based on the SVG specification notes
|
---|
156 | //
|
---|
157 |
|
---|
158 | // Compute the half distance between the current and the final point
|
---|
159 | double dx2 = (x0 - x) / 2.0;
|
---|
160 | double dy2 = (y0 - y) / 2.0;
|
---|
161 | // Convert angle from degrees to radians
|
---|
162 | angle = Math.toRadians(angle % 360.0);
|
---|
163 | double cosAngle = Math.cos(angle);
|
---|
164 | double sinAngle = Math.sin(angle);
|
---|
165 |
|
---|
166 | //
|
---|
167 | // Step 1 : Compute (x1, y1)
|
---|
168 | //
|
---|
169 | double x1 = (cosAngle * dx2 + sinAngle * dy2);
|
---|
170 | double y1 = (-sinAngle * dx2 + cosAngle * dy2);
|
---|
171 | // Ensure radii are large enough
|
---|
172 | rx = Math.abs(rx);
|
---|
173 | ry = Math.abs(ry);
|
---|
174 | double Prx = rx * rx;
|
---|
175 | double Pry = ry * ry;
|
---|
176 | double Px1 = x1 * x1;
|
---|
177 | double Py1 = y1 * y1;
|
---|
178 | // check that radii are large enough
|
---|
179 | double radiiCheck = Px1/Prx + Py1/Pry;
|
---|
180 | if (radiiCheck > 1) {
|
---|
181 | rx = Math.sqrt(radiiCheck) * rx;
|
---|
182 | ry = Math.sqrt(radiiCheck) * ry;
|
---|
183 | Prx = rx * rx;
|
---|
184 | Pry = ry * ry;
|
---|
185 | }
|
---|
186 |
|
---|
187 | //
|
---|
188 | // Step 2 : Compute (cx1, cy1)
|
---|
189 | //
|
---|
190 | double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
|
---|
191 | double sq = ((Prx*Pry)-(Prx*Py1)-(Pry*Px1)) / ((Prx*Py1)+(Pry*Px1));
|
---|
192 | sq = (sq < 0) ? 0 : sq;
|
---|
193 | double coef = (sign * Math.sqrt(sq));
|
---|
194 | double cx1 = coef * ((rx * y1) / ry);
|
---|
195 | double cy1 = coef * -((ry * x1) / rx);
|
---|
196 |
|
---|
197 | //
|
---|
198 | // Step 3 : Compute (cx, cy) from (cx1, cy1)
|
---|
199 | //
|
---|
200 | double sx2 = (x0 + x) / 2.0;
|
---|
201 | double sy2 = (y0 + y) / 2.0;
|
---|
202 | double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
|
---|
203 | double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);
|
---|
204 |
|
---|
205 | //
|
---|
206 | // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
|
---|
207 | //
|
---|
208 | double ux = (x1 - cx1) / rx;
|
---|
209 | double uy = (y1 - cy1) / ry;
|
---|
210 | double vx = (-x1 - cx1) / rx;
|
---|
211 | double vy = (-y1 - cy1) / ry;
|
---|
212 | double p, n;
|
---|
213 | // Compute the angle start
|
---|
214 | n = Math.sqrt((ux * ux) + (uy * uy));
|
---|
215 | p = ux; // (1 * ux) + (0 * uy)
|
---|
216 | sign = (uy < 0) ? -1d : 1d;
|
---|
217 | double angleStart = Math.toDegrees(sign * Math.acos(p / n));
|
---|
218 |
|
---|
219 | // Compute the angle extent
|
---|
220 | n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
|
---|
221 | p = ux * vx + uy * vy;
|
---|
222 | sign = (ux * vy - uy * vx < 0) ? -1d : 1d;
|
---|
223 | double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
|
---|
224 | if(!sweepFlag && angleExtent > 0) {
|
---|
225 | angleExtent -= 360f;
|
---|
226 | } else if (sweepFlag && angleExtent < 0) {
|
---|
227 | angleExtent += 360f;
|
---|
228 | }
|
---|
229 | angleExtent %= 360f;
|
---|
230 | angleStart %= 360f;
|
---|
231 |
|
---|
232 | //
|
---|
233 | // We can now build the resulting Arc2D in double precision
|
---|
234 | //
|
---|
235 | Arc2D.Double arc = new Arc2D.Double();
|
---|
236 | arc.x = cx - rx;
|
---|
237 | arc.y = cy - ry;
|
---|
238 | arc.width = rx * 2.0;
|
---|
239 | arc.height = ry * 2.0;
|
---|
240 | arc.start = -angleStart;
|
---|
241 | arc.extent = -angleExtent;
|
---|
242 |
|
---|
243 | return arc;
|
---|
244 | }
|
---|
245 | }
|
---|