CircleMacroPrimitive.java

package com.varnernet.gerb4j.macro;

import com.varnernet.gerb4j.Polarity;
import com.varnernet.gerb4j.render.GerberOutputTarget;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * Gerber macro primitive type 1 — Circle. Format: {@code
 * 1,exposure,diameter,centerX,centerY[,rotation]}
 */
final class CircleMacroPrimitive extends AbstractMacroPrimitive {

    private final double diameter;
    private final double centerX;
    private final double centerY;
    private final double rotation; // degrees, CCW

    CircleMacroPrimitive(
            final boolean exposed, final double diameter, final double centerX,
            final double centerY, final double rotation) {
        super(exposed);
        this.diameter = diameter;
        this.centerX = centerX;
        this.centerY = centerY;
        this.rotation = rotation;
    }

    @Override
    public Rectangle2D getBounds() {
        double r = diameter / 2.0;
        double[] center = rotatePoint(centerX, centerY, rotation);
        return new Rectangle2D.Double(center[0] - r, center[1] - r, diameter, diameter);
    }

    @Override
    public void render(final Point2D flashPoint, final GerberOutputTarget target, final Polarity polarity) {
        double[] rotated = rotatePoint(centerX, centerY, rotation);
        Point2D center = new Point2D.Double(flashPoint.getX() + rotated[0], flashPoint.getY() + rotated[1]);
        target.drawCircle(center, diameter, 0.0, effectivePolarity(polarity));
    }
}