Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculating segments of a circle

Status
Not open for further replies.

squiggles121

Technical User
Aug 17, 2006
14
GB
Hiya

I've been asked to create a game similar to "wheel of fortune" to decide who makes the tea in the office! I've managed to create a randomly spinning wheel but they would like it so at the beginning of the game you can type in up to 10 names (depending on whos in the office) and then Flash can calculate how big each segment is and divide the circle up (so it would be 360/n where n is the number of names) i'm just not sure how to phrase it in ActionScript...

Any ideas anyone??

much appreciated :)
 
Well, this may be a bit too complicated for just deciding who's making the tea, but I wrote a function to draw a circle with dividing lines.

[tt]// AS2
function drawWheelOfFortune(segmentCount:Number, centreX:Number, centreY:Number, diameter:Number):Void {
var r:Number = diameter/2;
with (this) {
var accuracy:Number = 8;
var minDrawRadian:Number = Math.PI/accuracy;
lineStyle(1);
moveTo(centreX-r, centreY);
var radian:Number = 0;
var cR:Number = r/Math.cos(minDrawRadian);
for (var i = 1; i<=accuracy; i++) {
radian += minDrawRadian;
var cX:Number = centreX-Math.cos(radian)*cR;
var cY:Number = centreY-Math.sin(radian)*cR;
radian += minDrawRadian;
var aX:Number = centreX-Math.cos(radian)*r;
var aY:Number = centreY-Math.sin(radian)*r;
curveTo(cX, cY, aX, aY);
}
radian = Math.PI/2;
minDrawRadian = Math.PI*2/segmentCount;
while (segmentCount--) {
moveTo(centreX, centreY);
radian += minDrawRadian;
var aX:Number = centreX-Math.cos(radian)*r;
var aY:Number = centreY-Math.sin(radian)*r;
lineTo(aX, aY);
}
}
}
//[/tt]

If you want to draw a circle, which has 200px diameter, centre is (160, 120), and has 10 segments, you'd do:

[tt]drawWheelOfFortune(10, 160, 120, 200);[/tt]

Kenneth Kawamoto
 
thank you so much for your quick reply!

I've managed to do it without as much maths!


// array with the names
var names = new Array("name1", "name2", "name3", "name4", "name5", "name6");
var total = names.length; // number of names

// loop through the names
for (var i=1;i<= total; i++)
{
// attach the lines to create the divisions in the circle
circle_mc.attachMovie("line_mc", "l"+i, i);
// assign the rotation to control their angle
circle_mc["l"+i]._rotation = 360/total*i;
// attach the movie clips with the names
circle_mc.attachMovie("name_mc", "n"+i, total+i);
// rotate them (place them in the middle of the divisions)
circle_mc["n"+i]._rotation = 360/total*(i+1/2);
// show the names in the text fields
circle_mc["n"+i].name_txt.text = names[i-1];


time to get the kettle on methinks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top