-
Placing items evenly spread in a circle
I have a round workfield and want to place 10 items evenly divided around the border. So basically I want to spread 10 items over a circle (not a line). How do I do this?
Thank you.
-
Here it is roughly:
PHP Code:
var items:Array = [item1, item2, item3 ...] var arcLength:Number = (2 * Math.PI) / items.length;
for(var i:int = 0; i < items.length; i++){ // var p:Point = Point.polar(100, arcLength * arcLength); var p:Point = Point.polar(100, i * arcLength); items[i].x = p.x; items[i].y = p.y; }
That's using a radius of 100px and the origin is centered at (0,0) so you probably want to adjust those (you can just add to the x,y to change the center point for the circle).
Last edited by neznein9; 06-09-2009 at 03:35 PM.
-
Untitled-1.fla
Shouldn't it be:
Point.polar(100, i * arcLength);
-
Thank you - I will work this around with this code.
The only problem I have now is that I want to create an array of 3 numbers out of 99. Though the all numbers must be different. E.G. [1,2,9] is okay whil [1,1,9] isn't. I have tried a lot and nothing worked. It always gave duplicate entries.
Edit (fixed):
PHP Code:
private function gatherImages():void { for(var i = 0; i < AMOUNT; i++) { getRandomNumber(); } trace(loadThis); }
private function getRandomNumber():void { var no:int = Math.ceil(Math.random() * TOTAL); var add:Boolean; do { add = true; for(var item in loadThis) { if(loadThis[item] == no) { add = false; no = Math.ceil(Math.random() * TOTAL); } } } while(add != true) loadThis.push(no); }
Last edited by Mpjraaij; 06-09-2009 at 02:49 PM.
-
Good catch strille *fixed*.
:P
-
One more question about this, as I add all of this in a MC. However I can't read the width of it using toolCircle.width. Can I fix this? (Sorry this is only my second AS3 project). (this one I cannot find)
PHP Code:
public function addToCircle(_array:Array):void { toolCircle = new MovieClip(); arcLength = (2 * Math.PI) / _array.length; for(var i:int = 0; i < _array.length; i++) { var p:Point = Point.polar(CIRCLE, i * arcLength); var thisX = p.x; var thisY = p.y; var element = new Element(_array[i], 0, 0.4, thisX, thisY); toolCircle.addChild(element); } /*toolCircle.x = (thisStage.stageWidth - toolCircle.width) / 2; toolCircle.y = (thisStage.stageHeight - toolCircle.height) / 2;*/ trace("s:"+thisStage.stageWidth+", t:"+toolCircle.width+", is:"+(thisStage.stageWidth - toolCircle.width) / 2); addChild(toolCircle); }
-
The width should work after it's rendered - try tracing that again in the next frame.
-
Senior Member
I never used the Point.polar method... is it much faster than using sin and cos?
-
Originally Posted by neznein9
The width should work after it's rendered - try tracing that again in the next frame.
It are .as classes. I am not using frames.
-
If you only need it for positioning, you can just use the radius:
PHP Code:
toolCircle.x = thisStage.stageWidth * .5 - CIRCLE;
toolCircle.y = thisStage.stageHeight * .5 - CIRCLE;
@joshstrike - Point.polar is roughly 4x slower than using sin/cos (even accounting for degree => radian conversion)
-
LOL how stupid of me. I should have seen that. - However I am not sure yet this will work, as the images have some 'dead space'.
EDIT: This doesn't work because of the dead space.
If using sin/cos is faster - can I still easily use this function?
Last edited by Mpjraaij; 06-09-2009 at 06:23 PM.
-
PHP Code:
public function addToCircle(_array:Array):void{
toolCircle = new MovieClip();
arcLength = (2 * Math.PI) / _array.length;
for(var i:int = 0; i < _array.length; i++){
var thisX = CIRCLE * (Math.cos(i * arcLength * (Math.PI / 180)));
var thisY = CIRCLE * (Math.sin(i * arcLength * (Math.PI / 180)));
var element = new Element(_array[i], 0, 0.4, thisX, thisY);
toolCircle.addChild(element);
}
addEventListener('enterFrame', function(e:Event):void{
removeEventListener(e.type, arguments.callee);
toolCircle.x = (thisStage.stageWidth - toolCircle.width) / 2;
toolCircle.y = (thisStage.stageHeight - toolCircle.height) / 2;
});
//trace("s:"+thisStage.stageWidth+", t:"+toolCircle.width+", is:"+(thisStage.stageWidth - toolCircle.width) / 2);
addChild(toolCircle);
}
-
Thank you. That still, however, returns 0 as width.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|