001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on January 26, 2004, 8:40 PM 035 */ 036 037package com.kitfox.svg.pathcmd; 038 039//import org.apache.batik.ext.awt.geom.ExtendedGeneralPath; 040import java.awt.*; 041import java.awt.geom.*; 042 043/** 044 * This is a little used SVG function, as most editors will save curves as 045 * Beziers. To reduce the need to rely on the Batik library, this functionallity 046 * is being bypassed for the time being. In the future, it would be nice to 047 * extend the GeneralPath command to include the arcTo ability provided by Batik. 048 * 049 * @author Mark McKay 050 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 051 */ 052public class Arc extends PathCommand 053{ 054 055 public float rx = 0f; 056 public float ry = 0f; 057 public float xAxisRot = 0f; 058 public boolean largeArc = false; 059 public boolean sweep = false; 060 public float x = 0f; 061 public float y = 0f; 062 063 /** Creates a new instance of MoveTo */ 064 public Arc() { 065 } 066 067 public Arc(boolean isRelative, float rx, float ry, float xAxisRot, boolean largeArc, boolean sweep, float x, float y) { 068 super(isRelative); 069 this.rx = rx; 070 this.ry = ry; 071 this.xAxisRot = xAxisRot; 072 this.largeArc = largeArc; 073 this.sweep = sweep; 074 this.x = x; 075 this.y = y; 076 } 077 078// public void appendPath(ExtendedGeneralPath path, BuildHistory hist) 079 public void appendPath(GeneralPath path, BuildHistory hist) 080 { 081 float offx = isRelative ? hist.lastPoint.x : 0f; 082 float offy = isRelative ? hist.lastPoint.y : 0f; 083 084 arcTo(path, rx, ry, xAxisRot, largeArc, sweep, 085 x + offx, y + offy, 086 hist.lastPoint.x, hist.lastPoint.y); 087// path.lineTo(x + offx, y + offy); 088// hist.setPoint(x + offx, y + offy); 089 hist.setLastPoint(x + offx, y + offy); 090 hist.setLastKnot(x + offx, y + offy); 091 } 092 093 public int getNumKnotsAdded() 094 { 095 return 6; 096 } 097 098 /** 099 * 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}