# Defining Coordinates of a Sphere

**URL:** <https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813>\
**Category:** Beginners\
**Created:** [March 31, 2019, 6:18pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813 "2019-03-31T18:18:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirstee\_baxt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kirstee_baxt/32/4377_2.png) [@kirstee\_baxt](https://discourse.processing.org/u/kirstee_baxt)\
**Post date:** [March 31, 2019, 6:18pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/1 "2019-03-31T18:18:49Z")

</div>

Hello!  
I’m interested in defining the coordinates of where a sphere lies in a 3D space. I know sphere uses just radius `sphere(r);`

Is there a way to define (x,y,z) values?

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [March 31, 2019, 7:16pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/2 "2019-03-31T19:16:31Z")

</div>

Here is a clue from processing reference:-

```auto
noStroke();
lights();
translate(58, 48, 0);
sphere(28);

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 31, 2019, 8:09pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/3 "2019-03-31T20:09:04Z")

</div>

[https://processing.org/tutorials/p3d/](https://processing.org/tutorials/p3d/)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 31, 2019, 9:41pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/4 "2019-03-31T21:41:33Z")

</div>

If you want a data structure for 3D spheres of different size / locations, you can also make a simple class.

```auto
ArrayList<SphereLoc> spheres = new ArrayList<SphereLoc>();
void setup() {
  size(400, 400, P3D);
  for(int i=0;i<50;i++){
    spheres.add(new SphereLoc(random(300),random(300),random(300),random(40)));
  }
}

void draw(){
  background(128);
  for(int i=0; i<spheres.size(); i++){
    spheres.get(i).render();
  }
}

class SphereLoc {
  float x, y, z, r;
  SphereLoc(float x, float y, float z, float r){
    this.x = x;
    this.y = y;
    this.z = z;
    this.r = r;
  }
  void render() {
    translate(x, y, z);
    sphere(r);
    translate(-x, -y, -z);
  }
}

```

You could also do the class render with pushMatrix / popMatrix, but translating back should be a bit faster.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 31, 2019, 11:04pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/5 "2019-03-31T23:04:15Z")

</div>

Some remarks:

- I recommend to use lights() in 3D and
- also to use noStroke(); with spheres, together with fill();
- There is a shorter form of a for loop, I use it in draw() below

here

```auto

ArrayList<SphereLoc> spheres = new ArrayList<SphereLoc>();

void setup() {
  size(900, 900, P3D);
  for (int i=0; i<50; i++) {
    spheres.add(new SphereLoc(random(0, 900), random(0, 900), random(70, -300), 
      random(40)));
  }
  noStroke();
}

void draw() {
  background(128);
  lights();
  for (SphereLoc currentSphere : spheres) {
    currentSphere.render();
  }
}

// ============================================================

class SphereLoc {

  float x, y, z, r;
  color col=color(random(255), random(255), random(255));

  // constr 
  SphereLoc(float x, float y, float z, float r) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.r = r;
  }// constr

  void render() {
    translate(x, y, z);
    fill(col); 
    sphere(r);
    translate(-x, -y, -z);
  }
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![kirstee\_baxt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kirstee_baxt/32/4377_2.png) [@kirstee\_baxt](https://discourse.processing.org/u/kirstee_baxt)\
**Post date:** [April 2, 2019, 7:27pm UTC](https://discourse.processing.org/t/defining-coordinates-of-a-sphere/9813/6 "2019-04-02T19:27:47Z")

</div>

@Chrisir @jeremydouglass  
Thanks for the help! The information about class worked great for my project.
