| 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 |
|
|---|
| 37 | package com.kitfox.svg.pathcmd;
|
|---|
| 38 |
|
|---|
| 39 | //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
|
|---|
| 40 | import java.awt.*;
|
|---|
| 41 | import 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 | */
|
|---|
| 52 | public 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 | @Override
|
|---|
| 80 | public void appendPath(GeneralPath path, BuildHistory hist)
|
|---|
| 81 | {
|
|---|
| 82 | float offx = isRelative ? hist.lastPoint.x : 0f;
|
|---|
| 83 | float offy = isRelative ? hist.lastPoint.y : 0f;
|
|---|
| 84 |
|
|---|
| 85 | arcTo(path, rx, ry, xAxisRot, largeArc, sweep,
|
|---|
| 86 | x + offx, y + offy,
|
|---|
| 87 | hist.lastPoint.x, hist.lastPoint.y);
|
|---|
| 88 | // path.lineTo(x + offx, y + offy);
|
|---|
| 89 | // hist.setPoint(x + offx, y + offy);
|
|---|
| 90 | hist.setLastPoint(x + offx, y + offy);
|
|---|
| 91 | hist.setLastKnot(x + offx, y + offy);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | @Override
|
|---|
| 95 | public int getNumKnotsAdded()
|
|---|
| 96 | {
|
|---|
| 97 | return 6;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Adds an elliptical arc, defined by two radii, an angle from the
|
|---|
| 102 | * x-axis, a flag to choose the large arc or not, a flag to
|
|---|
| 103 | * indicate if we increase or decrease the angles and the final
|
|---|
| 104 | * point of the arc.
|
|---|
| 105 | *
|
|---|
| 106 | * @param rx the x radius of the ellipse
|
|---|
| 107 | * @param ry the y radius of the ellipse
|
|---|
| 108 | *
|
|---|
| 109 | * @param angle the angle from the x-axis of the current
|
|---|
| 110 | * coordinate system to the x-axis of the ellipse in degrees.
|
|---|
| 111 | *
|
|---|
| 112 | * @param largeArcFlag the large arc flag. If true the arc
|
|---|
| 113 | * spanning less than or equal to 180 degrees is chosen, otherwise
|
|---|
| 114 | * the arc spanning greater than 180 degrees is chosen
|
|---|
| 115 | *
|
|---|
| 116 | * @param sweepFlag the sweep flag. If true the line joining
|
|---|
| 117 | * center to arc sweeps through decreasing angles otherwise it
|
|---|
| 118 | * sweeps through increasing angles
|
|---|
| 119 | *
|
|---|
| 120 | * @param x the absolute x coordinate of the final point of the arc.
|
|---|
| 121 | * @param y the absolute y coordinate of the final point of the arc.
|
|---|
| 122 | * @param x0 - The absolute x coordinate of the initial point of the arc.
|
|---|
| 123 | * @param y0 - The absolute y coordinate of the initial point of the arc.
|
|---|
| 124 | */
|
|---|
| 125 | public void arcTo(GeneralPath path, float rx, float ry,
|
|---|
| 126 | float angle,
|
|---|
| 127 | boolean largeArcFlag,
|
|---|
| 128 | boolean sweepFlag,
|
|---|
| 129 | float x, float y, float x0, float y0)
|
|---|
| 130 | {
|
|---|
| 131 |
|
|---|
| 132 | // Ensure radii are valid
|
|---|
| 133 | if (rx == 0 || ry == 0) {
|
|---|
| 134 | path.lineTo((float) x, (float) y);
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (x0 == x && y0 == y) {
|
|---|
| 139 | // If the endpoints (x, y) and (x0, y0) are identical, then this
|
|---|
| 140 | // is equivalent to omitting the elliptical arc segment entirely.
|
|---|
| 141 | return;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | Arc2D arc = computeArc(x0, y0, rx, ry, angle,
|
|---|
| 145 | largeArcFlag, sweepFlag, x, y);
|
|---|
| 146 | if (arc == null) return;
|
|---|
| 147 |
|
|---|
| 148 | AffineTransform t = AffineTransform.getRotateInstance
|
|---|
| 149 | (Math.toRadians(angle), arc.getCenterX(), arc.getCenterY());
|
|---|
| 150 | Shape s = t.createTransformedShape(arc);
|
|---|
| 151 | path.append(s, true);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * This constructs an unrotated Arc2D from the SVG specification of an
|
|---|
| 157 | * Elliptical arc. To get the final arc you need to apply a rotation
|
|---|
| 158 | * transform such as:
|
|---|
| 159 | *
|
|---|
| 160 | * AffineTransform.getRotateInstance
|
|---|
| 161 | * (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2);
|
|---|
| 162 | */
|
|---|
| 163 | public static Arc2D computeArc(double x0, double y0,
|
|---|
| 164 | double rx, double ry,
|
|---|
| 165 | double angle,
|
|---|
| 166 | boolean largeArcFlag,
|
|---|
| 167 | boolean sweepFlag,
|
|---|
| 168 | double x, double y) {
|
|---|
| 169 | //
|
|---|
| 170 | // Elliptical arc implementation based on the SVG specification notes
|
|---|
| 171 | //
|
|---|
| 172 |
|
|---|
| 173 | // Compute the half distance between the current and the final point
|
|---|
| 174 | double dx2 = (x0 - x) / 2.0;
|
|---|
| 175 | double dy2 = (y0 - y) / 2.0;
|
|---|
| 176 | // Convert angle from degrees to radians
|
|---|
| 177 | angle = Math.toRadians(angle % 360.0);
|
|---|
| 178 | double cosAngle = Math.cos(angle);
|
|---|
| 179 | double sinAngle = Math.sin(angle);
|
|---|
| 180 |
|
|---|
| 181 | //
|
|---|
| 182 | // Step 1 : Compute (x1, y1)
|
|---|
| 183 | //
|
|---|
| 184 | double x1 = (cosAngle * dx2 + sinAngle * dy2);
|
|---|
| 185 | double y1 = (-sinAngle * dx2 + cosAngle * dy2);
|
|---|
| 186 | // Ensure radii are large enough
|
|---|
| 187 | rx = Math.abs(rx);
|
|---|
| 188 | ry = Math.abs(ry);
|
|---|
| 189 | double Prx = rx * rx;
|
|---|
| 190 | double Pry = ry * ry;
|
|---|
| 191 | double Px1 = x1 * x1;
|
|---|
| 192 | double Py1 = y1 * y1;
|
|---|
| 193 | // check that radii are large enough
|
|---|
| 194 | double radiiCheck = Px1/Prx + Py1/Pry;
|
|---|
| 195 | if (radiiCheck > 1) {
|
|---|
| 196 | rx = Math.sqrt(radiiCheck) * rx;
|
|---|
| 197 | ry = Math.sqrt(radiiCheck) * ry;
|
|---|
| 198 | Prx = rx * rx;
|
|---|
| 199 | Pry = ry * ry;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | //
|
|---|
| 203 | // Step 2 : Compute (cx1, cy1)
|
|---|
| 204 | //
|
|---|
| 205 | double sign = (largeArcFlag == sweepFlag) ? -1 : 1;
|
|---|
| 206 | double sq = ((Prx*Pry)-(Prx*Py1)-(Pry*Px1)) / ((Prx*Py1)+(Pry*Px1));
|
|---|
| 207 | sq = (sq < 0) ? 0 : sq;
|
|---|
| 208 | double coef = (sign * Math.sqrt(sq));
|
|---|
| 209 | double cx1 = coef * ((rx * y1) / ry);
|
|---|
| 210 | double cy1 = coef * -((ry * x1) / rx);
|
|---|
| 211 |
|
|---|
| 212 | //
|
|---|
| 213 | // Step 3 : Compute (cx, cy) from (cx1, cy1)
|
|---|
| 214 | //
|
|---|
| 215 | double sx2 = (x0 + x) / 2.0;
|
|---|
| 216 | double sy2 = (y0 + y) / 2.0;
|
|---|
| 217 | double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
|
|---|
| 218 | double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);
|
|---|
| 219 |
|
|---|
| 220 | //
|
|---|
| 221 | // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
|
|---|
| 222 | //
|
|---|
| 223 | double ux = (x1 - cx1) / rx;
|
|---|
| 224 | double uy = (y1 - cy1) / ry;
|
|---|
| 225 | double vx = (-x1 - cx1) / rx;
|
|---|
| 226 | double vy = (-y1 - cy1) / ry;
|
|---|
| 227 | double p, n;
|
|---|
| 228 | // Compute the angle start
|
|---|
| 229 | n = Math.sqrt((ux * ux) + (uy * uy));
|
|---|
| 230 | p = ux; // (1 * ux) + (0 * uy)
|
|---|
| 231 | sign = (uy < 0) ? -1d : 1d;
|
|---|
| 232 | double angleStart = Math.toDegrees(sign * Math.acos(p / n));
|
|---|
| 233 |
|
|---|
| 234 | // Compute the angle extent
|
|---|
| 235 | n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
|
|---|
| 236 | p = ux * vx + uy * vy;
|
|---|
| 237 | sign = (ux * vy - uy * vx < 0) ? -1d : 1d;
|
|---|
| 238 | double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
|
|---|
| 239 | if(!sweepFlag && angleExtent > 0) {
|
|---|
| 240 | angleExtent -= 360f;
|
|---|
| 241 | } else if (sweepFlag && angleExtent < 0) {
|
|---|
| 242 | angleExtent += 360f;
|
|---|
| 243 | }
|
|---|
| 244 | angleExtent %= 360f;
|
|---|
| 245 | angleStart %= 360f;
|
|---|
| 246 |
|
|---|
| 247 | //
|
|---|
| 248 | // We can now build the resulting Arc2D in double precision
|
|---|
| 249 | //
|
|---|
| 250 | Arc2D.Double arc = new Arc2D.Double();
|
|---|
| 251 | arc.x = cx - rx;
|
|---|
| 252 | arc.y = cy - ry;
|
|---|
| 253 | arc.width = rx * 2.0;
|
|---|
| 254 | arc.height = ry * 2.0;
|
|---|
| 255 | arc.start = -angleStart;
|
|---|
| 256 | arc.extent = -angleExtent;
|
|---|
| 257 |
|
|---|
| 258 | return arc;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | @Override
|
|---|
| 262 | public String toString()
|
|---|
| 263 | {
|
|---|
| 264 | return "A " + rx + " " + ry
|
|---|
| 265 | + " " + xAxisRot + " " + largeArc
|
|---|
| 266 | + " " + sweep
|
|---|
| 267 | + " " + x + " " + y;
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|