# Drawing lines between two selected planets

**URL:** <https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 31, 2021, 2:21pm UTC](https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502 "2021-01-31T14:21:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![borromini](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@borromini](https://discourse.processing.org/u/borromini)\
**Post date:** [January 31, 2021, 2:21pm UTC](https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502/1 "2021-01-31T14:21:16Z")

</div>

Hi everyone, I have a source code which has moving planets on an orbit. I would like to draw lines between two of them (like draw lines between Earth and Jupiter). Here is an example I found on Youtube: [https://www.youtube.com/watch?v=wvfza7r-EiU](https://www.youtube.com/watch?v=wvfza7r-EiU)

and my processing codes: [GitHub - borromini20/planets](https://github.com/borromini20/planets.git)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [January 31, 2021, 6:10pm UTC](https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502/2 "2021-01-31T18:10:07Z")

</div>

Hi, I thought that you could save the positions of the lines with an ArrayList and then draw them with a for loop …  
But I think that pulls a lot of power …

Here is a small example

```auto
ArrayList<PVector> positions;

void setup(){
  size(600, 600);
  positions = new ArrayList<PVector>();
  stroke(255);
  strokeWeight(4);
}
void draw(){
  background(0);
  for(int i = 0; i < positions.size()-1; i += 2){
    PVector p1 = positions.get(i);
    PVector p2 = positions.get(i+1);
    line(p1.x, p1.y, p2.x, p2.y);
  }
}
void mousePressed(){
  positions.add(new PVector(mouseX, mouseY));
}

```

---

<div class="post-metadata">

**Author:** ![WeinSim](https://avatars.discourse-cdn.com/v4/letter/w/db5fbb/32.png) [@WeinSim](https://discourse.processing.org/u/WeinSim)\
**Post date:** [January 31, 2021, 6:49pm UTC](https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502/3 "2021-01-31T18:49:00Z")

</div>

@Flolo yes, that approach would probably work, but there is actually a way to fix the efficiency issue you mentioned, which could really turn out to be quite problem.  
You can create a `PGraphics` object onto which you draw all the lines, seperately from the planets. In code, this would look something like this:

```auto
PGraphics lines;
//other global variables

void setup() {
  //size() and other setup stuff
  lines = createGraphics(width, height);
  lines.beginDraw();
  lines.background(0);
  lines.endDraw();
}

void draw() {
  //move planets around
  lines.beginDraw();
  lines.stroke(255);
  lines.strokeWeight(2);
  lines.line(x1, y1, x2, y2); //drawing a line from (x1, y1) to (x2, y2)
  lines.endDraw();
  
  image(lines, 0, 0);
  //draw planets
}

```

Here’s the PGraphics Processing reference: [PGraphics \ Language (API) \ Processing 3+](https://processing.org/reference/PGraphics.html)  
Simon

---

<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:** [February 1, 2021, 6:44am UTC](https://discourse.processing.org/t/drawing-lines-between-two-selected-planets/27502/4 "2021-02-01T06:44:01Z")

</div>

Please note when necessary there is a 3D version of the line command
