Debian Codegraph @

src/com/github/weisj/jsvg/geometry/path/Arc.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.geometry.path;
23
24 import java.awt.Shape;
25 import java.awt.geom.AffineTransform;
26 import java.awt.geom.Arc2D;
27 import java.awt.geom.Path2D;
28 import java.awt.geom.Point2D;
29
30 import org.jetbrains.annotations.NotNull;
31
32 import com.github.weisj.jsvg.util.ShapeUtil;
33
34 /**
35 * This is a little used SVG function, as most editors will save curves as
36 * Béziers. To reduce the need to rely on the Batik library, this functionality
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:[email protected]">Mark McKay</a>
42 * @author Jannis Weis
43 */
44 final class Arc extends PathCommand {
45
46 private final float rx;
47 private final float ry;
48 private final float xAxisRot;
49 private final boolean largeArc;
50 private final boolean sweep;
51 private final float x;
52 private final float y;
53
54 public Arc(boolean isRelative,
55 float rx, float ry,
56 float xAxisRot,
57 boolean largeArc, boolean sweep,
58 float x, float y) {
59 super(isRelative, 6);
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 @Override
70 public void appendPath(@NotNull Path2D path, @NotNull BuildHistory hist) {
71 Point2D.Float offset = offset(hist);
72
73 arcTo(path, rx, ry, xAxisRot, largeArc, sweep,
74 x + offset.x, y + offset.y,
75 hist.lastPoint.x, hist.lastPoint.y);
76 hist.setLast(path.getCurrentPoint());
77 }
78
79 /**
80 * Adds an elliptical arc, defined by two radii, an angle from the
81 * x-axis, a flag to choose the large arc or not, a flag to
82 * indicate if we increase or decrease the angles and the final
83 * point of the arc.
84 *
85 * @param path The path that the arc will be appended to.
86 *
87 * @param rx the x radius of the ellipse
88 * @param ry the y radius of the ellipse
89 *
90 * @param angle the angle from the x-axis of the current
91 * coordinate system to the x-axis of the ellipse in degrees.
92 *
93 * @param largeArcFlag the large arc flag. If true the arc
94 * spanning less than or equal to 180 degrees is chosen, otherwise
95 * the arc spanning greater than 180 degrees is chosen
96 *
97 * @param sweepFlag the sweep flag. If true the line joining
98 * center to arc sweeps through decreasing angles otherwise it
99 * sweeps through increasing angles
100 *
101 * @param x the absolute x coordinate of the final point of the arc.
102 * @param y the absolute y coordinate of the final point of the arc.
103 * @param x0 - The absolute x coordinate of the initial point of the arc.
104 * @param y0 - The absolute y coordinate of the initial point of the arc.
105 */
106 private void arcTo(@NotNull Path2D path, float rx, float ry,
107 float angle,
108 boolean largeArcFlag,
109 boolean sweepFlag,
110 float x, float y, float x0, float y0) {
111
112 // Ensure radii are valid
113 if (rx == 0 || ry == 0) {
114 path.lineTo(x, y);
115 return;
116 }
117
118 if (x0 == x && y0 == y) {
119 // If the endpoints (x, y) and (x0, y0) are identical, then this
120 // is equivalent to omitting the elliptical arc segment entirely.
121 return;
122 }
123
124 Arc2D arc = computeRawArc(x0, y0, rx, ry, angle, largeArcFlag, sweepFlag, x, y);
125
126 AffineTransform t = AffineTransform.getRotateInstance(
127 Math.toRadians(angle), arc.getCenterX(), arc.getCenterY());
128 Shape s = ShapeUtil.transformShape(arc, t);
129 path.append(s, true);
130 }
131
132
133 /**
134 * This constructs a non-rotated Arc2D from the SVG specification of an
135 * Elliptical arc. To get the final arc you need to apply a rotation
136 * transform such as:
137 * <p>
138 * AffineTransform.getRotateInstance
139 * (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2);
140 *
141 * @param x0 origin of arc in x
142 * @param y0 origin of arc in y
143 * @param rx radius of arc in x
144 * @param ry radius of arc in y
145 * @param angle number of radians in arc
146 * @param largeArcFlag the large arc flag
147 * @param sweepFlag the sweep flag
148 * @param x ending coordinate of arc in x
149 * @param y ending coordinate of arc in y
150 * @return arc shape
151 *
152 */
153 private static @NotNull Arc2D computeRawArc(
154 double x0, double y0,
155 double rx, double ry,
156 double angle,
157 boolean largeArcFlag,
158 boolean sweepFlag,
159 double x, double y) {
160 //
161 // Elliptical arc implementation based on the SVG specification notes
162 //
163
164 // Compute the half distance between the current and the final point
165 double dx2 = (x0 - x) / 2.0;
166 double dy2 = (y0 - y) / 2.0;
167 // Convert angle from degrees to radians
168 angle = Math.toRadians(angle % 360.0);
169 double cosAngle = Math.cos(angle);
170 double sinAngle = Math.sin(angle);
171
172 //
173 // Step 1 : Compute (x1, y1)
174 //
175 double x1 = cosAngle * dx2 + sinAngle * dy2;
176 double y1 = -sinAngle * dx2 + cosAngle * dy2;
177 // Ensure radii are large enough
178 rx = Math.abs(rx);
179 ry = Math.abs(ry);
180 double pRx = rx * rx;
181 double pRy = ry * ry;
182 double pX1 = x1 * x1;
183 double pY1 = y1 * y1;
184 // check that radii are large enough
185 double radiiCheck = pX1 / pRx + pY1 / pRy;
186 if (radiiCheck > 1) {
187 rx = Math.sqrt(radiiCheck) * rx;
188 ry = Math.sqrt(radiiCheck) * ry;
189 pRx = rx * rx;
190 pRy = ry * ry;
191 }
192
193 //
194 // Step 2 : Compute (cx1, cy1)
195 //
196 double sign = largeArcFlag == sweepFlag ? -1 : 1;
197 double sq = ((pRx * pRy) - (pRx * pY1) - (pRy * pX1)) / ((pRx * pY1) + (pRy * pX1));
198 sq = sq < 0 ? 0 : sq;
199 double coefficient = sign * Math.sqrt(sq);
200 double cx1 = coefficient * ((rx * y1) / ry);
201 double cy1 = coefficient * -((ry * x1) / rx);
202
203 //
204 // Step 3 : Compute (cx, cy) from (cx1, cy1)
205 //
206 double sx2 = (x0 + x) / 2.0;
207 double sy2 = (y0 + y) / 2.0;
208 double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1);
209 double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1);
210
211 //
212 // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle)
213 //
214 double ux = (x1 - cx1) / rx;
215 double uy = (y1 - cy1) / ry;
216 double vx = (-x1 - cx1) / rx;
217 double vy = (-y1 - cy1) / ry;
218 double p;
219 double n;
220 // Compute the angle start
221 n = Math.sqrt((ux * ux) + (uy * uy));
222 p = ux; // (1 * ux) + (0 * uy)
223 sign = (uy < 0) ? -1d : 1d;
224 double angleStart = Math.toDegrees(sign * Math.acos(p / n));
225
226 // Compute the angle extent
227 n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
228 p = ux * vx + uy * vy;
229 sign = ux * vy - uy * vx < 0 ? -1d : 1d;
230 double angleExtent = Math.toDegrees(sign * Math.acos(p / n));
231 if (!sweepFlag && angleExtent > 0) {
232 angleExtent -= 360f;
233 } else if (sweepFlag && angleExtent < 0) {
234 angleExtent += 360f;
235 }
236 angleExtent %= 360f;
237 angleStart %= 360f;
238
239 //
240 // We can now build the resulting Arc2D in double precision
241 //
242 Arc2D.Double arc = new Arc2D.Double();
243 arc.x = cx - rx;
244 arc.y = cy - ry;
245 arc.width = rx * 2.0;
246 arc.height = ry * 2.0;
247 arc.start = -angleStart;
248 arc.extent = -angleExtent;
249
250 return arc;
251 }
252
253 @Override
254 public String toString() {
255 return "A " + rx + " " + ry
256 + " " + xAxisRot + " " + largeArc
257 + " " + sweep
258 + " " + x + " " + y;
259 }
260 }