Debian Codegraph @

src/com/github/weisj/jsvg/geometry/mesh/Bezier.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2022 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.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 }