Plotting functions in Java can be challenging, particularly when converting double arrays of function values into integer coordinates for display. A common solution involves selecting only the necessary points based on the available screen space, often using a binary search for efficiency. Users need to define their plotting window and ensure that the function values are appropriately scaled to fit within the pixel limits of the display. The discussion highlights the importance of selecting points carefully and provides pseudocode for implementing the plotting logic. Overall, the thread emphasizes the need for organized approaches to simplify the plotting process in Java.
#1
JoAuSc
197
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?
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
JoAuSc
197
1
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.
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
Dear Peeps
I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are
Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C.
I also need to learn C++, Matlab and for personal interest Haskell.
For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...