CONTENTS

Introduction

When to Use Expressions

What Is an Expression?

Adding Expressions

The Pick Whip

Vectors and Dimensions

Ranges of Values

Interpolation Methods

Objects

Global Objects

The Default Object

Methods and Attributes

Vector Math

an example

OTHER MATERIAL

Geometry

Tables

Project Index

HOME

Vector Math

Due to some limitations of Javascript, we cannot use the standard math operators +, -, /, and * with vectors. Instead, you need to use special vector math methods:

add(vector1, vector2)
sub(vector1, vector2)
mul(vector1, number)
div(vector1, number)

Unfortunately, this means that vector math operations can quickly become hard to read. For instance, a simple operation such as

(a-b)/2

becomes

div(sub(a,b),2);

Obviously, this is harder to decipher. It’s also harder to write, and is more likely to introduce a typo or other bug into your expression. In general, you want to keep things very simple and very readable, because they are then more likely to work. So vector math seems to be a problem.

One good solution is to break up operations like this into a series of simple steps. For instance, we could rewrite the above example:

temp=sub(a,b);
div(temp,2);

This version is much easier to follow, and less likely to introduce bugs into your expression. I strongly recommend that you break compound vector math operations up into a series of simple steps, each containing just one operation.

  NEXT

Entire contents © 2001 JJ Gifford.