| •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; |
| 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 |
} |