| •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.mesh; |
| 23 |
|
| 24 |
import static com.github.weisj.jsvg.geometry.mesh.MeshUtil.p; |
| 25 |
import static com.github.weisj.jsvg.geometry.util.GeometryUtil.distanceSquared; |
| 26 |
import static com.github.weisj.jsvg.geometry.util.GeometryUtil.midPoint; |
| 27 |
|
| 28 |
import java.awt.geom.Path2D; |
| 29 |
import java.awt.geom.Point2D; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
|
| 33 |
public class Bezier { |
| 34 |
public final @NotNull Point2D.Float a; |
| 35 |
public final @NotNull Point2D.Float b; |
| 36 |
public final @NotNull Point2D.Float c; |
| 37 |
public final @NotNull Point2D.Float d; |
| 38 |
|
| 39 |
public Bezier(@NotNull Point2D.Float a, @NotNull Point2D.Float b, @NotNull Point2D.Float c, |
| 40 |
@NotNull Point2D.Float d) { |
| 41 |
this.a = a; |
| 42 |
this.b = b; |
| 43 |
this.c = c; |
| 44 |
this.d = d; |
| 45 |
} |
| 46 |
|
| 47 |
public void appendTo(@NotNull Path2D p) { |
| 48 |
p.curveTo(b.x, b.y, c.x, c.y, d.x, d.y); |
| 49 |
} |
| 50 |
|
| 51 |
public @NotNull Bezier inverse() { |
| 52 |
return new Bezier(d, c, b, a); |
| 53 |
} |
| 54 |
|
| 55 |
public @NotNull Split<Bezier> split() { |
| 56 |
Point2D.Float ab = midPoint(a, b); |
| 57 |
Point2D.Float bc = midPoint(b, c); |
| 58 |
Point2D.Float cd = midPoint(c, d); |
| 59 |
Point2D.Float abbc = midPoint(ab, bc); |
| 60 |
Point2D.Float bccd = midPoint(bc, cd); |
| 61 |
Point2D.Float abbcbccd = midPoint(abbc, bccd); |
| 62 |
return new Split<>( |
| 63 |
new Bezier(a, ab, abbc, abbcbccd), |
| 64 |
new Bezier(abbcbccd, bccd, cd, d)); |
| 65 |
} |
| 66 |
|
| 67 |
public static @NotNull Bezier straightLine(Point2D.Float a, Point2D.Float b) { |
| 68 |
return new LineBezier(a, b); |
| 69 |
} |
| 70 |
|
| 71 |
public static @NotNull Bezier combine(@NotNull Bezier b1, @NotNull Bezier b2, @NotNull Bezier b3) { |
| 72 |
return new Bezier( |
| 73 |
p(b1.a.x + b2.a.x - b3.a.x, b1.a.y + b2.a.y - b3.a.y), |
| 74 |
p(b1.b.x + b2.b.x - b3.b.x, b1.b.y + b2.b.y - b3.b.y), |
| 75 |
p(b1.c.x + b2.c.x - b3.c.x, b1.c.y + b2.c.y - b3.c.y), |
| 76 |
p(b1.d.x + b2.d.x - b3.d.x, b1.d.y + b2.d.y - b3.d.y)); |
| 77 |
} |
| 78 |
|
| 79 |
public int estimateStepCount(float scaleX, float scaleY) { |
| 80 |
double steps = Math.max( |
| 81 |
Math.max(distanceSquared(a, b, scaleX, scaleY), distanceSquared(c, d, scaleX, scaleY)), |
| 82 |
Math.max(distanceSquared(a, c, scaleX, scaleY) / 4, distanceSquared(b, d, scaleX, scaleY) / 4)); |
| 83 |
steps *= 18; |
| 84 |
steps = Math.max(1, steps); |
| 85 |
return (Math.getExponent(steps) + 1) / 2; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
@Override |
| 90 |
public String toString() { |
| 91 |
return "Bezier{" + |
| 92 |
"a=" + a + |
| 93 |
", b=" + b + |
| 94 |
", c=" + c + |
| 95 |
", d=" + d + |
| 96 |
'}'; |
| 97 |
} |
| 98 |
} |