# Random lines emanating from a sphere in 3D

**URL:** <https://discourse.processing.org/t/random-lines-emanating-from-a-sphere-in-3d/15707>\
**Category:** Coding Questions\
**Created:** [November 22, 2019, 4:33am UTC](https://discourse.processing.org/t/random-lines-emanating-from-a-sphere-in-3d/15707 "2019-11-22T04:33:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicnut](https://avatars.discourse-cdn.com/v4/letter/n/7feea3/32.png) [@nicnut](https://discourse.processing.org/u/nicnut)\
**Post date:** [November 22, 2019, 4:33am UTC](https://discourse.processing.org/t/random-lines-emanating-from-a-sphere-in-3d/15707/1 "2019-11-22T04:33:40Z")

</div>

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:

```auto

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]);   
    }
  }
  
  

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 22, 2019, 6:38am UTC](https://discourse.processing.org/t/random-lines-emanating-from-a-sphere-in-3d/15707/2 "2019-11-22T06:38:15Z")

</div>

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](https://processing.org/reference/PVector.html)  
using that and it’s functions make your job more easy

```auto
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](http://mrfeinberg.com/peasycam/) 302 Jonathan Feinberg

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![nicnut](https://avatars.discourse-cdn.com/v4/letter/n/7feea3/32.png) [@nicnut](https://discourse.processing.org/u/nicnut)\
**Post date:** [November 24, 2019, 12:05am UTC](https://discourse.processing.org/t/random-lines-emanating-from-a-sphere-in-3d/15707/3 "2019-11-24T00:05:38Z")

</div>

@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
