java - spreading out objects on a line evenly -
i creating objects on line in window created piece of code:
void createturtles() { int nrturtles = keyboard.nextint("set amount of turtles: "); w = new graphicswindow(500, 300); drawlinez(); (int k = 1; k <= nrturtles; k++) { turtle t = new turtle(w, 50, 50 + k*10); t.right(90); t.setspeed(100); t.pendown(); turtles.add(t); } } this codeline:
turtle t = new turtle(w, 50, 50 + k*10); creates 1 turtle @ time. right have set turtles have y coordinat of 50, , x coordinat of 50+k*10. because line starts @ x coordinat of 50 , stops @ x coordinat of 250.
now want is, based on nr of turtles created (user inputs this), want turtles spread on line evenly. how it? has line wrote , maybe k value or 10.
the line illustrated in picture (see link below), red line, number of turtles created at.
devide height - 100 of window number of turtles , have distancebetweenturles:
int nrturtles = keyboard.nextint("set amount of turtles: "); int height = 300; w = new graphicswindow(500, height); drawlinez(); double distancebetweenturles = (height - 100.0) / nrturtles; (int k = 1; k <= nrturtles; k++) { turtle t = new turtle(w, 50, 50 + (int) (k * distancebetweenturtles)); t.right(90); t.setspeed(100); t.pendown(); turtles.add(t); }
Comments
Post a Comment