Is there a formula to place objects 3d in a radial way, like forming a sphere?

edited June 2016 in Questions about Code

Hey, I'm struggling to find a way to place instances of a class into an array in such way that being radial so form a sphere, I think it would look somewhat like this, but along all the surface of the sphere, I tried using the cos and sin into the position variables of the vertex of the QUAD_STRIP with no success. Thanks in advance guys.

Answers

  • x = r * cos(u) * cos(v)
    y = r * cos(u) * sin(v)
    z = r * sin(u)
     where 0<u<2π and 0<v<π
    

    This is the parametric equation for a sphere. I would just choose random values within the bounds for u and v, then evaluate.

  • I'll try, thanks a lot Minesha

  • what i'm struggling with in your description in the QUAD_STRIP part because picture just looks like lines.

    whenever i want radial lines i use a random pvector which i then normalise and magnify so that all the points are equidistant from the centre. something like

    PVector p = new PVector(random(-100, 100), random(-100, 100), random(-100, 100)); // could use random3d
    p.setMag(2); // inner radius 2
    line(p.x, p.y, p.z, 4 * p.x, 4 * p.y, 4 * p.z); // outer radius 8
    

    (untested)

  • // forum.Processing.org/two/discussion/17032/
    // is-there-a-formula-to-place-objects-3d-
    // in-a-radial-way-like-forming-a-sphere
    
    // GoToLoop (2016/Jun/08)
    
    final int RAYS = 1000, INNER = 100, OUTER = 4, BOLD = 2;
    final PVector inner = new PVector(), outer = new PVector();
    
    size(600, 600);
    smooth(4);
    noLoop();
    
    strokeWeight(BOLD);
    stroke(0);
    background(-1);
    translate(width>>1, height>>1);
    
    for (int i = 0; i < RAYS; ++i) {
      PVector.random3D(inner, this).mult(INNER);
      outer.set(inner).mult(OUTER);
      line(inner.x, inner.y, outer.x, outer.y);
    }
    
  • 2d lines?

  • A 3D sphere, Koogs, but I'll check this out

  • yeah, i know. but gotoloop is using 2d lines whereas i guess you're using a 3d renderer...

  • I only added P3D and z coordinate to the PVector, and worked, now I'll try to apply this to my code, because mine aren´t just lines but objects

  • code not by me

  • float radius = 200.0;
    float rho = radius;
    float factor = TWO_PI/20; 
    float x, y, z;
    
    float zAngle;
    float yAngle;
    
    void setup() {
      size(500, 500, P3D);
      smooth();
    }
    
    void draw() {
      background(50);
      stroke(255);
      lights();
    
      translate(width/2, height/2);
    
      rotateX(radians(mouseY));
      rotateY(radians(mouseX));
    
      for (float phi = 0.0; phi < PI; phi += factor) {
    
        for (float theta = 0.0; theta < TWO_PI + factor; theta += factor) {
    
          x = rho * sin(phi) * cos(theta);
          z = rho * sin(phi) * sin(theta);
          y = -rho * cos(phi);
    
          pushMatrix();
    
          translate(x, y, z);
    
          //I think this should give me relevant angles that can then be used (with modification) for rotation of the rectangles
          zAngle = acos(z/radius);
          yAngle = atan2(y, x);
    
          rotateY(radians(zAngle-90)); //this is my pain point - I can't find any combination of rotations that does what I want
          rotateX(radians(yAngle-90));
    
          rect(0, 0, 15, 15);
    
          popMatrix();
        }
      }
    }
    //
    
Sign In or Register to comment.