Hi all,

I created a script that generates a top view of a curved settee based on the radius and overall length provided by the user.

I draw two arcs using imagearc(), and then connect the endpoints by calculating the X,Y coordinates using radians.

The X,Y coordinates are calculated fine until you increase the radius up to about 200" - then the coordinates start to calculate incorrectly by a few pixels or more.

There's a sample image below of the endpoints being displayed incorrectly (notice the gap). Any idea how to get an accurate X,Y coordinate out of this?

The code is below. $r is input radius, $l is input overall length.

        // Set the center points for the arc radius
        $centerx = $imgwidth/2;
        $centery = $imgheight/2 + $r - ($h/2);

    // Calculate the degree in radians of the arc
    $p = 2 * asin($l/(2*$r));

    // Convert radians to degrees
    $theta = rad2deg($p);

    // Calculate start and end points in degrees
    $start = 270 - ($theta/2);
    $end = 270 + ($theta/2);

    // Draw first arc
    imagearc($img, $centerx, $centery, ($r*2), ($r*2), $start, $end, $color);

    // Convert start point to radians
    $r_start = deg2rad(90 + ($theta/2));
    $r_end = deg2rad(90 - ($theta/2));

    // Calculate X,Y coordinates of start/end points
    $ax = cos($r_start)*$r;
    $ay = -sin($r_start)*$r;        
    $bx = cos($r_end)*$r;
    $by = -sin($r_end)*$r;

    // Subtract 24" from the radius to prepare the inside curve
    $r = $r - (24 * $scale);

    // Calculate start and end degrees
    $start = 270 - ($theta/2);
    $end = 270 + ($theta/2);

    // Draw second arc
    imagearc($img, $centerx, $centery, ($r*2), ($r*2), $start, $end, $color);

    $arccentery = $centery - $r - 24;

    // Calculate X,Y coordinates of start/end points
    $cx = cos($r_start)*$r;
    $cy = -sin($r_start)*$r;        
    $dx = cos($r_end)*$r;
    $dy = -sin($r_end)*$r;

    $ax = round($ax+$centerx);
    $ay = round($ay+$centery);
    $bx = round($bx+$centerx);
    $by = round($by+$centery);
    $cx = round($cx+$centerx);
    $cy = round($cy+$centery);
    $dx = round($dx+$centerx);
    $dy = round($dy+$centery);

    // Close the arc with endpoint lines
    imageline($img, $ax, $ay, $cx, $cy, $black);
    imageline($img, $bx, $by, $dx, $dy, $black);

    perhaps the error lies in the fact that imagearc() expects an integer for the start and end degrees, whereas the line endpoints are calculated as floats.

    have you checked out the user notes for the imagearc() function? specifically, look for a function named imagenofilledarc(). It might be of some use to you.

      Indeed. If I round the arc radians to a single point of precision I get accurate coordinates; at the cost of a fair amount of precision. I suppose being limited to an integer with imagearc() I'll have to plot my own arc to get the precision.

      Thanks for the help.

        Write a Reply...