How do you calculate the radius of the smallest circle in which a sprite fits?


I am making a game in HTML5, JS.

In the game I perform the collisions by circle-shapes since my sprites are rotating.

The player has different shapes since it has different sprites when it holds different weapons.

The radius of the player is half the sprites width / height.

The sprites are resized from 16x16 to 50x50 pixels (50 / 16 = 3.125, so 3.125 times bigger)

My question is: How do you calculate the radius of the smallest circle-shape that the sprite would fit in? Like this:

Normal Sprite:

Red = To calculate radius of (how?), Blue = Radius I use (sprite width or height / 2):

(For a better look of the images open them in a image program or something to zoom in please.)

Several steps to getting your answer...


Determine the bounding box of the player-pixels (excluding transparent pixels).

  • Use .getImageData to get the individual pixel data for the image.

  • Examine each vertical column from the left and determine where the first player-pixel occurs.

  • Examine each vertical column from the right and determine where the first player-pixel occurs.

  • Examine each horizontal row from the top and determine where the first player-pixel occurs.

  • Examine each horizontal row from the bottom and determine where the first player-pixel occurs.

Now you have top, left, bottom & right bounding values.

Determine the radius of the bounding box (== half of its diagonal):

var dx=right-left;
var dy=bottom-top;
var radius=Math.sqrt(dx*dx+dy*dy)/2;

So your containing circle has that calculated radius.

Here's example code:

<!doctype html>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src=""></script>

    body{ background-color: ivory; padding:20px; }
    #canvas{border:1px solid red;}


    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");


    var img=new Image();

    function start(){

        var cw=canvas.width;
        var ch=canvas.height;


        var data=ctx.getImageData(0,0,cw,ch).data;
        var leftX=getLeft(data,cw,ch);
        var rightX=getRight(data,cw,ch);
        var topY=getTop(data,cw,ch);
        var bottomY=getBottom(data,cw,ch);
        var w=rightX-leftX;
        var h=bottomY-topY;
        var cx=leftX+w/2;
        var cy=topY+h/2;
        var radius=Math.sqrt(w*w+h*h)/2;




    function getLeft(data,width,height){
        for(var x=0;x<width;x++)
        for(var y=0;y<height;y++)
            if(data[(width*y+x)*4+3]>0){ return(x); }

    function getRight(data,width,height){
        for(var x=width-1;x>=0;x--)
        for(var y=height-1;y>=0;y--)
            if(data[(width*y+x)*4+3]>0){ return(x); }

    function getTop(data,width,height){
        for(var y=0;y<height;y++)
        for(var x=0;x<width;x++)
            if(data[(width*y+x)*4+3]>0){ return(y); }

    function getBottom(data,width,height){
        for(var y=height-1;y>=0;y--)
        for(var x=width-1;x>=0;x--)
            if(data[(width*y+x)*4+3]>0){ return(y); }

}); // end $(function(){});


    <h4>1. Calc boundingbox of non-transparent pixels<br>2. Calc radius that contains that boundingbox</h4>
    <canvas id="canvas" width=300 height=300></canvas>