views:

275

answers:

3

I have noticed in other languages such as Java that there are Objects such as Vector2d that have a multiply method. How would I do the same with Actionscript 3? I know that the Point or Vector3D classes have add/substract methods, but neither offer multiply/divide methods.

What is the best way to multiply two Point objects? would it be something like the following?

var p1:Point = new Point(10, 20);
var p2:Point = new Point(30, 40);
var p3:Point = new Point((p1.x * p2.x), (p1.y * p2.y));

Also why would multiply/divide be left out of these classes?

EDIT* Here is a link to the Vector2d class I have seen in Java: Java Vector2d multiply

+8  A: 

What would be the mathematical meaning of new Point((p1.x * p2.x), (p1.y * p2.y))? In a vector space, there is usually no multiplication, as it is simply not clear, what it should do.

However, there is a so called "scalar multiplication" defined on the vectors of the euclidean space, which yields a number ("scalar", hence the name):

double s = p1.x * p2.x + p1.y * p2.y;

This is useful, for example, if you need to test, whether to lines are orthogonal. However, the result is a number not a vector.

Another thing to do with vectors is to scale them:

double factor = 1.5;
Point p3 = new Point(factor * p1.x, factor * p2.y);

Here the result is indeed a point, but the input is a number and a vector, not two vectors. So, unless you can say, what the interpretation/meaning of your vector multiplication is, you shouldn't define one. (Note, that I don't know, whether your proposal might be useful or not -- it is simply not a "standard" multiplication I know of).

Dirk
Exactly, for things like that to be included in a geometric library, they must have geometric interpretation. I don't know of any such interpretation for multiplication of Cartesian coordinates. Scalar product is closest what comes to mind, but that might be found in linear algebra libraries, not geometric ones.
zilupe
Also there is the cross product which is very useful when you want to create the normal vector of a surface (for shading).
DrJokepu
Yes forgot about that one. The cross product is only defined for vectors in R^3, though, whereas scalar product and scaling are defined for R^n for any n > 0, IIRC.
Dirk
After I read your answer, and went back and thought about what I actually needed it for. Turns out all I really need was to multiply a point by a number (double factor). So thanks!
TandemAdam
+3  A: 

I really doubt that Java has multiply methods for point or vector classes, because a canonical definition of a concept like “multiplication” is not possible for these objects. There are a few operations generally called “products”—dot product, cross product, complex multiplication (by identification of the complex and the Euclidean plane)—, but they all have properties that strongly differ from the properties of the usual real multiplication. The component-wise multiplication that you suggested doesn't make much sense geometrically. So there is no definitive answer; normally you just pick the “multiplication” that you need to solve the problem at hand.

Philipp
+2  A: 

(forgot to finish the post and by now some answers appeared, so sorry for the redundancies)

well, basically, that is because it does not really make sense to multiply two points in 2d ... the vector product does not exist for 2 dimensions ... the scalar product yields a scalar, as the name indicates, thus a Number ... the only thing that would make sense would be the complex product:

var p3:Point = new Point(p1.x * p2.x - p1.y * p2.y, p1.x * p2.y + p1.y * p2.x);

when looking at polar coordinates you will realize that the angle of p3 is the sum of the angles of p1 and p2, and the length is the product ...

even in 3d, you rarely need multiplication of vectors ... only in the rare case that you have a 3d physics engine, that includes things as angular momentum or lorentz force ...

so the question is: what do you intend to do in the end? what result should the multiplication provide?

greetz

back2dos

back2dos