source: josm/trunk/src/com/kitfox/svg/pathcmd/Arc.java@ 10234

Last change on this file since 10234 was 8084, checked in by bastiK, 10 years ago

add svn:eol-style=native for svgsalamander

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
33 *
34 * Created on January 26, 2004, 8:40 PM
35 */
36
37package com.kitfox.svg.pathcmd;
38
39//import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
40import java.awt.*;
41import java.awt.geom.*;
42
43/**
44 * This is a little used SVG function, as most editors will save curves as
45 * Beziers. To reduce the need to rely on the Batik library, this functionallity
46 * is being bypassed for the time being. In the future, it would be nice to
47 * extend the GeneralPath command to include the arcTo ability provided by Batik.
48 *
49 * @author Mark McKay
50 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
51 */
52public class Arc extends PathCommand
53{
54
55 public float rx = 0f;
56 public float ry = 0f;
57 public float xAxisRot = 0f;
58 public boolean largeArc = false;
59 public boolean sweep = false;
60 public float x = 0f;
61 public float y = 0f;
62
63 /** Creates a new instance of MoveTo */
64 public Arc() {
65 }
66
67 public Arc(boolean isRelative, float rx, float ry, float xAxisRot, boolean largeArc, boolean sweep, float x, float y) {
68 super(isRelative);
69 this.rx = rx;
70 this.ry = ry;
71 this.xAxisRot = xAxisRot;
72 this.largeArc = largeArc;
73 this.sweep = sweep;
74 this.x = x;
75 this.y = y;
76 }
77
78// public void appendPath(ExtendedGeneralPath path, BuildHistory hist)
79 public void appendPath(GeneralPath path, BuildHistory hist)
80 {
81 float offx = isRelative ? hist.lastPoint.x : 0f;
82 float offy = isRelative ? hist.lastPoint.y : 0f;
83
84 arcTo(path, rx, ry, xAxisRot, largeArc, sweep,
85 x + offx, y + offy,
86 hist.lastPoint.x, hist.lastPoint.y);
87// path.lineTo(x + offx, y + offy);
88// hist.setPoint(x + offx, y + offy);
89 hist.setLastPoint(x + offx, y + offy);
90 hist.setLastKnot(x + offx, y + offy);
91 }
92
93 public int getNumKnotsAdded()
94 {
95 return 6;
96 }
97
98 /**
99 * Adds an elliptical arc, defined by two radii, an angle from the
100 * x-axis, a flag to choose the large arc or not, a flag to
101 * indicate if we increase or decrease the angles and the final
102 * point of the arc.
103 *
104 * @param rx the x radius of the ellipse
105 * @param ry the y radius of the ellipse
106 *
107 * @param angle the angle from the x-axis of the current
108 * coordinate system to the x-axis of the ellipse in degrees.
109 *
110 * @param largeArcFlag the large arc flag. If true the arc
111 * spanning less than or equal to 180 degrees is chosen, otherwise
112 * the arc spanning greater than 180 degrees is chosen
113 *
114 * @param sweepFlag the sweep flag. If true the line joining
115 * center to arc sweeps through decreasing angles otherwise it
116 * sweeps through increasing angles
117 *
118 * @param x the absolute x coordinate of the final point of the arc.
119 * @param y the absolute y coordinate of the final point of the arc.
120 * @param x0 - The absolute x coordinate of the initial point of the arc.
121 * @param y0 - The absolute y coordinate of the initial point of the arc.
122 */
123 public void arcTo(GeneralPath path, float rx, float ry,
124 float angle,
125 boolean largeArcFlag,
126 boolean sweepFlag,
127 float x, float y, float x0, float y0)
128 {
129
130 // Ensure radii are valid
131 if (rx == 0 || ry == 0) {
132 path.lineTo((float) x, (float) y);
133 return;
134 }
135
136 if (x0 == x && y0 == y) {
137 // If the endpoints (x, y) and (x0, y0) are identical, then this
138 // is equivalent to omitting the elliptical arc segment entirely.
139 return;
140 }
141
142 Arc2D arc = computeArc(x0, y0, rx, ry, angle,
143 largeArcFlag, sweepFlag, x, y);
144 if (arc == null) return;
145
146 AffineTransform t = AffineTransform.getRotateInstance
147 (Math.toRadians(angle), arc.getCenterX(), arc.getCenterY());
148 Shape s = t.createTransformedShape(arc);
149 path.append(s, true);
150 }
151
152
153 /**
154 * This constructs an unrotated Arc2D from the SVG specification of an
155 * Elliptical arc. To get the final arc you need to apply a rotation
156 * transform such as:
157 *
158 * AffineTransform.getRotateInstance
159 * (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2);
160 */
161 public static Arc2D computeArc(double x0, double y0,
162 double rx, double ry,
163 double angle,
164 boolean largeArcFlag,
165 boolean sweepFlag,
166 double x, double y) {
167 //
168 // Elliptical arc implementation based on the SVG specification notes
169 //
170
171 // Compute the half distance between the current and the final point
172 double dx2 = (x0 - x) / 2.0;
173 double dy2 = (y0 - y) / 2.0;
174 // Convert angle from degrees to radians
175 angle = Math.toRadians(angle % 360.0);
176 double cosAngle = Math.cos(angle);
177 double sinAngle = Math.sin(angle);
178
179 //
180 // Step 1 : Compute (x1, y1)
181 //
182 double x1 = (cosAngle * dx2 + sinAngle * dy2);
183 double y1 = (-sinAngle * dx2 + cosAngle * dy2);
184 // Ensure radii are large enough
185 rx = Math.abs(rx);
186 ry = Math.abs(ry);
187 double Prx = rx * rx;
188 double Pry = ry * ry;
189 double Px1 = x1 * x1;
190 double Py1 = y1 * y1;
191 // check that radii are large enough
192 double radiiCheck = Px1/Prx + Py1/Pry;
193 if (radiiCheck > 1) {
194 rx = Math.sqrt(radiiCheck) * rx;
195 ry = Math.sqrt(radiiCheck) * ry;
196 Prx = rx * rx;
197 Pry = ry * ry;
198 }
199
200 //
201 // Step 2 : Compute (cx1, cy1)
202 //
203 double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
204 double sq = ((Prx*Pry)-(Prx*Py1)-(Pry*Px1)) / ((Prx*Py1)+(Pry*Px1));
205 sq = (sq < 0) ? 0 : sq;
206 double coef = (sign * Math.sqrt(sq));
207 double cx1 = coef * ((rx * y1) / ry);
208 double cy1 = coef * -((ry * x1) / rx);
209
210 //
211 // Step 3 : Compute (cx, cy) from (cx1, cy1)
212 //
213 double sx2 = (x0 + x) / 2.0;
214 double sy2 = (y0 + y) / 2.0;
215 double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
216 double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);
217
218 //
219 // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
220 //
221 double ux = (x1 - cx1) / rx;
222 double uy = (y1 - cy1) / ry;
223 double vx = (-x1 - cx1) / rx;
224 double vy = (-y1 - cy1) / ry;
225 double p, n;
226 // Compute the angle start
227 n = Math.sqrt((ux * ux) + (uy * uy));
228 p = ux; // (1 * ux) + (0 * uy)
229 sign = (uy < 0) ? -1d : 1d;
230 double angleStart = Math.toDegrees(sign * Math.acos(p / n));
231
232 // Compute the angle extent
233 n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
234 p = ux * vx + uy * vy;
235 sign = (ux * vy - uy * vx < 0) ? -1d : 1d;
236 double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
237 if(!sweepFlag && angleExtent > 0) {
238 angleExtent -= 360f;
239 } else if (sweepFlag && angleExtent < 0) {
240 angleExtent += 360f;
241 }
242 angleExtent %= 360f;
243 angleStart %= 360f;
244
245 //
246 // We can now build the resulting Arc2D in double precision
247 //
248 Arc2D.Double arc = new Arc2D.Double();
249 arc.x = cx - rx;
250 arc.y = cy - ry;
251 arc.width = rx * 2.0;
252 arc.height = ry * 2.0;
253 arc.start = -angleStart;
254 arc.extent = -angleExtent;
255
256 return arc;
257 }
258
259 public String toString()
260 {
261 return "A " + rx + " " + ry
262 + " " + xAxisRot + " " + largeArc
263 + " " + sweep
264 + " " + x + " " + y;
265 }
266}
Note: See TracBrowser for help on using the repository browser.