Plotting Functions in Java: A Pain in the Neck

In summary, the conversation discusses the challenges of plotting functions in Java, specifically when dealing with large arrays and limited space on a monitor. The speakers suggest various solutions, such as using binary search and selecting points based on domain values, and provide code examples for reference. They also discuss the importance of defining the problem clearly and accounting for different factors, such as scaling and coordinate systems.
  • #1
JoAuSc
198
1
In programming in Java I've frequently wanted to plot functions, meaning I've had to convert a double array of function values (and a corresponding array of their domain values) to integer arrays of coordinates to plot. I've always found this to be a pain in the neck, especially if you're trying to have the number of points plotted depend on how much space you have available in the x direction. (I suppose if I were a more organized person, I would've just solved this problem once and completely generally and then just copy-and-pasted it into new programs.) Even when I finally get it right, the code it takes to do this ends up being confusing and somewhat unreadable. How do you guys deal with this kind of problem?
 
Technology news on Phys.org
  • #2
What exactly is your problem? You want to know how to crop points so that only what you have space for will fit? If the numbers that you evaluate the function at are stored monotone increasing, then you can just do a binary search for the index values of the beginning and end points that you are looking for, and then plot everything between those values. I don't know what you mean by "converting to integer arrays of coordinates to plot." Don't you already have that if you have an array of numbers and an array of values of the function?
 
  • #3
0rthodontist said:
What exactly is your problem? You want to know how to crop points so that only what you have space for will fit? If the numbers that you evaluate the function at are stored monotone increasing, then you can just do a binary search for the index values of the beginning and end points that you are looking for, and then plot everything between those values. I don't know what you mean by "converting to integer arrays of coordinates to plot." Don't you already have that if you have an array of numbers and an array of values of the function?
If I have a function array of a million points, but I only have a monitor with ~1000 pixels, then it wouldn't make sense to plot all million of them. You have to plot only every nth function value, where n = (function array length)/(coordinate array length). Of course, since n is an integer (to be used in a for loop, something like

Code:
for ( int i = 0; i < numCoords; i++ ) {
   j = function.length*i/numCoords;
   xCoords[i] = xOrigin + (int)(domain[j]);
   yCoords[i] = yOrigin - (int)(function[j]);
}

then you can't just have j = n*i where n = (function.length/numCoords) because that'd round to zero. I usually do what's above, but this problem has got to be somewhat common, and so there's got to be others out there who do it differently. I'm just wondering how other people solve this kind of problem.
 
  • #4
Well, I think your problem is that you haven't defined the problem carefully enough. For example, you're not scaling anything, and you're selecting points based on array indices rather than domain values.

You want to plot some points in a window. You have two things to think about:
--The actual function values, which function values you will choose to plot, and the window of those values (minwinx, maxwinx, minwiny, maxwiny)
--The limits of the pixels on the screen (the drawing window) which has its own minscreenx, maxscreenx, minscreeny, maxscreeny.

See my post in https://www.physicsforums.com/showthread.php?t=133282&page=2 which was a similar problem.

First you should select the points which you will plot. Then you should call a function which does plot them on the screen.

Here is some code that selects points from the function array based on the window. It assumes the points are sorted by x.
Code:
int j = 0;
int skipped = 0;
for (int x = minscreenx; x < maxscreenx; x++) { // x is pixel coordinate on screen
   d = (i-minscreenx)*(maxwinx - minwinx) + minwinx; // d = domain value for the function
   for (;domain[j] < d;j++)
      if (j >= domain.length) break;
   // j is now the location of the first domain value >= d
   if(function[j] <= maxwiny && function[j] >= minwiny)
   {
      xPlot[i - skipped] = domain[j];
      yPlot[i - skipped] = function[j];
   }
   else // skip the point, it's off the screen
      skipped++;
}

Here's the function I gave in that other thread, for convenience. It's pseudocode (not Java). If you want to use it you'll have to account for the y-axis pointing down, as well as translating it into Java. It will plot the points that the previous code put in xPlot and yPlot
Code:
plotScreenPoint (x, y) where x and y are points in xPlot and yPlot
   let winx = (x - minwinx) / (maxwinx - minwinx)
   let winy = (y - minwiny) / (maxwiny - minwiny)
   if 0 <= winx <= 1 and 0 <= winy <= 1 then
     let screenx = winx * (maxscreenx - minscreenx) + minscreenx
     let screeny = winy * (maxscreeny - minscreeny) + minscreeny
     point(screenx, screeny)
   otherwise do nothing
 
Last edited:

What is the purpose of plotting functions in Java?

The purpose of plotting functions in Java is to create visual representations of mathematical functions, allowing for a better understanding and analysis of their behavior and relationships.

What are the benefits of using Java for plotting functions?

Java offers a wide range of tools and libraries for plotting functions, making it a powerful and versatile option for visualizing mathematical data. Additionally, Java's object-oriented structure allows for efficient and organized code that can be easily modified and extended.

What are the major challenges of plotting functions in Java?

One major challenge of plotting functions in Java is the complexity of the language, which can make it difficult for beginners to learn and implement. Additionally, the process of creating a graph with precise and accurate data points can be time-consuming and require a strong understanding of mathematical concepts.

Are there any alternative languages or tools for plotting functions?

Yes, there are many alternative languages and tools for plotting functions, such as Python, R, and MATLAB. Each language and tool has its own strengths and weaknesses, so the choice ultimately depends on the specific needs and preferences of the user.

What are some tips for effectively plotting functions in Java?

Some tips for effectively plotting functions in Java include thoroughly understanding the mathematical concepts involved, using appropriate data structures and libraries, and regularly debugging and testing the code for accuracy. It is also helpful to have a strong understanding of Java's graphic capabilities and to continuously seek out resources and guidance from experienced programmers.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
934
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
881
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
7
Views
10K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top