# Quick Question on line graphs

**URL:** <https://discourse.processing.org/t/quick-question-on-line-graphs/2786>\
**Category:** Coding Questions\
**Created:** [August 20, 2018, 10:52am UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786 "2018-08-20T10:52:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Groot](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@Groot](https://discourse.processing.org/u/Groot)\
**Post date:** [August 20, 2018, 10:52am UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786/1 "2018-08-20T10:52:58Z")

</div>

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?

Thanks.

```auto
int x = 0;
float py = 0;
 
void setup(){
  size(600,200);
  background(0);
}
 
void draw(){
  noStroke();
  fill(0,0,0,15);
  rect(0,0,width,height);
  translate(0,height/2);
  x++;
  x%=width;
  float fx = py + random(-20,20);
  stroke(100,255,100);
  noFill();
  line(x-1,py, x,fx);
  py = fx;
  if(py>height/2) py=height/2;
  if(py<-height/2) py=-height/2;
}

```

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [August 20, 2018, 11:33am UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786/2 "2018-08-20T11:33:47Z")

</div>

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

Simple as that.

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

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

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

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

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

```

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 21, 2018, 9:44pm UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786/3 "2018-08-21T21:44:07Z")

</div>

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:

> [@Guidelines—Answering Questions](https://discourse.processing.org/t/guidelines-how-to-answer-questions/2145):
>
> If you’re here, it means you’re thinking about contributing to the Processing community by answering questions on the forum. That’s great! This guide is meant to help you answer questions in a way that’s most helpful. Be nice. Remember that you’re talking to a person! Follow the guidelines in the [FAQ](https://discourse.processing.org/faq). Also read through the [P5.js community guidelines](https://p5js.org/community/). Specifically, avoid making judgments like “this code is bad” or “that’s a dumb way to do this”. Be mindful of how your tone comes across over the…

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [August 22, 2018, 5:34am UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786/4 "2018-08-22T05:34:57Z")

</div>

> [@tony](#):
>
> how do I get to the answer

> [@MxFxM](#):
>
> Make it a class,  
> create multiple objects with that class,  
> draw all the objects.

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?

> [@MxFxM](#):
>
> things.get(i).show();

Why ist there a get(i)?"

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 22, 2018, 6:20am UTC](https://discourse.processing.org/t/quick-question-on-line-graphs/2786/5 "2018-08-22T06:20:41Z")

</div>

> [@MxFxM](#):
>
> 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?

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.

> [@MxFxM](#):
>
> So with this he sees “oh it really does work and the code does not seem too much changed”

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.
