plinth's answer is the same thing I eventually found. There's lots of tricky math that reduces to a magic constant and subdividing the task into four separate Bézier curves.
I needed to do this in raw PDF commands, but the process is the same.
Move to the start of the first curve. This is the center of the circle minus the radius in whatever direction you like.
Figure out the end of the curve (the $x3
and $y3
values in this code). The subscripts come from the Bézier curve control point labels that most people use.
Figure out the control points. That's where the $magic
value shows up.
When you are finished with one segment, do the next. There's nothing particular special about four segments other than it works nicely in cartesian coordinates with just additions and subtractions.
If you want a fill, you end with the f
to paint the inside of the path you just created.
There's some refactoring that I could do, but as I was working with this is was much easier to see that I was getting the signs right by having separate blocks of code. This is the subroutine I was adding to PDF::EasyPDF:
sub make_magic_circle
{
my( $pdf, # PDF::EasyPDF object
$center,
$r # radius
) = @_;
my( $xc, $yc ) = $center->xy;
my $magic = $r * 0.552;
my( $x0p, $y0p ) = ( $xc - $r, $yc );
$pdf->{stream} .= "$x0p $y0p m\n";
{
( $x0p, $y0p ) = ( $xc - $r, $yc );
my( $x1, $y1 ) = ( $x0p, $y0p + $magic );
my( $x2, $y2 ) = ( $x0p + $r - $magic, $y0p + $r );
my( $x3, $y3 ) = ( $x0p + $r, $y0p + $r );
$pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
}
{
( $x0p, $y0p ) = ( $xc, $yc + $r );
my( $x1, $y1 ) = ( $x0p + $magic, $y0p );
my( $x2, $y2 ) = ( $x0p + $r, $y0p - $r + $magic );
my( $x3, $y3 ) = ( $x0p + $r, $y0p - $r );
$pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
}
{
( $x0p, $y0p ) = ( $xc + $r, $yc );
my( $x1, $y1 ) = ( $x0p, $y0p - $magic );
my( $x2, $y2 ) = ( $x0p - $r + $magic, $y0p - $r );
my( $x3, $y3 ) = ( $x0p - $r, $y0p - $r );
$pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
}
{
( $x0p, $y0p ) = ( $xc, $yc - $r );
my( $x1, $y1 ) = ( $x0p - $magic, $y0p );
my( $x2, $y2 ) = ( $x0p - $r, $y0p + $r - $magic );
my( $x3, $y3 ) = ( $x0p - $r, $y0p + $r );
$pdf->{stream} .= "$x1 $y1 $x2 $y2 $x3 $y3 c\n";
}
$pdf->{stream} .= "f\n";
}