hi im trying to create a program that adds balls with mousePressed on the sketch and connects each of the balls with lines. ive managed to create an array of length 5 that adds a value(mouseX and mouseY), each time i mouse click but when i click the mouse again it deletes the previous. i tried to add a line function that connects all the balls together but it keeps connecting from 0,0 to the ball location.
Any help at all would be greatly appreciated.
PVector[] location = new PVector[10];
int ballnum;
int i;
;
void setup() {
size(600, 600);
background(255);
ballnum=0;
i = 0;
}
void draw() {
fill(255);
for (int i=0; i <ballnum; i++) {
location[i] = new PVector(0, 0);
if (mousePressed) {
location[ballnum] = new PVector(mouseX, mouseY);
circle(location[ballnum].x, location[ballnum].y, 20);
line(location[ballnum].x, location[ballnum].y, location[ballnum-1].x, location[ballnum-1].y);
}
}
println();
//println(ballnum, "=ballnum");
// println();
println(location[ballnum], ballnum);
println();
println("--------------------");
println(location[1]);
println();
println(location[2]);
println();
println(location[3]);
println();
println(location[4]);
println();
}
void mousePressed()
{
ballnum++;
}
ok so ive manged to store each of the positions of the balls and made it abit more simpler but now im stuck with making each ball connect to every other ball with a straight line. what should i do?
int length = 101;
PVector[] position = new PVector[length];
int BallNum;
void setup() {
size(600, 600);
background(255);
}
void draw() {
fill(255);
layout();
fill(0);
}
void mousePressed()
{
BallNum++;
position[BallNum]= new PVector(mouseX, mouseY);
circle(position[BallNum].x, position[BallNum].y, 20);
if (BallNum >= 2) {
//line(position[BallNum].x,position[BallNum].y,position[BallNum+1].x,position[BallNum+1].y);
}
}
void layout() {
for (int yLine=0; yLine<=width; yLine+=width/10) {
for (int xLine=0; xLine<=height; xLine+=height/10) {
line(yLine, 0, yLine, height);
line(0, xLine, width, xLine);
}
}
}
i understand your point but i can just put the click amount to really high number such as 100000. well i guess i have to try to solve this with arraylist but still i dont understand how to connect with lines in both methods
thanks for all your help but ive figured it out the solution but not im trying to make each node connect to the nearest grid intersection any idea how i can do that? i think if make an array of all the grid intersections and compare them to the current node that is placed which should do it but im not sure how to structure it.