We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to Graph Eqautions
Page Index Toggle Pages: 1
How to Graph Eqautions (Read 811 times)
How to Graph Eqautions
Dec 3rd, 2009, 1:41pm
 
I have an equation that I want to put into to processing but I do not know how to put it in, can some please help me! Sad Undecided
Re: How to Graph Eqautions
Reply #1 - Dec 3rd, 2009, 1:47pm
 
Quote:
can some please help me

Yes, we can.
Re: How to Graph Eqautions
Reply #2 - Dec 3rd, 2009, 1:54pm
 
How do I graph an equation into processing??
Re: How to Graph Eqautions
Reply #3 - Dec 4th, 2009, 1:27am
 
You compute the y coordinates corresponding to the x values of a given range.
You transform arbitrary ranges (input and output) to sketch coordinates (width, height).
You draw lines from a previous (x, y) point to the next one.

OK, here is a simple quick example to get you started:
Code:
void setup()
{
 size(640, 480);
 background(255);
 stroke(#000055);
 noLoop(); // Just draw once and stop
}

void draw()
{
 int prevX = -1, prevY = -1;
 for (int posX = 0; posX < width; posX++)
 {
   // The x coordinate in the math world
   float x = map(posX, 0, width, -5.0, +5.0);
   // The corresponding y coordinate, using the function of your choice
   float y = sin(x);
   // Map back to sketch world
   int posY = (int) map(y, -1.0, +1.0, 0, height);
   if (prevX >= 0)
   {
line(prevX, prevY, posX, posY);
   }
   prevX = posX;
   prevY = posY;
 }
}

If that's for homework, try and understand it (ask if you have doubts) instead of regurgitating it as is.
Exercise: put margins around the graph, add axes, etc.
Re: How to Graph Eqautions
Reply #4 - Dec 4th, 2009, 9:52am
 
Thank you very much, I am going to give it my best shot!
Page Index Toggle Pages: 1