Go to content Go to navigation and search

Home

Current SQL Server Blog Articles


Search

Browse

RSS / Atom

Email me

textpattern

Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

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

Friday May 06 2011 at 01:38

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
As
Begin
    DECLARE
      @dAngleRad              Float,
      @dCentreToChordMidPoint Float;
    BEGIN
      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 );
    END;
END
GO

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.

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment [1]

Hello,
Can you correct “aCos(dCentreToChordMidPoint/====p_dRadius);” part of function…
Thanks alot…

— Kemal · 10 March 2014, 19:36 · #