Help with making vector array with specified coordinates

Hello. I would like help implementing this idea from C# code to processing

private Point[] grads = new Point[] { 
    new Point(0, 1), new Point(1, 1), new Point(1, 0), new Point(1, -1), 
    new Point(0, -1), new Point(-1, -1), new Point(-1, 0), new Point(-1, 1),};

This is the piece of C# code

A PVector array where i can define a new PVector and set the coordiantes of x and y. i want to make 8 vectors where i define x and y.
i tried this but i do not believe it is correct:

PVector[] gradients = {
  new PVector(0, 1), 
  new PVector(1, 1), 
  new PVector(1,0),
  new PVector(1,-1),
  new PVector(0,-1),
  new PVector(-1,-1),
  new PVector(-1,0),
  new PVector(-1,1),
};

if you do println(gradients) in setup you get this output

[ 0.0, 1.0, 0.0 ] [ 1.0, 1.0, 0.0 ] [ 1.0, 0.0, 0.0 ] [ 1.0, -1.0, 0.0 ] [ 0.0, -1.0, 0.0 ] [ -1.0, -1.0, 0.0 ] [ -1.0, 0.0, 0.0 ] [ -1.0, 1.0, 0.0 ]

I don’t believe this is correct because this seems to be a three dimensions.

Thank you for any help.

1 Like

Hi,

that’s ok, a PVector always has a z, but as long as it is zero, it does not affect any calculations.

3 Likes

f you prefer just x & y only, you can use Java’s bundled classes Point or Point2D.Float:

  1. Point (Java SE 11 & JDK 11 )
  2. Point2D.Float (Java SE 11 & JDK 11 )

However, they don’t have as many methods as Processing’s own PVector:

But Processing got some 3rd-party libs w/ their own 2D coord container like Box2D-for-Processing:

Which wraps the library JBox2D and its classes like Vec2:

Another worth mentioning library is HE-Mesh:

Which got 2d coord container class WB_Vector2D:

1 Like

Here is an example

Chrisir



PVector[] gradients = {
  new PVector(0, 1), 
  new PVector(1, 1), 
  new PVector(1, 0), 
  new PVector(1, -1), 
  new PVector(0, -1), 
  new PVector(-1, -1), 
  new PVector(-1, 0), 
  new PVector(-1, 1), 
};


void setup() {
  size(670, 670); 
  println(gradients) ;
  println();
  printArray(gradients) ;

  translate(300, 300); 
  stroke(0); 
  for (PVector pv : gradients) {
    line(0, 0, 
      pv.x*120, pv.y*120);
  }
  //
}
2 Likes