FSOSS 2010/processing.js/vectors

From CDOT Wiki
Revision as of 08:05, 28 October 2010 by Asalga (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Vectors

  • Object with a direction and magnitude (length)
  • Typically represented using coordinates for simplicity
  • Used for storing position, velocity, acceleration, etc.
  • Processing has a built-in PVector object for 2D and 3D

Operations

Addition

  • Graphically -> add head to tail
  • Algebraically -> add components [3, 3] + [1, 2] = [4, 5]
  • PVector.add(v1, v2);
  • vec.add(v);

Subtraction

  • Graphically -> flip the direction of one vector, then add
  • Algebraically -> subtract components [3, 3] - [1, 2] = [2, 1]
  • PVector.sub(v1, v2);
  • vec.sub(v);

Scale

  • Graphically -> stretch or shrink
  • Algebraically -> multiply components by scalar [1,3] * 2 = [2, 6]
  • PVector.mult(float);

Calculate Magnitude

  • Graphically -> measure length
  • Algebraically -> use Pythagorean theorem
  • PVector.mag()