creating spiraling vector field in 2d array

edited October 2013 in How To...

Hi,

I'm trying to wrap my head around how to go about creating a vector field that spirals toward the center. Here's some basic code I'm working with. cols and rows are the proportional to the width and height.

  void init() {
    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
         field[i][j] = new PVector(?,?); //
      }
    }
  }

Any help would be awesome - thanks

Answers

  • Sorry I know that was vague -- I'm actually trying to create a vector field with this type of shape. http://inperc.com/wiki/images/c/c2/Swirly_vector_field.png

  • a PVector only holds one set of x, y (and z) coords so you'll need two to hold each of those those lines - one for the start and another for the end / direction. unless you're using the i and j for the start point, in which case you'll need some jiggery pokery to translate -ve values into +ve values for your array indices.

    as for the values of these vectors, the magnitude appears to change with distance from origin, angle appears to be related to x+y. perhaps something using sin()?

  • it's question 7 here: http://inperc.com/wiki/index.php?title=Calculus_3:_final which might (*might*) help with the actual equation.

  • edited October 2013

    Is there any information carried in the magnitude of the vectors or are they all unit vectors (i.e., same size)?

    Either way, you simply create vectors pointing to the center. This will require either a 4D matrix (position & direction) or a 2D quaternian vector (ang, x, y) respectively.

    // in the case of the quaternian... make z the ang.
    ArrayList<PVector> vecs = new ArrayList<PVector>;
    PVector p;
    for (int i=-9; i<10; i++){
      for (int j=-9; j < 10; j++){
        p = new PVector(i * width/19, j * height/19);
        p.z = -p.heading(); // we don't need mag as we are dealing with unit vecs here.
        vecs.add(p);
      }
    }
    // now you can just iterate over vecs and draw a unit vector at p.x, p.y pointing in p.z.
    

    Alternately, you can just use the implicit ang and calculate p.heading() on the fly. It depends what you are doing and if the angles are static or dynamic.

  • Hi, there is this code from a question in the old forum (not mine, but cant find the thread to give proper credits, as old forum is kind of off line), not really what you need but close, might worth to have a look:

    int distance = 10;
    int wid = 600;
    int hei = 400;
    float theta = 0;
    int numVectorsX = wid/distance + 1;
    int numVectorsY = hei/distance + 1;
    Field[][] vectors = new Field[numVectorsX][numVectorsY];
    void setup() {
      size(wid, hei);
      smooth();
      noStroke();
      for (int i = 0; i < numVectorsX; i+=2) {
        for (int j = 0; j < numVectorsY; j+=2) {
          int l = 12;//will be determined by some multiple of the inverse of the distance to mouse position
          // x, y, l, theta
          vectors[i][j] = new Field(i*distance, j*distance, l, theta);
        }
      }
    }
    void draw() {
      frameRate(24);
      stroke(1);
      background(#DAEBF2);
      for (int i = 0; i < numVectorsX; i+=2) {
        for (int j = 0; j < numVectorsY; j+=2) {
          // x, y, l, theta
          vectors[i][j].plot();
        }
      }
    }
    class Field {
      float x, y, l, theta;
      //Constructor
      Field(float xpos, float ypos, float lpos, float thetapos) { 
        x = xpos;
        y = ypos;
        l = lpos;
        theta = thetapos;
      }
      void plot() {
        float theta = atan((mouseY-y)/(mouseX-x));
        if (mouseX < x) {
           theta += PI;
          //println(theta);
        }
        //theta *= -1; //points the field away from the mouse position
        pushMatrix();
        translate(x, y);
        rotate(theta);
        line(0, 0, l, 0);
        translate(l, 0);
        fill(0, 0, 0);
        beginShape();
        vertex(0, -3);
        vertex(0, 3);
        vertex(3, 0);
        endShape(CLOSE);
        popMatrix();
        noFill();
      }
    }
    
Sign In or Register to comment.