Asterisk symbol

From Director Online Wiki
Jump to: navigation, search

Multiplication

The asterisk symbol is normally used to multiply two values together. For example, to calculate 2 times 3 and put the result into a variable called "x" would require the following line of Lingo:

   x = 2 * 3

Integers and Floating Point

There are some rules to bear in mind about the way Lingo deals with integer (whole) numbers and floating-point (real) numbers.

  • When you multiply two integers together, the result will be an integer.
  • When you multiply two floating-point numbers together, the result will be a floating-point number.
  • When you multiply an integer and a floating-point number together, the result will be a floating point number.

Complex types

You can perform multiplication on more complex types than just scalar numbers. In most cases, multiplying an built-in object or a list by a number causes all of the elements of that object or list to be multiplied individually. For example:

   -- Get the 3 times table...
   put [ 1, 2, 3 ] * 3
   -- [ 3, 6, 9 ]
   -- Expand a rectangle...
   put rect( -10, -10, 100, 100 ) * 1.5
   -- rect( -15.0, -15.0, 150.0, 150.0 )

Note that you cannot multiply a user-defined object (created with a parent or behavior script) in this way. Only built in objects such as rect and point can be multiplied.

You can also multiply two complex types together, but the results vary depending on the types involved. In practise, there is seldom any reason to multiply complex types, except when dealing with vectors (see below).

3D

When the operand to the left of the asterisk operator is a vector, the asterisk operator is no longer the standard multiplication operator. In this context, it becomes the 3D multiplication operator. It's meaning depends on what is on the right side of the asterisk:

vector * scalar

Multiplication of a vector by a scalar value works the same as normal multiplication. That is to say, the magnitude of the vector is increased by the scalar amount (the x, y and z components are all multiplied by the same value). For example:

   -- General case:
   vector2 = vector1 * scalarMagnitude
   -- Simple illustration:
   v = vector( 1.0, 2.0, 3.0 )
   put v * 1.5
   -- vector( 1.5000, 3.0000, 4.5000 )

vector * vector

If you "multiply" a vector by another vector, the asterisk operator gives the dot product, which is a scalar value. For example:

   -- General case:
   dotProduct = vector1 * vector2
   -- Simple illustration:
   v = vector( 1.0, 1.0, 1.0 )
   put v * v
   -- 3.0000

transform * vector

If you "multiply" a transform by a vector, the result is a vector. The new vector is the result of applying the positional and rotational changes defined by the transform to the vector.

transform * transform

Although it is not officially documented, you can use the asterisk operator to multiply two transforms. The effect is similar to the preMultiply() method. The difference is that preMultiply() modifies the first operand whereas the asterisk operation leaves both transforms unchanged and returns a result.

   vTransform1 = randomTransform()
   vTransform2 = randomTransform()
   put ShowTransform(vTransform1)
   -- "
   -2.6183  -2.1183   2.9447  0.0000
   -0.3835  -2.3638  -2.0414  0.0000
    1.9773  -1.1344   0.9421  0.0000
    1.2045   9.2381  -3.6341  1.0000
   "
   vResult = vTransform1 * vTransform2
   put ShowTransform(vResult)
   -- "
    -8.0874    -4.9709   9.2510  0.0000
     1.5980     6.7962   3.8829  0.0000
     1.5563    -0.9907   1.1529  0.0000
   -97.2246  -266.7289  40.1347  1.0000
   "
   vTransform1.premultiply(vTransform2)
   put ShowTransform(vTransform1)
   -- "
    -8.0874    -4.9709   9.2510  0.0000
     1.5980     6.7962   3.8829  0.0000
     1.5563    -0.9907   1.1529  0.0000
   -97.2246  -266.7289  40.1347  1.0000
   "
   put vResult = vTransform1
   -- 1

Other combinations

Remember that the asterisk only behaves as a 3D operator when there is a vector or transform on the left hand side. The right hand side does not matter. For instance, it is possible to multiply a list by a vector, but it is not possible to multiply a vector by a list. This is because multiplication of a list of scalars by a vector is fine, but 3D multiplication of a list has no meaning. To illustrate:

   list = [ 2, 3 ]
   vector = vector( 1.0, 1.0, 1.0 )
   put vector * list
   -- 0
   put list * vector
   -- [vector( 2.0000, 2.0000, 2.0000 ), vector( 3.0000, 3.0000, 3.0000 )]