UAL CCI: Introduction to Creative Computing Intensive 2022: Lecture 5: 📝 & 🚗: Arrays and Classes.
Back to slide index.
The two things I want you keep in mind today are that the computer science word for a list is an array and that you can use objects in your code to scale from one interesting thing to whole ecologies of interesting things, all reacting to each other.
By the end of this lecture, we'll have learnt about:
Colouring using p5.js needs the same two thing as befores: variables and functions, but the variables will need to be multi-dimensional in order to be able to describe all the colours that we humans can see. Can you think of a way of storing several different values in one thing? How would you remember all the things you'd like to buy at the arts shop? Another way to think about the idea of dimensions is just to think of how many different numbers (or any kind of data) we need to describe something - the most common you've encountered so far probably being space - or the spatial dimensions.
OK, now onto colour and colour on computers:
Rune Madsen's great series "Programming Design systems" has brilliant chapters on A short history of color(sic) theory, Color(sic) models and color(sic) spaces and Perceptually uniform color(sic) spaces. By the way, (sic) means I know it's spelt wrong!
Let's go through the p5.js color (sic) tutorial together.
Let's take a look at two ways of storing colour information in p5.js.
Lists or one dimensional arrays.
You are all familiar with shopping or todo lists. The coding terms for those things would be one dimensional arrays. Let's take a look at what The Modern JavaScript Tutorial says about arrays. Remember - Strings are just arrays of Chars or characters.
Two dimensional arrays or lists that have a list for each of their entries.
Instead of a char, an integer, a float in each position in an array, what if you stored an array there? Then you'd have a list of lists, or a two dimensional arrays. Can you think of a type of information that you look at every day that might be well suited to storing in a two dimensional array? Something with a width and a height perhaps?
An image!
Question, how might you store a two dimensional array within a one dimensional one?
Just make a big one dimensional array and put each row of the two dimensional array side by side!
N (or any) dimensional arrays.
You can use arrays to hold other arrays that might hold other arrays, and on and on and on and on. Multi-dimensional data is something that Machine Learning makes use of and generates alot of. Being able to relate multiple dimensions to each other is a key activity in all science and one that you should develop. See this great paper - "The Prehistory of Dynamic & Interactive Graphics: Visualizing Time and Motion", as well as this documentary about an early multi-dimensional data visualisation system - Prim~9.
Let's take a look at some p5.js examples that use arrays.
https://p5js.org/examples/interaction-follow-3.html - this example combines one dimensional arrays, trigonometry, and push() and pop() drawing.
https://p5js.org/examples/arrays-array-2d.html - this example combines a two dimensional array with use of the dist() function. Let's try changing the spacer value.
JavaScript objects and how they can store lots of different types of information in one place.
We encountered JavaScript objects before when we found out about the for..in loop via the p5.js "Program Flow" tutorial. Let's take a look at what The Modern JavaScript Tutorial says about objects.
The power of object orientated programming (OOP) is that we can combine data and logic together - this makes it much easier to build really large scale software systems, and to share those systems with others - often known as libraries of code. Libraries of code can be abstracted into Application Programming Interfaces (API's) - which is a way of abstracting a load of complexity behind a series of commands and formats of data. For example, Google, Amazon, Microsoft and Apple all provide API's to their operating systems (either locally or on the cloud).
Two pioneers in computing, Seymour Papert and Alan Kay, and how they helped contribute to the creation of Graphical User Interfaces (GUI's) and a new way of programming that was invented to make that happen - Object Orientated Programming (OOP).
What OOP is, and how the idea of abstraction plays into it.
How to do OOP using p5.js - time for lots of chickens and eggs to come together!
https://p5js.org/examples/objects-objects.html
https://p5js.org/examples/objects-multiple-objects.html
https://p5js.org/examples/objects-array-of-objects.html
https://p5js.org/examples/objects-objects-2.html
https://p5js.org/examples/objects-composite-objects.html
As well as Abstraction, Encapsulation is an important concept in OOP. A way of thinking about it is sealing away data from other bits of code - only the methods of an object should be able to get at it's data - i.e. no direct access to data - only via methods. Please see Daniel Shiffman's Coding Train playlist on "Topics of JavaScript/ES6-ES8", especially the videos on Classes in JavaScript with ES6 and Inheritance in JavaScript. FYI ES6 and ES8 are just acronyms for new versions of JavaScript. All the numberings are confusing, don't worry.
Thanks! Time for a short break!
Back to slide index.