Drawing lines between two selected planets

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: Dance of Mercury and Earth - YouTube

and my processing codes: https://github.com/borromini20/planets.git

Thanks in advance

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

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));
}
1 Like

@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:

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+
Simon

2 Likes

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