Move a single point

Hello, How can i do to move each point by sphere in movement for example? Thank you so much.

void setup() {
  size(800, 800, P3D);
}
void draw() {
  background(255);
  for (int i = 0; i < width; i+= 10) {
    for (int j = 0; j < height; j+=2) {
      point(i, j);
    }
  }
}

Answers

  • move each point by sphere

    I'm not sure what that means. Can you explain what you want to happen?

  • Schermata 2016-10-29 alle 19.18.01

    I would like deform this grid of point by some object like a sphere.

  • Answer ✓

    You are thinking 3D? You will need to work with the equation of a sphere so to lay points on its surface. Any point that is not in your sphere's surface intersecting the plane, then must be drawn on the plane surface.Here is an idea using points.

    Kf

    int r=200;      //sphere radius
    int pl=r*2;     //length of base plane (square shape)
    int stepSp=20;  //Distance btw points in sphere
    int stepPl=10;  //Distance btw points in plane
    
    void setup(){
    size(800,800,P3D);
      strokeWeight(4);
    }
    
    void draw(){
      background(92);
    
      float myx=map(mouseX,0,width,-width>>1,width>>1);
      float myy=map(mouseY,0,height,0,1200);
      translate(width>>1,height>>1);
      camera(myx, myx,myy, 0.0, 0.0, 0.0, 
           0.0, 1.0, 0.0);
    
      //A plane
      stroke(0,0,255);
      for(int i=-pl;i<pl;i=i+stepPl){
        for(int j=-pl;j<pl;j=j+stepPl){
          //Draw points only if outside of sphere's volume
          if(dist(i,j,0,0)>r)
            point(i,j,0);
        }
      }
    
      //A sphere  
      stroke(255,0,0);
      for(int i=-r;i<r;i=i+stepSp){    
        for(int j=-r;j<r;j=j+stepSp){
          point(i,j,sqrt(r*r - i*i - j*j));
        }
        //Second half of the spher, if needed
        for(int j=-r;j<r;j=j+stepSp){
          //point(i,j,-sqrt(r*r - i*i - j*j));
        }
      }
    }
    
  • Answer ✓

    width>>1

    oh god, it's spreading...

  • edited October 2016 Answer ✓

    iterate through the dots, like you are doing.

    if the x,y distance from the centre of the sphere is < radius of the sphere then the z value can be found from the implicit definition of a sphere

    x^2 + y^2 + z^2 = r^2

    so

    z = sqrt(r^2 - x^2 - y^2)

  • edited October 2016 Answer ✓

    The approach given by @koogs (and @kfrajer) is interesting because this will solve for two points each time -- a positive point (positive z half-sphere), and a negative point (negative z half-sphere).

        stroke(64,128,255);
        point(x, y, z);
        stroke(128,255,64);
        point(x, y, -z);
    

    PointSpheres

  • Thank you to all guys! ;)

  • Looking back I realized that I wrote a sketch about this and never posted it, so I'm sharing it here.

    Keep in mind that in Processing you can get shapes like spheres automatically as meshes (if you don't need points specifically).

    That said, here is a demo sketch that demonstrates two different point spheres. The fibonacci spiral approach is a bit more involved, gives relatively even coverage of the surface -- unlike z-projection of a plane of points, which is simpler to implement, but gives dense points at the pole (center) and creates unusual curved artifacts at the equator (edges).

    /**
     * Point Spheres
     * 2016-10-29 Jeremy Douglass - Processing 3.2.1
     * https:// forum.processing.org/two/discussion/18779/move-a-single-point
     **/
    
    void setup() {
      size(600, 600, P3D);
      strokeWeight(2);
    }
    void draw() {
      background(0);
    
      stroke(32,32,32);
      pointPlane(width, height, 10); // w, h, spacing
    
      stroke(255,0,0);
      pushMatrix();
        translate(width/3,height/3); // place on screen
        timedTumble(40); // animated spin
        pointSphereFibonacci(width/5, 1000); // radius and number of points
      popMatrix();
    
      pushMatrix();
        translate(width*2/3,height*2/3); // place on screen
        timedTumble(20); // animated spin
        pointSphere(width/5); // radius of sphere
      popMatrix();
    }
    
    void pointPlane(int pwidth, int pheight, int spacing){
      // initial sketch by paolopastorino
      for (int x = 0; x < pwidth; x+= spacing) {
        for (int y = 0; y < pheight; y+= spacing) {
          point(x, y);
        }
      }
    }
    
    void pointSphere(int r){
      // based on suggestion by koogs
      float z;
      pushStyle();
      for (int x = -r; x < r; x+= 10) {
        for (int y = -r; y < r; y+=10) {
          z = sqrt( (r*r) - (x*x) - (y*y));
            stroke(0,255,0);
            point(x, y, z);
            stroke(0,0,255);
            point(x, y, -z);
        }
      }
      popStyle();
    }
    
    void pointSphereFibonacci(int radius, int points){
      // based on fibonacci sphere by Jim Bumgardner
      // https:// www.openprocessing.org/sketch/41142
      float phi = (sqrt(5)+1)/2 - 1; // golden ratio
      float ga = phi*2*PI;           // golden angle
      for (int i = 1; i <= points; ++i) {
        float lon = ga*i;
        lon /= 2*PI; lon -= floor(lon); lon *= 2*PI;
        if (lon > PI)  lon -= 2*PI;
        float lat = asin(-1 + 2*i/(float)points);
        pushMatrix();
          rotateY( lon);
          rotateZ( -lat);
          point(radius,0,0);
        popMatrix();
      }
    }
    
    //// Timed 3D tumble, rotations cycle back to start every `duration` milliseconds
    void timedTumble(float duration){
      duration = duration*1000; // milliseconds
      float spin = TWO_PI*2 * (millis()%duration)/duration;
      rotateX(spin);
      rotateY(spin/2);
      rotateZ(spin/8);
    }
    

    NOTE: For performance, you don't want to recalculate every single draw loop the way that this example -- it is written simply for demo clarity. Instead, it is much more efficient to calculate the point locations once, then store those locations in an array, then loop through that array at draw time. See Jim Bumgardner's original fibonacci sphere code for an example of that.

    PointSpheres_1

Sign In or Register to comment.