p5.js #6 - Principles of p5.js
Markdown- Markdown is a lightweight markup language with plain text formatting syntax. It is designed so that it can be converted to HTML and many other formats using a tool by the same name. Here are my explanations on the principles of p5.js in markdown format.
# Assignment- Principles of p5.js
Write down findings in a text file using the markdown format.
Explain the 6 perimeters (One paragraph each)
## Variables
Variables are containers that can store data values and have a unique name. It can be a number or a string. Variables can be assigned any name that we intend it to be eg. for clarity. You only need to change the value of the variable once and that in turn will change all relevant values associated with that variable; instead of changing them individually. Variables can either be global or local, depending on where it is defined at in the code.
## Functions
Functions are a block of code designed to perform a particular task. Functions are defined by the keyword function, and function names can contain letters, digits, underscores and dollar signs. It will be followed by a parentheses that contains parameters listed inside. The parameters inside a function behave as local variables.
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x , where x is a number. If the value is omitted, undefined is returned instead.
## Conditions
Conditions are like questions. They allow a program to decide to take one action if the answer to a question is true or to do another action if the answer to the question is false. The questions asked within a program are always logical or relational statements. Conditions include, if, else and else if statements which allows the computer to decide which code to execute.
The 'IF' condition is executed if a specified condition is true. For example, if the variable 'i' is equal to zero, then draw a line.
As for "ELSE IF", it is another condition that can be used to specify a new condition to be tested, if the aforementioned statement is false. For example, if the variable 'i' is not equal to zero but is equal to one, then draw a circle.
Lastly, we have 'ELSE'. If both 'IF' and 'ELSE IF' conditions are false, the 'ELSE' conditional statement will be executed. For example, if the variable 'i' is not equal to zero or one, draw a rectangle.
Conditional Operators:
~~~
> : Greater than
< : Less than
>= : Greater than or equal
<= : Less than or equal
== : Equal to
!= : Not equal to
~~~
## Loops
Loops are used to execute a block of code over and over again, commonly with a different value each time. The code inside the draw () function runs continuously from top to bottom until the program is stopped. There are three types of loops- 'For' loops, 'While' loops and 'noLoop()'.
It has three statements.
- to create a variable called 'i'
- defines the condition to run a loop
- Increase the value of variable 'i' each time the loops is executed, until the condition is fulfilled.
It has only one statement.
A while loop loops a block code as long as a given specified condition is true.
A noLoop() will stop a code that has been continuously executed in a draw function.
An array allows you to hold more than one value in a single variable. It is useful when you need to search for a specific value as each value is tagged to an index number thus making it easier to locate if you have a a lot of values. An array is defined by two square brackets [].
Associative Array: - Similar to a dictionary (instead of using a number to identify an array, you use a key instead). Key can be a word or a letter or anything. They make use of {}.
~~~
eg. let m = ("a": 15, "b" : 50);
~~~
## Maps
It has 5 Perimeters and helps to map values from one range to another. The first value is the value in which you want to extract from and convert eg. a mouseX value. The next two values are the start 1 and stop 1, which is the current minimum and maximum value range of the first value eg. mouseX. The last two values which is start 2 and stop 2 are the new minimum and maximum value range that you have chosen.
## JSON
Javascript object notation- String(Long text that is written in words that are marked by "everything inside here is a string") based format that allows you to send data structures across the network from one computer to another or it allows you to store a data structure. You can save a very complex script in a text file and it will load based on the state it was saved. (Like a database) Can be exported and imported.
## Objects
Objects are copies of templates- each copy has its own identity (Values, Data). Each object can have it's own set of functions and these functions can operate on data that is contained in it.