# Dynamic seperation of circle into segments

**URL:** <https://forum.kirupa.com/t/dynamic-seperation-of-circle-into-segments/262542>\
**Category:** Uncategorized\
**Created:** [June 6, 2008, 8:16pm UTC](https://forum.kirupa.com/t/dynamic-seperation-of-circle-into-segments/262542 "2008-06-06T20:16:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nebula\_228](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nebula\_228](https://forum.kirupa.com/u/nebula_228)\
**Post date:** [June 6, 2008, 8:16pm UTC](https://forum.kirupa.com/t/dynamic-seperation-of-circle-into-segments/262542/1 "2008-06-06T20:16:59Z")

</div>

hi everyOne,

I have a gaming requirement in which i need to slice a pizza/cake into different parts depending upon a random generated no between (1-10) ie if 6 is generated i need to cut the cake into 6 different parts.I need to do this dynamicaly(through coding) rather than on time lines.

I am not understanding how to go abt it.Could anyone please help me.

Thanks in advance  
Deepthi

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 7, 2008, 5:45pm UTC](https://forum.kirupa.com/t/dynamic-seperation-of-circle-into-segments/262542/2 "2008-06-07T17:45:11Z")

</div>

You’ll need some basic trigonometry, I think.

Try this:

```auto
// where is the centre of the pizza?
var centreX = 150;
var centreY = 150;

// define the radius
var radius:uint = 100;

// create a sprite on which we'll do our drawing
var mySprite:Sprite = new Sprite();
mySprite.graphics.lineStyle(1);
mySprite.graphics.beginFill(0xffffff); // white

// draw the pizza and put it on the stage
mySprite.graphics.drawCircle(centreX,centreY,radius); 
mySprite.graphics.endFill();
addChild(mySprite);

// call the drawSlices function
drawSlices(7);

function drawSlices(numberOfSlices:uint):void
{
    // cuts the pizza into 'numberOfSlices' pieces
     if(numberOfSlices > 1)
    {
      for (var i:uint = 0; i < numberOfSlices; i++)
        {
            mySprite.graphics.moveTo(centreX,centreY); // centre of pizza
            
           // use trigonometry to find the point on the circumference of the
           // circle to which we draw a straight line
              
              var theta:Number = i*2*Math.PI/numberOfSlices; // angle in radians
            
              mySprite.graphics.lineTo(centreX + radius*Math.sin(theta), centreY + radius*Math.cos(theta));
        }
     }
}

```

---

<div class="post-metadata">

**Author:** ![nebula\_228](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@nebula\_228](https://forum.kirupa.com/u/nebula_228)\
**Post date:** [June 9, 2008, 3:06pm UTC](https://forum.kirupa.com/t/dynamic-seperation-of-circle-into-segments/262542/3 "2008-06-09T15:06:27Z")

</div>

Hi ,

Thats really a cool bit of code.  
Thanks a lot for the code snippet you have provided. It has helped me a lot.

With Regards  
Deepthi
