COGO: Compute number of vertices required to stroke circle or circular arc

In working with circles or arc segments one needs to be able to do more than just calculate chordlengths, arclengths or arc2chord separations.

For example, in order to be able to convert circles and arcs to vertex-stroked polygons and vice versa one needs to be able to compute the number of vertices to place between the points that subtend an arc on the circumference. This is what this function does.

Note that I have a schema call cogo in which I create functions like this. You can use anything you like.

 /* ----------------------------------------------------------------------------------------
 * @function   : OptimalCircleSegments
 * @precis     : Returns the optimal integer number of circle segments for given arc-to-chord
 *               separation and radius
 * @version    : 1.0
 * @usage      : FUNCTION OptimalCircleSegments( @dRadius Float,
 *                                               @dArcToChordSeparation Float)
 *                        RETURNS INTEGER
 *               eg select cogo.OptimalCircleSegments(10,0.01) as segments;
 * @param      : @dRadius                 : Radius of Circle
 * @paramtype  : @dRadius                 : Float
 * @param      : @dArcToChordSeparation   : Distance between the midpoint of the Arc and the Chord in metres
 * @paramtype  : @dArcToChordSeparation   : Float
 * @return     : OptimalCircleSegments    : the optimal number of segments
 * @rtnType    : OptimalCircleSegments    : Integer
 * @note       : Does not throw exceptions
 * @note       : Assumes planar projection eg UTM.
 * @history    : Simon Greener - Feb 2005 - Original coding.
 Create Function [cogo].[OptimalCircleSegments](@p_dRadius               Float,
                                                  @p_dArcToChordSeparation Float)
 Returns Integer
       @dAngleRad              Float,
       @dCentreToChordMidPoint Float;
       IF ( @p_dRadius IS NULL OR
            @p_dArcToChordSeparation IS NULL )
           RETURN NULL;
       SET @dCentreToChordMidPoint = @p_dRadius - @p_dArcToChordSeparation;
       SET @dAngleRad              = 2 * aCos(@dCentreToChordMidPoint/==@==p_dRadius);
       Return CEILING( (2 * PI() ) / @dAngleRad );

Here is an example of how to use the function.

 select cogo.OptimalCircleSegments(10,0.01) as segments;

I hope this is useful to someone.

Leave a Reply

Your email address will not be published. Required fields are marked *