Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Converting from an array of function values to coordinate array of different length

  1. Oct 15, 2006 #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?
  2. jcsd
  3. Oct 15, 2006 #2


    User Avatar
    Science Advisor

    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?
  4. Oct 15, 2006 #3
    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 (Text):
    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.
  5. Oct 16, 2006 #4


    User Avatar
    Science Advisor

    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 (Text):

    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
    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 (Text):

    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: Oct 16, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook