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
}