Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Find distance between two coordinates

Tags:

python

I'm working with a range of 0 to 255.

My data would be a set of numbers within this range.

Except I need the difference between

255 & 0 = 1
254 & 5 = 7

So in a sense this range is a circular set, and 255 and 0 are the pole.

Is there some function / easy way to do this? Or do I need to take the second half of the range and assign it some value to be calculated against? i.e. 255 becomes 255 - 254 if it is being compared against [1 - 127.4]

like image 815
Justin Avatar asked Jun 27 '26 15:06

Justin


1 Answers

def diff256(a, b):
  return min((a - b) % 256, (b - a) % 256)
like image 51
Ignacio Vazquez-Abrams Avatar answered Jun 29 '26 11:06

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!