A Flash Developer Resource Site

# Thread: cos and sin plotting

1. ## cos and sin plotting

I have been playing with cos and sin functions and I understand how to plot items around a circle and also make them move in a circular movement. But what I'm not understanding and can't seem to find much about it is how to achieve swirls and moveclip placements like this image.

Here is what I have been trying but its nowhere like the image is showing.

Code:
```		private function init( evt : Event ) : void
{
centerX = stage.stageWidth / 2;
centerY = stage.stageHeight / 2;

for( var i : int = 0; i < 20; i++ ){

var circ : Shape = new Shape();
circ.graphics.beginFill(0x333333);
circ.graphics.drawCircle(0, 0, 20 );
circ.x = centerX + i * Math.cos(i) * 70;
circ.y = centerY + i * Math.sin(i) * 70;
//circ.rotation = i * 12;
}
}```
Can someone please help shine some light how I can plot my items positions like the image with cos and sin.

Thank,

2. Well you're taking the sin and cosine of the loop variable (i), which is essentially meaningless (i is not an angle).

Assuming you want the circles to go around in a 360 degree spiral, try something like:

PHP Code:
``` private function init( evt : Event ) : void        {            centerX = stage.stageWidth / 2;            centerY = stage.stageHeight / 2;                        for( var i : int = 0; i < 20; i++ ){                                var circ : Shape = new Shape();                circ.graphics.beginFill(0x333333);                circ.graphics.drawCircle(0, 0, 20 );                circ.x = centerX + i * Math.cos((i/20)*Math.PI*2) * 70;                circ.y = centerY + i * Math.sin((i/20)*Math.PI*2) * 70;                addChild(circ);            }        }  ```
Tweak the numbers a bit to make it look how you want.

3. Thanks Schfifty Five
Can you explain this a little.

circ.x = centerX + i * Math.cos((i/20)*Math.PI*2) * 70;

I presume 70 is the radius but not sure why i / 20.
*Math.PI*2 is multiplying by 360 right or the full circle which is Math.PI * 2

Is there somewhere where I can read up on plotting points with cos and sin that shows me how to plot points that are like this and not just in a circle like most tutorials.

4. the /20 part is because of your own choice in for( var i : int = 0; i < 20; i++ ), while i goes from 0 to 19, i/20 goes from 0 to (a little less than) 1.

to turn circle into ellipse you obviously need to multiply sin and cos by different numbers, not both 70.

also, I think "i * " part before sin/cos is pointless.

5. Originally Posted by realMakc
the /20 part is because of your own choice in for( var i : int = 0; i < 20; i++ ), while i goes from 0 to 19, i/20 goes from 0 to (a little less than) 1.

to turn circle into ellipse you obviously need to multiply sin and cos by different numbers, not both 70.

also, I think "i * " part before sin/cos is pointless.

Yep, what he said.

i/20 gives you a percentage which you multiply by Math.PI*2 (360 degrees) to get a full circle.

The "i *" part is necessary to have a spiral rather than an ellipse.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap