| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 36 |
y |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 113 |
if (rx == 0 || ry == 0) { |
| 114 |
path.lineTo(x, y); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (x0 == x && y0 == y) { |
| 119 |
|
| 120 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
double dx2 = (x0 - x) / 2.0; |
| 166 |
double dy2 = (y0 - y) / 2.0; |
| 167 |
|
| 168 |
angle = Math.toRadians(angle % 360.0); |
| 169 |
double cosAngle = Math.cos(angle); |
| 170 |
double sinAngle = Math.sin(angle); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
double x1 = cosAngle * dx2 + sinAngle * dy2; |
| 176 |
double y1 = -sinAngle * dx2 + cosAngle * dy2; |
| 177 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 221 |
n = Math.sqrt((ux * ux) + (uy * uy)); |
| 222 |
p = ux; |
| 223 |
sign = (uy < 0) ? -1d : 1d; |
| 224 |
double angleStart = Math.toDegrees(sign * Math.acos(p / n)); |
| 225 |
|
| 226 |
|
| 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 |
|
| 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 |
} |