Plotting Functions in Java: A Pain in the Neck

Click For Summary

Discussion Overview

The discussion revolves around the challenges of plotting functions in Java, particularly the conversion of function values and domain values into coordinates suitable for graphical representation. Participants explore various methods for selecting and scaling points to fit within a specified plotting window, addressing issues related to array indexing and screen pixel limitations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses frustration with the complexity of converting double arrays of function values into integer arrays for plotting, especially when the number of points must adapt to available screen space.
  • Another participant suggests using binary search to efficiently find the range of points to plot when the domain values are sorted, questioning the need for conversion to integer arrays if function values are already available.
  • A further elaboration indicates that when dealing with a large number of function points, only a subset should be plotted based on the screen resolution, proposing a method to select every nth function value.
  • Another participant critiques the initial problem definition, emphasizing the importance of scaling and selecting points based on both function values and screen limits, and provides pseudocode for selecting and plotting points based on specified windows.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to the problem, with differing opinions on the necessity of certain conversions and the methods for selecting points to plot. The discussion remains unresolved regarding the most effective solution.

Contextual Notes

Limitations include assumptions about the ordering of domain values, the need for scaling, and the handling of pixel coordinates versus function values. The discussion also highlights the complexity of translating pseudocode into Java.

JoAuSc
Messages
197
Reaction score
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
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?
 
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.
 
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
20
Views
2K
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K