We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I need some way to plot a graph in Processing. I just need it for a simple quadratic equation. I found this tool: http://www.gwoptics.org/processing/gwoptics_p5lib/ via this thread https://processing.org/discourse/beta/num_1270229678.html However it seems like the gwoptics library is not supported in Processing 3. Any suggestions of alternatives I can use?
Answers
https://www.openprocessing.org/sketch/7262
Try this other link as well:
https://forum.processing.org/two/discussion/comment/70182/#Comment_70182
Also you should check the gicentreUtils lib (Add library using the Processing IDE). Also the grafica library, which I think it would be more relevant to your needs.
Kf
@keykeeper -- as @kfrajer mentions, for axes, labels, etc., you could try the giCentre Utils "Chart".
For graphing a quadratic function in Processing - you could just implement the quadratic function as a Processing function to solve y for any x given a b c:
If you want to graph a parabola in a given range, generate a list of points by looping over the range xmin to xmax, and draw point(x, quadraticY(x)) of the parabola, e.g.:
You can:
point()
svertex
You might try the graphica library
Thank you all for input on this. I imported both gicentreUtils and grafica and tested all the examples but none of them seems to provide me with a simple solution for plotting the graph for 3(xx)+2x-1=0
jeremydouglass - the screenshot you provided seems like what I am after, but can I please ask you to provide the complete code for generating this graph?
Take my link
@keykeeper
if you post your code, we can make either grafica or gicentreUtils work. Or you can use @Chrisir's link as it is close to what @jeremydouglass presented.
Kf
https://www.google.de/search?q=x*x
for processing we have to write it with
*
:3*(x*x)+2*x-1=0
Google can't do this.
check wolfram alpha
new version with your formula and improved scaling
@keykeeper -- here is a simple demo that draws the x and y axis, centers drawing on the axis, and then renders a quadratic equation with points. It created the image in the thread above.
P.S. I believe that there is also an alternate approach which uses the built in curve functions and is much more concise. However, it is a bit tricky.
You can describe a segment of quadratic equation parabola using only a pair of endpoints -- one a single
vertex
, one a single quadraticVertex. However, getting the correct parabola requires you to calculate the single quadraticVertex control point correctly, which lies at the midpoint but beyond the parabola vertex.Thank you all for the input on this. It has been really helpful.
I just have two follow ups: Chrisir - In the method matchToScreenCoordinates x and y is multiplied with 40. Why 40?
jeremydouglass-I tried your code sending in a=3 b=2,c=-1 but the graph did not cross the x axis. Is there anything else I need to adjust?
The 40 is just the scaling from the data to what you see on the screen
40 just looked ok, no deeper reason
@keykeeper -- looks like there was an error in this line:
Should be:
That was a quick demo -- if you want labeled axes etc. etc. then the draw loop needs to be redone.
You also want to base your function on units, not pixels on. I cheated and just made a = 0.025 to avoid the incredibly steep curve that you get on a 400-wide graph. Instead compute actual points and then map them to pixel space with a scaling factor.
@keykeeper -- if you want nicely labeled and formatted graphs with good presentation, your best bet is probably to:
y = a*x*x + b*x + c;
),If instead you want to learn how to create graphs or how customize your own from scratch, then both @Chrisir and my examples each demonstrate some of the basic logic of setting up graph display (axes, scaling, etc.). I've updated my example to show axis units, to make the function display-independent, and to display both the "points" (black) and the "PShape" (blue) method of rendering the same curve.
A final note on the points vs. shape approaches:
Note that the "points" method is simple, but needs really high resolution -- e.g. maybe 4+ points drawn on the curve for every 1 pixel of x-width -- it will look sparse / disconnected at low point sample rates -- or even at high rates where the slope is steepest.
By contrast contrast, PShape doesn't need as many points, and will always look connected -- however at very low plotting resolution drops, it will start to look angular / jagged.
Thank you again for the advise. It has been very good to get insight on how to create graphs in Processing.