0

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?

1
  • Feel free to accept and upvote the answer if you found it useful and it answered your question which seems to be the case! Commented Jul 15, 2012 at 11:12

1 Answer 1

2

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 k is a scaled version of y that'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 converting k into a value between 0 and d.height.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i don't fully understand the explanation but i will once i pore over it a bit. Is this a standard way of doing things?
Pretty standard, yes. Try out different values of max, min and value, to see what happens with k, and with d.height. Then you'll understand.
ok its making more sense. I did a simple example with the numbers {1,2,3,4,5} and in order to map these numbers to 0-1 you do indeed need to do: value - min/difference between max and min. I feel like i have seen this before when dealing with Math.random() for some reason..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.