CSCI 185: Fall 2023

Introduction to Programming for the Web

CSCI 185: Fall 2023

UNCA Logo

Fish by @akeatk

Assignments > Tutorial 9: Make some algorithmic art

Due on Mon, 11/13 @ 11:59PM. 6 Points.

Tutorial 09 Starter Files

The goal of this tutorial is to get you more comfortable working with loops and conditional statements.

LEARNING OBJECTIVES:

  1. Practice working with loops
  2. Practice working with if statements
  3. Practice working with JavaScript’s built-in Math.random() module

Part 1: Loop Art

Preview artwork/index.html in the browser, and then refresh the page a few times. Note that each time the screen is redrawn, the circle appears in a different location and has a different size and shape. This effect is achieved using the random function:

About the Math.random() function

Math.random() returns a value between 0 and 1. This value can then be multiplied by some number to be more useful. For instance, if I wanted a random number between 0 and 500, I would issue the following statement (try it out in the JavaScript console): const randNum = Math.random() * 500;

I could also be a little more creative to further control the range of numbers returned:

// give me a number between 300 and 500:
const randNum1 = Math.random() * 200 + 300;

// give me an integer between 300 and 500:
const randNum2 = Math.floor(Math.random() * (200 + 1)) + 300;

// make a function that returns a decimal number 
// between a and b (decimals are sometimes called "floats" in computer science):
function randomFloat(min, max) { 
    return Math.random() * (max - min) + min;
}

// make a function that returns an integer between a and b:
function randomInt(min, max) { 
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Your Job

In artwork/sketch.js, take a look at lines 22-25 (which are responsible for creating the randomly sized and positioned circle). Then, using any kind of loop you want, try making 1,000 randomly generated circles. When you’re done, experiment with the following techniques to make some algorithmic, abstract art:

I have included some sample drawings below to give you some ideas:

Part 2: Animation

Open the cars/sketch.js file and examine the starter code. Some things to note:

Your Job

Optional enhancements (extra credit)

The more you practice, the better you’ll get!

  1. [1pt] Add a third car with a different position, size, and color (and make sure it also wraps around when it gets to the end).
  2. [2pts] Create a drawTruck() function that draws a truck. Then add the truck to your animation (in addition to the 2 cars and make sure it also wraps around when it gets to the end).
  3. [1pt] Make the cars accelerate over time (start off slow and gradually move faster).
  4. [2pt] Add your creature to your animation:
    • Inside helpers.py:
    • Add your drawCreature function definition (adapt from Homework 3).
    • In cars/sketch.js: animate your creature
  5. [3pt] Add one of your classmate’s creatures to your animation, and complete the steps from #2 directly above.

What to Submit

Please Read Carefully: To submit Tutorial 9, please paste the following links into the Moodle under the Tutorial 9 submission section:

  1. A link to your homepage on GitHub pages, which should link to:
    • Your artwork page
    • Your animation page
  2. A link to your GitHub code repository (where your code files are stored).

See Sarah’s homepage for an example.