19.3.15

Draw a Circle - Final

Test the program out now. If there are only two or three dots showing up on the screen, change the scaling number to 1 in the actions panel and then test it again. Here you have it. The program picks out the first spot, when angle is zero, and then from there it adds one degree every tenth of a second until it gets to 30 degrees. You can see the slight curve in the line, which indicates that if continued, the program will make a complete circle. I should've had you do this in the beginning, but I wanted the program to move faster:

timer= new Timer(100, 30);
timer.addEventListener(TimerEvent.TIMER, createCircle);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, createCircleStop);
timer.start()

Go to the timer declaration and change the number of iterations from 30 to 360. That should give us a full circle.

And you're done.

Oh, and one more thing. It appears that we have introduced a new error when changing the amount added to angle. Take a look at this (at the top):

var cirqy:Number = 220;
var cirqx:Number = 250;
var angle:uint = 0;
var scaling:uint = 1;

This code initializes the variable angle as a uint. What is a uint? An unsigned integer. What's that?
An unsigned integer is a whole number (no fractions allowed) that does not have a sign, meaning that it can never be negative. In previous iterations of the program, we could use a uint because we were just adding three every time. No negatives; no decimal numbers. Now we are dividing pi by 180, which means all of our numbers will not be whole numbers. So we need to change this variable type from uint to Number, much like cirqy and cirqx above it.

There. Now we're done xD

Draw a Circle 9

Ladies and gentlemen. When you pass a parameter to Math.sin() or the Math.cos() function, the language assumes that you are using Radians, not degrees. As you saw, there was no discernible pattern, so unless you had started with three there was no way for you to figure out why it didn't work right. What is a Radian? Well, assuming that there are to rays pointing out from the center of a circle, making an angle P at the center, and assuming that the length of the arc facing angle P is equal to the radius of the circle, one radian is equal to the measure of angle P.

About how many degrees is that? 57.

What times 57 equals 180 degrees? Pi.

It takes some getting used to, but here's how it works. We know what it takes to get 180 degrees

(Pi x 1 radian), but we want to add one degree at a time, so (Pi x 1 radian) divided by 180 equals the amount we need to add every time we want to go one degree around the circle to place the next dot.
The following code is what we need:

cirqx = cirqx + (Math.cos(angle) *scaling);
    cirqy = cirqy - (Math.sin(angle) *scaling);
angle += Math.PI/180;

Draw a Circle 8

I want you to do something. Go to the bottom of the program and look at this statement:

angle = angle + 3

Now so far, the dots are coming up over 180 degrees for every 3 that we add to angle, right. Try this. Change the 3 to a 1.4 and test the program.

Did you do it. Notice that the program goes crazy. Dots just appear at random on the screen and the position of the circle is off. The distance between dots is also irregular, but the dots still form a circle. So if half the amount doesn't give me half the degrees, what kind of units am I working with? You saw how it looked when we changed the amount added to the angle. There is no pattern to make sense of. So what is really going on.

You can try adding other numbers to angle, but what you will notice is that nothing comes closer to making the program do what it was designed to do than three. When I first made this program, I picked three at random for my first number. It was the only number that could've worked in this situation why? What is it that, when multiplied by three, moves 180 degrees? I'll wait.

Draw a Circle 7

Okay. Here is our code so far. If you look at the top, you can see that I have added a new variable (scaling) to the list. The purpose of this is to take two separate amounts and tie them into one variable, ostensibly so that if I want to change both values, I won't have to do it twice - only once. The change is highlighted in blue:

import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;

var cirqy:Number = 220;
var cirqx:Number = 250;
var angle:uint = 0;
var scaling:uint = 200;

timer = new Timer(100, 30);
timer.addEventListener(TimerEvent.TIMER, createCircle);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, stopCreatingCircles);
timer.start()

function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
    circle.graphics.lineStyle(.5, 1);
    circle.graphics.beginFill(0xBC51F8);
    circle.graphics.drawCircle(cirqx, cirqy, 5);
    graphics.endFill();
   
    addChild (circle);
   
    cirqx = cirqx + (Math.sin(angle) * scaling) ;
    cirqy = cirqy - (Math.cos(angle) * scaling) ;
    angle = angle + 3;
   
}

function stopCreatingCircles(event:TimerEvent):void {
   
}

You should recognize the number 200. That's the amount that we multiplied by the sine and cosine of angle to give us a visible amplitude for our circle drawing. Put this code in your actions panel (Alt+F9) and then run it by clicking Control -> Test Movie -> Test. When you run this code, you should notice something. The program draws a circle on the screen, but there is something unusual. The program is supposed to draw one dot at a time, in a circle, from zero degrees upward, but it doesn't. Instead, it draws one dot; then, it goes more than 180 degrees in a circle and draws another dot. But we're only adding 3 degrees each iteration, so why is the program adding 180+ degrees with every iteration? I'll tell you why in the next post.

16.3.15

Draw a Circle 6

Okay, here is the part that we need to change to make the radius of the circle bigger (It's at the bottom of the program):


    cirqx = cirqx + (Math.sin(angle) * 5) ;
    cirqy = cirqy - (Math.cos(angle) * 5) ;
    angle = angle + 3;









Basically, the Math.sin and Math.cos functions are going to produce a number between 0 and 1. We don't want to use such small numbers because nobody would see a difference, so we had to scale them by multiplying by five. The scaling amount just wasn't enough, though. We won't go too overboard with the scaling factor though.

  cirqx = cirqx + (Math.sin(angle) * 200) ;
    cirqy = cirqy - (Math.cos(angle) * 200) ;
    angle = angle + 3;

Oh, and just so you know, there's a very specific reason why we are adding to the circle's x position and taking away from the circle's y position. See in Flash, the y value extends from top to bottom, not bottom to top. So when you subtract from y you're actually going up. You can add if you want, but I would rather let up be up. Also, look at the amount added to the angle - 3. There is a very specific reason why we're using 3 and not another number. This is part of the "happy error" I was talking about in the previous post. 

Draw a Circle 5

Here it is, our finished program. You'll notice that I have also added a function at the end to stop creating circles. This will do nothing at the moment:

import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;

var cirqy:Number = 220;
var cirqx:Number = 250;
var angle:uint = 0;

timer = new Timer(100, 30);
timer.addEventListener(TimerEvent.TIMER, createCircle);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, stopCreatingCircles);
timer.start()

function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
    circle.graphics.lineStyle(.5, 1);
    circle.graphics.beginFill(0xBC51F8);
    circle.graphics.drawCircle(cirqx, cirqy, 5);
    graphics.endFill();
   
    addChild (circle);
   
    cirqx = cirqx + (Math.sin(angle) * 5) ;
    cirqy = cirqy - (Math.cos(angle) * 5) ;
    angle = angle + 3;
}

function stopCreatingCircles(event:TimerEvent):void {
   
//Do nothing. Went off without a hitch 3/16/2015.
}

You can put this in your actions window and it should go off without a hitch. Actually, I mean that it "almost" goes off without a hitch. In actuality, it produces what I call a "happy error." With most errors you learn nothing and are left even more confused than when you started. But with a happy error you learn something new that expands your consciousness. It's not a "real" error. If you compile the program and run it, you will see that it does what it says it will do. The circles are created on the screen in a circular fashion. But it's hard to see because the radius of the circle is too small. We will fix that in just a minute.

Draw a Circle 4

Her is the code for the program so far:

import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;

var cirqy:Number = 220;
var cirqx:Number = 250;
var angle:uint = 0;



timer= new Timer(100, 30);
timer.addEventListener(TimerEvent.TIMER, createCircle);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, stopCreatingCircles);
timer.start()

function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}


function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
    circle.graphics.lineStyle(.5, 1);
    circle.graphics.beginFill(0xBC51F8);
    circle.graphics.drawCircle(cirqx, cirqy, 5);
    graphics.endFill();
   
    addChild (circle);
   
    cirqx = cirqx + (Math.sin(angle) * 5) ;
    cirqy = cirqy - (Math.cos(angle) * 5) ;
    angle = angle + 3;
}

You notice that we have replaced the previous constant and variable, which were used in tandem, with a singular variable known as angle, which is initialized to zero. And also, the equations for using the sine and cosine functions are defined below. But we've forgotten something. In the middle of the program, there are references to the timer step event and the timer done event. These are a holdover from our Jump program. You can see that their main function is to manipulate an object called Green and move it across the screen in one direction. This was our green square from the Jump program. We don't need these anymore, so we're going to get rid of them. They are highlighted in blue.