I’m working this example in my java book, it is supposed to graph a mathematical function and it does work, but i don’t understand the last two lines, can someone explain them to me? Assume that data is an array of doubles and holds function values for a certain ‘range’ in this case the range is -pi to pi.
The book has a comment for this little slice of code and i have an idea of what it is doing, but i would like to know exactly why we do the last two lines in this loop.
// Scale and translate data values
for (int i = 0; i < d.width; i++) {
double value = data[i];
double k = (value - min) / (max - min);
data[i] = d.height * (1 - k);
}
can someone help me out?
Sure. In order to draw the graph, the y-values of the function need to be scaled, so that they fit within the height of the area in which they’ll be drawn. So
kis a scaled version ofythat’s in the range 0 to 1 – that is, the minimum value will map to 0 and the maximum value will map to 1. Lastly, find the data point to draw by convertingkinto a value between 0 andd.height.