Home > Net >  Flip a Y coordinate within a min-max range
Flip a Y coordinate within a min-max range

Time:11-30

I'm calculating a Y coordinate which represents a value on a y-axis on a chart I'm drawing in PIL, to display on an LCD display.

I set a start_y and an end_y. The (physical) top of the screen is y = 0. I get my y coordinate as follows:

start_y = 10 # 10 pixels from top of screen
end_y = 60 # 60 pixels from top of screen
min = 10
max = 25
percentage_within_range = (value - min) / (max - min)
y = (percentage_within_range * (end_y - start_y))   start_y

But the y coordinate I get is the 'wrong way around'. I.e. if the value was 19, the resultant y would be 40. That's 20 pixels from the bottom of the chart's display area, but I want it to be 20 pixels from the top of the chart's display area.

How do I reframe my calculation to return my y value 'reversed'?

CodePudding user response:

because of the nature of the equation, it will give the value of the total pixels from the top of the chart as your min max and start end also start from the top. If you want the remaining pixels, You will have to subtract the current answer from the highest pixel possible which is end_y. Your equation will become

y = end_y - ((percentage_within_range * (end_y - start_y))   start_y)

as far as my understanding of the question, I came upon this answer.

  • Related