| Thread Closed |
converting from an array of function values to coordinate array of different length |
Share Thread |
| Oct15-06, 10:31 AM | #1 |
|
|
converting from an array of function values to coordinate array of different length
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?
|
| Oct15-06, 02:00 PM | #2 |
|
Recognitions:
|
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?
|
| Oct15-06, 11:00 PM | #3 |
|
|
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]);
}
|
| Oct16-06, 12:12 AM | #4 |
|
Recognitions:
|
converting from an array of function values to coordinate array of different length
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 http://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++;
}
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
|
| Thread Closed |
Similar discussions for: converting from an array of function values to coordinate array of different length
|
||||
| Thread | Forum | Replies | ||
| How can I tell when I've reached the end of an array? | Programming & Comp Sci | 1 | ||
| antenna array | Introductory Physics Homework | 4 | ||
| Antenna array | Introductory Physics Homework | 1 | ||
| C prog: printing values from array of structures | Computing & Technology | 36 | ||
| antenna array | Introductory Physics Homework | 4 | ||