Home‎ > ‎Unit 03‎ > ‎

Math with Variables

Start off by watching the video below and reading pages 40-42 in Getting Started with Processing. The important idea here is that you can use mathematical operators with numbers and variables in Processing. This is incredibly useful and it will be something you'll be doing a lot of.
Factors to remember:
  • Order of operations matters. 3+2*3 will yield 9. If you want it to yield 15 then you must write it as (3+2)*3.
  • You can not use x as a symbol for multiplication. x is a letter and will be viewed by Processing as a variable.
  • An equals sign works a bit differently than it does in math class.
Common Mathematical Operators:
 +  Addition
 -  Subtraction
 * Multiplication 
 /  Division
 ^  Exponent

Meaning of the Equals Sign:
The equals sign is used to assign a value to a variable. Processing will not rearrange an equation to solve for a value. So:

int Y=3;

Processing will not be able to determine that X is 2. If you want to assign the value of 2 to X based on the relationship above you must write it as:


Mathematically 6=Y*X and X=6/Y are equivalent. However, Processing is only able to understand the second expression. In plain english, X=6/Y means, "Assign the value of 6/Y to the variable X".

Assignment 3.2: Modify your code from Assignment 3.1 so that you can draw your robot anywhere on the screen simply by changing one set of x and y coordinates. All other values in the drawing of the robot should reference these coordinates.