How can I save lines?

I need the lines drawn to be saved and match up to form an overall route. Really confused as to what’s missing.
float cx; // x coordinate of point 1
float cy; // x coordinate of point 1
int lastClickX = -1;
int lastClickY = -1;
float x = 0;

void setup() {
size(700, 700);

}

void mousePressed() {
cx = mouseX;
cy = mouseY;

redraw();

}
void mouseReleased() {
if(lastClickX>=0 && lastClickY>=0) line(mouseX,mouseY,lastClickX,lastClickY);
lastClickX = mouseX;
lastClickY = mouseY;

}

void draw() {
background(0, 0, 100);
ellipse(cx, cy, 20, 20);
stroke(255);
line(cx, cy, mouseX, mouseY);
line(mouseX,mouseY,lastClickX,lastClickY);

line (pmouseX, pmouseY, mouseX, mouseY);
println ();
float distance = dist(cx, cy, mouseX, mouseY);
textSize(60);
text(""+distance, 40, 500);
}

Do you want to save the lines (as data, eg start and end point) on the hard drive?

Hi! Thank you for your reply :slight_smile:

I’d just like the lines to appear on the screen. As in, I would like to draw a point, have that stay on screen and then be able to draw the next line. i.e. Point A – Point B – Point C

Does that make sense?

you can delete that

:wink:

2 Likes

Woah, I really am a beginner :grinning_face_with_smiling_eyes:

The points work! But it seems that the background was hiding some kind of wild lines being drawn. I assume it has something to do with the framerate? Or something in that direction?

Hello,

There are resources here:

As a beginner you should be take a look the tutorials and look up the references for all the elements of your code.

:)

instead of getting rid of background you can really store the lines (start points) in an ArrayList

example


float cx; // x coordinate of point 1
float cy; // x coordinate of point 1
int lastClickX = -1;
int lastClickY = -1;
float x = 0;

ArrayList<PVector> list = new ArrayList(); 

void setup() {
  size(700, 700);
}

void mousePressed() {
  cx = mouseX;
  cy = mouseY;

  list.add(new PVector(mouseX, mouseY));

  //  redraw();
}
void mouseReleased() {
  if (lastClickX>=0 && lastClickY>=0) 
  {
    //line(mouseX, mouseY, lastClickX, lastClickY);
    // list.add(new PVector(mouseX, mouseY));
  }
  lastClickX = mouseX;
  lastClickY = mouseY;
}

void draw() {
  background(0, 0, 100);
  ellipse(cx, cy, 20, 20);
  stroke(255);
  line(cx, cy, mouseX, mouseY);
  line(mouseX, mouseY, lastClickX, lastClickY);

  line (pmouseX, pmouseY, mouseX, mouseY);
  println ();
  float distance = dist(cx, cy, mouseX, mouseY);
  textSize(60);
  text(""+distance, 40, 500);

  for (PVector pv : list) {
    ellipse(pv.x, pv.y, 
      8, 8);
    //line(pv.x, pv.y,.....);
  }
}

1 Like