Quick Question on line graphs


From a previous i found the below (thanks again to TFguy44:

how would you have multiple of these lines running at the same time on the same page?


int x = 0;
float py = 0;
void setup(){
void draw(){
  float fx = py + random(-20,20);
  line(x-1,py, x,fx);
  py = fx;
  if(py>height/2) py=height/2;
  if(py<-height/2) py=-height/2;


Make it a class,
create multiple objects with that class,
draw all the objects.

Simple as that.

ArrayList<Thing> things = new ArrayList<Thing>();

void setup() {
  size(600, 200);
  for (int count = 0; count < 3; count++) {
    things.add(new Thing());

void draw() {
  fill(0, 0, 0, 15);
  rect(0, 0, width, height);
  for (int i = 0; i < things.size(); i++) {

class Thing {
  int x = 0;
  float py = 0;

  void show() {
    translate(0, height/2);
    float fx = py + random(-20, 20);
    stroke(100, 255, 100);
    line(x-1, py, x, fx);
    py = fx;
    if (py>height/2) py=height/2;
    if (py<-height/2) py=-height/2;


I appreciate your enthusiasm, but giving someone the code, outright, is not helpful in the long run. For anyone who has coded for a while, the answer may have seemed “simple as that”, but to a beginner, it’s probably not as apparent - there’s a reason @Groot came here for help.

In situations like these, try to impress process over code - not “what is the answer” but “how do I get to the answer?”, because, in the long run, learning how to teach yourself is much more useful than the knowledge itself. And do so in a way that doesn’t make them feel stupid for not knowing something “simple as that”.

I definitely encourage you to keep contributing to this community, and give this thread a read if you haven’t already:


Here you go. It doesn’t make sense to me just telling him (or her, I assume him) what to do if he is new to this. How could he know what a class is? So giving the answer he can try to figure out what I did and if he has questions abou that he can ask those.
Giving only a hint might result in a frustrated user especially seeing how long it sometimes akes just to get a simple answer. What if he needs it for anoher project and can’t wait?
So with this he sees “oh it really does work and the code does not seem too much changed” and if he still can’t figure out how he could go on like:
"what does this part do?

Why ist there a get(i)?"


I think this is also a valid point. Which is why I wouldn’t even start by telling @Groot to make a class. In fact, before assuming anything, I would ask them a little about their background. “Can I ask how much experience you have with coding?” or " Lets start by breaking the problem into smaller pieces - do you know what classes are?".

The last thing I want to do is give someone a piece of code that goes way over their head , because at that point, you’re not encouraging them to go out and learn by themselves - you’re just catching them a fish. (Admittedly, you example is pretty succinct and with some comments, self-explanatory)

Now, I’m not saying that you should never give out code (In fact, a lot of the threads on this forum are answered very succinctly with a link to a stackoverflow or a short example). But in some cases, like this, it’s better to teach them how to fish, rather than catching it for them. When I used to tutor in college, I would often help students who were several semesters below me in the math series. They were doing math I thought was trivial, which led to bad practices; it was much easier to just give them the answer to their problems, and move on. But I would see the same students again and again. What took more time, but benefitted them in the long run, was pushing them towards the answer. Teaching them how to approach a problem, as opposed to just giving them the answer, was definitely more time consuming. But what you find is that people are capable, and that most of the time, they can reach the answer by themselves - they just needed to be pushed in the right direction.

This last part is definitely valid point. For all I know, @Groot is a wicked smart dude, making line graphs like a champ out there after fiddling with your code.

Overall, I’m pumped that you’re out here looking to help others. But I ask that you consider the idea that “giving the answer” may not necessarily be helping them learn to learn.