View Full Version : Calculating distance beetween two 2d points in pixels?
crazyfuturamanoob
September 10th, 2008, 04:45 PM
So how can I calculate distance beetween two 2d points in python? :confused:
Reiger
September 10th, 2008, 04:47 PM
Call me stupid but when did good ole' Pythagoras ever fail in multi-dimensional spaces?
In 2D:
Given two points (x0,y0) and (x1,y1):
sqrt((x0-x1)^2 + (y0-y1)^2)
Assuming the square root function is directly available as sqrt in Python.
cmat
September 10th, 2008, 05:09 PM
Close but to use exponents in Python, they must be in the form "x ** y".
To use the "sqrt()" function you need to "import math".
import math
x1 = 56.0
y1 = 23.0
x2 = 120.0
y2 = -234.0
print math.sqrt( ( x1-x2 )**2 + ( y1-y2 )**2 )
Reiger
September 10th, 2008, 05:25 PM
Yeah well, actually I don't know Python. :D Just pointing out there exists that famous formula ;)
dribeas
September 10th, 2008, 08:42 PM
So how can I calculate distance beetween two 2d points in python? :confused:
What is your definition of 'distance in pixels'? The distance given before is Cartesian distance, which is common life distance, but there are others, as Manhattan distance (sum of horizontal plus vertical distance (minimum number of pixels you must step over to get from point a to point be by crossing only sides and not diagonals)...
Manhattan_distance = ( x2 - x1 ) + ( y2 - y1 )
jimi_hendrix
September 10th, 2008, 10:39 PM
this might be what your all saying and im missing it but i think theres a postulate that says the distance between two points is the difference is the absolute value of the difference in their coordinat
Lux Perpetua
September 11th, 2008, 05:54 AM
What is your definition of 'distance in pixels'? The distance given before is Cartesian distance, which is common life distance, but there are others, as Manhattan distance (sum of horizontal plus vertical distance (minimum number of pixels you must step over to get from point a to point be by crossing only sides and not diagonals)...
Manhattan_distance = ( x2 - x1 ) + ( y2 - y1 )
More accurately, |x2 - x1| + |y2 - y1| (note absolute values).
dribeas
September 11th, 2008, 06:59 AM
More accurately, |x2 - x1| + |y2 - y1| (note absolute values).
Right :)
maximinus_uk
September 11th, 2008, 12:30 PM
The distance from pixel(a,b) to pixel(c,d) *in pixels* is the modulus of the larger number of either (a-c) or (b-d); subtract 2 from this number if you wish to exclude the start and end points.
This is to do with how pixels are rendered on a grid. Agreed, it differs from pythag!
I first came across this when using Bresenhams algorithm (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm) many many years ago. We were calculating how pixels would need to be rendered on a given line; turns out the answer is trivial to calculate - and you donīt need pythag.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.