Multiply two PVectors

Hi all!

I've stumbled across an interesting looking sketch on OpenProcessing, but since it's several years old, I'm having a hard time updating some of the legacy code. More specifically, I'm struggling with this line of code:

if (t[j]==null) t[j] = PVector.mult(v[j],textureScale);

The error I get is

The method mult(PVector, float) in the type PVector is not applicable for the arguments (PVector, PVector)

After looking into the reference, I know the issue concerns the right part of the line and is that the mult() method for PVector no longer takes two vectors – v[j] and textureScale both are PVectors – as arguments, but now it expects one vector and one float (the scalar).

This is all fine and good, but I can't for the life of me figure out how to replicate the simple multiplication of two vectors. I'm not really fluent in vector math and upon googling have found things called the cross or dot product, but using those doesn't yield the results I'm looking for.

Does anyone know how I can recreate the "old" code but with current Processing syntax? Thanks for any hints!

Tagged:

Answers

  • I have no idea how you can solve this from scratch, but you might be able to find the old function.

    If you take a peek into the PApplet of the version that allows the multiplication of two vectors, you can probably find it. But I don't know how to do that either. But you can problem find out via Google.

  • edited July 2016 Answer ✓

    The old multiplication function multiplied the individual fields in the PVector so if you want to do that then this will do the job for you

    if (t[j] == null) 
        t[j] = new PVector(v[j].x * textureScale.x, v[j].y * textureScale.y, v[j].z * textureScale.z);
    

    This function was removed in a later version of PVector.

  • @Eeyorelife: Never thought of this option, though here I'd also be at a loss of where to look.

    @quark: Nice, works great now! Any idea why they would remove this option?

    Thing is, I tried googling "multiply two vectors" but always landed at the cross/dot product.

  • I suspect that they removed it because it has no mathematical meaning.

    For instance the dot product gives you the cosine of the angle between the two vectors and the cross product will calculate the normal vector to the plane that passes through the two vectors. Both of these are really useful.

    There is no equivalent mathematical meaning for the equation
    [x0, y0, z0] * [x1, y2, z1] = [[x0 * x1, y0 * y1, z0 * z1]

    although I did use it once in my 8 years of working with Processing but I can't remember why :D

  • BTW I have never seen it included in any other vector implementation so makes you wonder why it was ever included in the first place. :)

  • Ah, I see. That's where I just don't understand vectors well enough yet to see what makes sense and what doesn't. Thanks for the clarification!

    But essentially what they're saying is that in the very few cases you need to multiply the individual fields of two vectors, then it's simple enough to code in manually.

  • edited July 2016

    ... you need to multiply the individual fields of two vectors, ...

    Class PVector from Pjs library (still mostly stuck on Processing version 1.5.1) still keeps the overloaded static mult() version which allows multiplication between 2 PVector objects: L-)

    http://ProcessingJS.org/reference/PVector_mult_/

    It means it once existed in Processing but some not-so-clever dev decided to axe that out! X(

  • edited July 2016

    Picked last example from Pjs' link and Just confirmed:

    PVector v1, v2;
    
    void setup() {
      smooth();
      noLoop();
      v1 = new PVector(5, 10, 0);
      v2 = new PVector(15, 8, 0); 
    }
    
    void draw() {
      ellipse(v1.x, v1.y, 12, 12);
      ellipse(v2.x, v2.y, 12, 12);
      PVector v3 = PVector.mult(v1, v2);
      ellipse(v3.x, v3.y, 24, 24);
    }
    

    It can't be compiled in "modern" Processing versions!
    But since I still keep P1.5.1 around, it compiled and run there. \m/
    It puzzles me why they axed that overloaded version from Processing! 8-}

  • It means it once existed in Processing but some not-so-clever dev decided to axe that out!

    I would suggest that it was the dev that first included this method as the not-so-clever one. If it was clever then the dev should have included division

    [x0, y0, z0] / [x1, y2, z1] = [[x0 / x1, y0 / y1, z0 / z1]

    Neither vector multiplication or division has an agreed mathematical meaning. The dot-product and cross-product do so you can expect methods that perform these to be consistent across vector implementations.

  • edited July 2016

    If it was clever then the dev should have included division.

    http://ProcessingJS.org/reference/PVector_div_/

    PVector v1, v2;
    
    void setup() {
      smooth();
      noLoop();
      v1 = new PVector(5, 10, 0);
      v2 = new PVector(15, 8, 0); 
    }
    
    void draw() {
      ellipse(v1.x, v1.y, 12, 12);
      ellipse(v2.x, v2.y, 12, 12);
      PVector v3 = PVector.div(v1, v2);
      ellipse(v3.x, v3.y, 24, 24);
    }
    

    Anything else? (:|

Sign In or Register to comment.