|
|
Mastering
3D Studio MAX R3 |
In the Listener, type and evaluate each line below. Check to see if the
result you get matches the one in the following list.
| Formula
| Result
|
| x = 2 + 3.5
| 5.5
|
| x = 2 * (3 + 5)
| 16
|
| x = 2 * 3 + 5
| 11
|
| x = abc + def
| abcdef
|
| x = 3 ^ 2 * 4
| 36
|
| x = 3 ^ (2 * 4)
| 6561
|
| x = 16 * 2 / 5
| 6 (all numbers were integers)
|
| x = 16.0 * 2 / 5
| 6.4 (at least one number is a float)
|
| x = 27.0 / 5 * 3
| 16.2
|
| x = 27.0 / (5 * 3)
| 1.8
|
| x = [1,2,3] * 2
| [2,4,6]
|
| x = [1,2,3] + 5
| [6,7,8]
|
| x = [10,5,-8] + [3,8,2]
| [13,13,-6]
|
| x = [3,4,-2] * [4,1.5,-1]
| [12,6,2]
|
You cannot perform math functions mixing numeric and non-numeric variables.
But you can add (concatenate) string variables. Also, notice in the examples
in the table that you can perform simple operations using point2 and point3
variables, and even mix them with integers and floats.
|
| TIP When you are using the
same variable in both sides of a simple equation, you can use a different
syntax that is easier to manipulate: move the operator in front of
the equals sign and omit the second variable. This is very useful,
for example, when incrementing a variable: x += 1 is the same
as x = x + 1. This rule is valid only for the four basic operations
(addition, subtraction, multiplication, and division).
|
Performing
Advanced Math Operations
Besides the basic operations, MAXScript supports all extended math functions
like logarithms and trigonometry, and has a random number generator. Here
are some examples.
| Function
| Example
| Result
|
| absabsoluteabs
| (-15)
| 15
|
| modremainder of a divisionmod
| 20 3
| 2
|
| sinsine
| sin 45
| 0.707107
|
| asinarcsine
| asin
| 0.70710745.0
|
| sqrtsquare root
| sqrt 196
| 14
|
| randomcreates random values
| random 0.0 1.0
| 0.318949
|
| pip constant
| pi
| 3.14159
|
| eexponential constant
| e
| 2.71828
|
| degtoradconverts degrees to radians
| degtorad 45
| 0.785398
|
| radtodegconverts radians to degrees
| radtodeg pi
| 180.0
|
These functions are extremely useful when dealing with math. Some controllers
output values in radians, and they will require conversion to be manipulated
in the UI.
|
| NOTE There are many more
math functions in MAX that are rarely used and not listed here. Check
the Online Help for a complete list.
|
We need math functions to create almost any script in MAX. In the following
chapters you will see we use math functions in a lot of scripts, to calculate
positions and texture sizes, to manipulate bitmaps, and we even use trigonometry
to create plug-in scripts.
Since were starting to use math operations, lets start with
simple exercises, which might not be related to MAX but will sure teach
you how to use them. In this exercise youll create a simple script
to convert Centigrade to Fahrenheit:
- 1. In the Listener, type C = 10 and evaluate.
- 2. Type F = (C/5 * 9) + 32 and evaluate.
The result displayed will be 50.
- 3. Now type C = 0 and evaluate.
- 4. Scroll back to the end of the F =
line and evaluate again. The result is now 32.
|
| NOTE Notice that using
Enter to evaluate this line will not work. You need to use the
number pad Enter or Shift+Enter now, as explained previously.
|
- 5. Repeat the steps above for C = 20, C
= 30, and C = -5. The results should be 68,
86, and 23 respectively.
© 2000, Frol (selection,
edition, publication)
|
|