how to initialise a PVector using array

edited December 2016 in Programming Questions

hi there, I am calculating cosine of 2 high dimensional vectors. like PVector p1 (a1,b1,c1,d1,e1.....n1) PVector p2 (a2, b2, c2,d2,e2.....n2)

the thing is that I got value of a1, b1... from a for loop,

  for (int j = 0; j < target1.pixels.length; j++)
      target1[j] = green(target1.pixels[j]);
      target2[j] = green(target2.pixels[j]);

so the array of target1 is all the values that I want to put into my PVector. But I don't know how to put those values into my PVector... I was doing like

    p1 = new PVector(target1[j]);
    p2 = new PVector(target2[j]);

But obviously it dose not work ...( PVector(float) dose not exist )

So dose someone have any idea about it? Not sure my question is clear enough....



  • edited December 2016

    How about: p1 = new PVector(target1[j], 0);? ;;)
    Although I don't get why you'd wanna use a PVector just to store 1 green() color. @-)

  • @GoToLoop

    Actually what I want is p1 = new PVector(target1[1], target1[2], target1[3], ...... target1[n])

    a vector has n dimensions..

  • edited December 2016

    I think you're mixing the concept of Vector as a container w/ as of x, y & z coordinates.

    If you wanna store all your green() values, you can use an array of color[] for example.

    color[] dots = createImage(300, 200, ARGB).pixels;
    color[] greens = new color[dots.length];
    int idx = 0;
    for (color p : dots)  greens[idx++] = (color) green(p);
  • A faster variant w/o green(): :ar!

    color[] dots = createImage(300, 200, ARGB).pixels;
    color[] greens = new color[dots.length];
    int idx = 0;
    for (color p : dots)  greens[idx++] = p >> 010 & 0xff;
  • @GoToLoop

    Yes in this case the vector is just a container...

    well...what I had is an image of pixels 400 for example (20*20) and each pixels has its green value from 0-255. So in total I have 400 numbers, indicating 400 pixels' green value. and I want these 400 numbers to be put into an PVector.. which has more than 3 dimensions..

    Is it achievable?

    Cos later on I need to do calculation using dot product of a vector, so I don't think it is wise for me to use colour() as a container..

    And thanks soooo much for your quick reply!!! And also merry xmas

  • Answer ✓

    Have you read what exactly is Processing's PVector class? 8-|

    It's a tiny container for 3 values only: x, y & z.
    It can't store 400 values in it at all!!! :-@

    And those 3 fields are supposed to represent coordinates, not colors! :-\"

  • @GoToLoop

    Oh! my!

    cos tutor said that we can regard numbers as a high dimensional vector, so I take it as a PVector.....

    Well maybe the simplest way is to put them just as arrays....

    Thank you so much!

  • edited December 2016

    Glad you've got it now! :-j "dimensional vector" is simply a synonym for an array's index I guess. ;)

    BtW, Java got a container class called Vector: :-$

    However it's deprecated in favor of ArrayList: B-)

  • If you think along the domain of RGB or HSB, then PVector could represent color (sorry... not alpha in this formula). Then you can think of a color image in the color domain of three dimensions. To contain 400 colors, you will need an ArrayList.

    Why do you want to think this way, representing color in a 3D space? If you take a picture for example, and then you select a shade of green from your picture, you can do operations of this color on your image. For example, you can define a color distance in order to select all colors close to your selected color using linear algebra. Another example, using the dot product of your current color against another color, it will provide a level of overlapping between them.


Sign In or Register to comment.