Question

I'm trying to figure out how to approach this assignment. We are to use only what is included in java.* and javax.* to graph a given function with constraints on x and y.

An idea that I had is to find the value of the function at every x_n, such that x_n = x + .01*n, and then draw a line between each point.

So for example, if f(x) = 3x, x = [0..3], I would find the value of f(0.00), f(0.01), f(0.02),..., f(2.99), f(3.00) and draw lines between each value.

Would this work, or is there a better approach? Please keep in mind that this is our first assignment in Java, so the simpler the approach, the better I'll be able to understand it. I appreciate any input!

Was it helpful?

Solution

This is a perfectly good approach, and just about the simplest. Go for it!

For a general solution, the change in x (currently 0.01) should be set according to the range of x and the pixel size of your image.

For example, if x=[0..100] you wouldn't want to plot 10,001 points, it would be inefficient and also look horrible. Often looks best if you plot one point every few x pixels (3 or 4?) and draw straight lines between them.

OTHER TIPS

You could use java.awt.geom.GeneralPath class to do so. Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top