001 /*
002 * MoveTo.java
003 *
004 *
005 * The Salamander Project - 2D and 3D graphics libraries in Java
006 * Copyright (C) 2004 Mark McKay
007 *
008 * This library is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU Lesser General Public
010 * License as published by the Free Software Foundation; either
011 * version 2.1 of the License, or (at your option) any later version.
012 *
013 * This library is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016 * Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with this library; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021 *
022 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
023 * projects can be found at http://www.kitfox.com
024 *
025 * Created on January 26, 2004, 8:40 PM
026 */
027
028 package com.kitfox.svg.pathcmd;
029
030 //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
031 import java.awt.*;
032 import java.awt.geom.*;
033
034 /**
035 * This is a little used SVG function, as most editors will save curves as
036 * Beziers. To reduce the need to rely on the Batik library, this functionallity
037 * is being bypassed for the time being. In the future, it would be nice to
038 * extend the GeneralPath command to include the arcTo ability provided by Batik.
039 *
040 * @author Mark McKay
041 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
042 */
043 public class Arc extends PathCommand
044 {
045
046 public float rx = 0f;
047 public float ry = 0f;
048 public float xAxisRot = 0f;
049 public boolean largeArc = false;
050 public boolean sweep = false;
051 public float x = 0f;
052 public float y = 0f;
053
054 /** Creates a new instance of MoveTo */
055 public Arc() {
056 }
057
058 public Arc(boolean isRelative, float rx, float ry, float xAxisRot, boolean largeArc, boolean sweep, float x, float y) {
059 super(isRelative);
060 this.rx = rx;
061 this.ry = ry;
062 this.xAxisRot = xAxisRot;
063 this.largeArc = largeArc;
064 this.sweep = sweep;
065 this.x = x;
066 this.y = y;
067 }
068
069 // public void appendPath(ExtendedGeneralPath path, BuildHistory hist)
070 public void appendPath(GeneralPath path, BuildHistory hist)
071 {
072 float offx = isRelative ? hist.history[0].x : 0f;
073 float offy = isRelative ? hist.history[0].y : 0f;
074
075 arcTo(path, rx, ry, xAxisRot, largeArc, sweep, x + offx, y + offy, hist.history[0].x, hist.history[0].y);
076 // path.lineTo(x + offx, y + offy);
077 hist.setPoint(x + offx, y + offy);
078 }
079
080 public int getNumKnotsAdded()
081 {
082 return 6;
083 }
084
085 /**
086 * Adds an elliptical arc, defined by two radii, an angle from the
087 * x-axis, a flag to choose the large arc or not, a flag to
088 * indicate if we increase or decrease the angles and the final
089 * point of the arc.
090 *
091 * @param rx the x radius of the ellipse
092 * @param ry the y radius of the ellipse
093 *
094 * @param angle the angle from the x-axis of the current
095 * coordinate system to the x-axis of the ellipse in degrees.
096 *
097 * @param largeArcFlag the large arc flag. If true the arc
098 * spanning less than or equal to 180 degrees is chosen, otherwise
099 * 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 }