Random lines emanating from a sphere in 3D

Hi,

I am still pretty new to processing. There are a few things I don’t really have a great grasp on yet, including arrays and 3d space.

I am trying to have a Sphere in 3D that has lines emanating from the sphere in random directions. The lines can really go anywhere, but I don’t want them going in the sphere, but every direction in the 3d space outside of the sphere.

I have some code here that I am trying out, but it doesn’t do what I want. All the lines are emanating from a similar space outside the sphere, but not all over the sphere as I am trying to achieve. I was trying to have is so that the lines are emanating from the center of the sphere, minus the radius (so that they would be at different points on the surface.

If this doesn’t make sense let me know.

Here is my code for now:



float ramp1=200;
float ramp2=100;


float[] lll =new float[100]; 

void setup() {

//  fullScreen(P3D, 2);
   size(800, 800, P3D); 

  //background(0);


}





void draw() {

  background(255);


  stroke(0);               //  for the sphere
  strokeWeight(1);
  translate(width/2, height/2, 0);
  //rotateX(mouseY * 0.05);
  rotateY(radians(ramp2));
  noFill();
  sphereDetail(30);
  sphere(ramp1);
  
  
    strokeWeight(1);

  for (int i = 0; i < lll.length; i++) {
    lll[i] = random(500);
 }
 


  if (ramp2 > 1) {
    for (int i = 0; i < 50; i++) {
      line(0+ramp1/2, 0+ramp1/2, random(ramp1/2), lll[i], lll[i+1], lll[i]);   
    }
  }
  
  

}








1 Like

not fully clear how you want it,
possibly from sphere surface pointing radial outwards…

__
while you develop better not make random in draw, difficult to see anything.

__
there is a nice thing for 2D and 3D thinking
that is called vector,
in Processing PVector its a float array { px,py,pz }
https://processing.org/reference/PVector.html
using that and it’s functions make your job more easy

int r1=200, r2=100, many = 100;
PVector[] rays = new PVector[many];

void setup() {
  size(800, 800, P3D);
  for ( int i = 0; i < rays.length; i++ ) { 
    rays[i]=new PVector(random(-1, 1), random(-1, 1), random(-1, 1) ); //___ create random vector in space
    rays[i]=rays[i].setMag(r1); //__________________________________________ set their length all on sphere surface
  }
}

void draw() {
  background(0, 0, 80);
  translate(width/2, height/2, 0);
  fill(200, 200, 0);
  //noFill();
  //sphere(r1);
  for ( int i = 0; i < rays.length; i++ ) {
    PVector ray = new PVector(rays[i].x, rays[i].y, rays[i].z); //__________ make a copy of startpoint
    ray.setMag(r1+r2); //___________________________________________________ make longer for endpoint ( all same long )
    stroke(0, 0, 200);
    line(0,0,0,rays[i].x, rays[i].y, rays[i].z); //_________________________ line from 0 to startpoint  BLUE
    stroke(200, 200, 0);
    line(rays[i].x, rays[i].y, rays[i].z, ray.x, ray.y, ray.z); //__________ line from startpoint to endpoint
  }
}


sorry, but if you work in 3D
first should be to have a 3D look on what you draw there.
can install lib
PeasyCam 302 Jonathan Feinberg

import peasy.PeasyCam;
PeasyCam cam;

int r1=200, r2=100, many = 100;
PVector[] rays = new PVector[many];
boolean showsun = false;

void make_rays() {
  for ( int i = 0; i < rays.length; i++ ) { 
    rays[i]=new PVector(random(-1, 1), random(-1, 1), random(-1, 1) ); //___ create random vector in space
    rays[i]=rays[i].setMag(r1); //__________________________________________ set their length all on sphere surface
  }  
}

void draw_rays() {
  for ( int i = 0; i < rays.length; i++ ) {
    PVector ray = new PVector(rays[i].x, rays[i].y, rays[i].z); //__________ make a copy of startpoint
    ray.setMag(r1+r2); //___________________________________________________ make longer for endpoint ( all same long )
    stroke(0, 0, 200);
    line(0,0,0,rays[i].x, rays[i].y, rays[i].z); //_________________________ line from 0 to startpoint  BLUE
    stroke(200, 200, 0);
    line(rays[i].x, rays[i].y, rays[i].z, ray.x, ray.y, ray.z); //__________ line from startpoint to endpoint
  }
}

void setup() {
  size(800, 800, P3D);
  cam = new PeasyCam(this, 400);
  make_rays();
  println("use: key [s] show sun, [r] rerandom, \n mouse 3D view");
}

void draw() {
  background(0, 0, 80);
  //  translate(width/2, height/2, 0);  // not used with PeasyCam
  fill(200, 200, 0);  //noFill();
  if ( showsun ) sphere(r1);
  draw_rays();
}

void keyPressed() {
  if ( key == 's' ) showsun = ! showsun; //________________________________ toggle sphere
  if ( key == 'r' ) make_rays(); //________________________________________ re randomize that rays array
}



3 Likes

@kll

Thank you. THis is pretty great. I forgot about PVector. I learned a lot from your solution. I can see how having the random in set up is way better. OK I am going to mess around with your solution. I will work on this and post some results.

Thank You. Nick

2 Likes