Debian Codegraph @

src/com/github/weisj/jsvg/geometry/SVGCircle.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;
23
24 import java.awt.*;
25 import java.awt.geom.Ellipse2D;
26 import java.awt.geom.Rectangle2D;
27
28 import org.jetbrains.annotations.NotNull;
29
30 import com.github.weisj.jsvg.attributes.value.LengthValue;
31 import com.github.weisj.jsvg.renderer.MeasureContext;
32 import com.github.weisj.jsvg.renderer.RenderContext;
33
34 public final class SVGCircle implements SVGShape {
35
36 private final @NotNull Ellipse2D.Float circle = new Ellipse2D.Float();
37 private final @NotNull LengthValue cx;
38 private final @NotNull LengthValue cy;
39 private final @NotNull LengthValue r;
40
41 public SVGCircle(@NotNull LengthValue cx, @NotNull LengthValue cy, @NotNull LengthValue r) {
42 this.cx = cx;
43 this.cy = cy;
44 this.r = r;
45 }
46
47 private void validateShape(@NotNull MeasureContext measureContext) {
48 float x = cx.resolve(measureContext);
49 float y = cy.resolve(measureContext);
50 float rr = r.resolve(measureContext);
51 circle.setFrame(x - rr, y - rr, 2 * rr, 2 * rr);
52 }
53
54 @Override
55 public @NotNull Shape shape(@NotNull RenderContext context, boolean validate) {
56 if (validate) validateShape(context.measureContext());
57 return circle;
58 }
59
60 @Override
61 public @NotNull Rectangle2D bounds(@NotNull RenderContext context, boolean validate) {
62 if (validate) validateShape(context.measureContext());
63 return circle.getBounds2D();
64 }
65
66 @Override
67 public double pathLength(@NotNull RenderContext context) {
68 return circumference(r.resolve(context.measureContext()));
69 }
70
71 @Override
72 public boolean isClosed(@NotNull RenderContext context) {
73 return true;
74 }
75
76 static double circumference(double radius) {
77 return 2 * Math.PI * radius;
78 }
79 }