I have a function which needs to take out equidistant points between two points on the screen(2d).
Like this -
|--------------|
The distance is already decided. For example, I take it as 2
here, then the points I need the -
|--.--.--.--.--|
The points can be anywhere on the 2d plane, which means if I draw a line between the two points, it can be any orientation possible in a 2d plane, i.e diagonal, horizontal, etc.
I can't figure out how to do this in python.
I do not know what to google for ... And I'm 14 so I don't know any type of math for this.
I know the how to calculate distance and slope of the line, but I don't know how to proceed.
Thanks in advance!
Distance in a coordinate system. Distance in a 2D coordinate plane: The distance between two points on a 2D coordinate plane can be found using the following distance formula. d = √ (x 2 - x 1) 2 + (y 2 - y 1) 2. where (x 1, y 1) and (x 2, y 2) are the coordinates of the two points involved.
How to Find Equidistant Coordinates Between Two Locations on Earth. One of the key points of The Meeting Point Locator is to obtain an orthogonal great circle to the bearing defined by any two given locations on Earth. A great circle is the intersection of the sphere and a plane that passes through the center point of the sphere.
What is the Equidistant Formula? The distance between any two given points can be calculated with the help of the distance formula: d = √[(x2 −x1)2 +(y2 −y1)2] √ [ ( x 2 − x 1) 2 + ( y 2 − y 1) 2]; here [ x1 x 1 and y1 y 1 )] are the coordinates of one point and [ x2 x 2 and y2 y 2] are the coordinates of the other point.
The distance between any two given points can be calculated with the help of the distance formula: d = √[(x2 −x1)2 +(y2 −y1)2] √ [ ( x 2 − x 1) 2 + ( y 2 − y 1) 2]; here [ x1 x 1 and y1 y 1 )] are the coordinates of one point and [ x2 x 2 and y2 y 2] are the coordinates of the other point.
What you need to do is interpolate between the two points.
For example, let's say your two endpoints have coordinates (x1, y1)
and (x2, y2)
, and you want to split the distance between them into n
equal parts, then you can calculate n-1
new points between them like this:
points = []
for i in range(1, n):
a = float(i) / n # rescale 0 < i < n --> 0 < a < 1
x = (1 - a) * x1 + a * x2 # interpolate x coordinate
y = (1 - a) * y1 + a * y2 # interpolate y coordinate
points.append( (x,y) )
Here, a
is the position of the interpolated point on the line between the original points, scaled so that the values a = 0
and a = 1
correspond to the original points themselves.
Alternatively, if you want to have your interpolating points a fixed distance d
apart, then you can calculate the distance d_full
between your original points using the Pythagorean theorem, divide d
by that distance to get s = d / d_full
, and then increment a
in steps of s
from 0 to 1:
d_full = ( (x2 - x1)**2 + (y2 - y1)**2 )**0.5
s = d / d_full
points = []
a = s # start at s so we don't duplicate (x1, y1)
while a < 1:
x = (1 - a) * x1 + a * x2
y = (1 - a) * y1 + a * y2
points.append( (x,y) )
a += s
Note that this may result in a new point being placed at (x2, y2)
or very close to it, depending on how exactly d
divides the distance between the points. If you want to avoid that, you can replace the condition a < 1
with, say, a < 1 - s/2
.
Edit: The code above places the points at intervals of d
starting at (x1, x2)
. That means that if, say, d = 2
and the original points are at (0,0)
and (0,5)
, you'll get new points at (0,2)
and (0,4)
.
If you'd instead prefer the new points to be centered between the original points (i.e. at (0,1)
and (0,3)
in the example), you can modify the code to do that by replacing the starting point a = s
with a = (1 % s) / 2
.
The %
is the modulo or remainder operator, so 1 % s
gives the remaining distance "left over" after the distance from 0 to 1 has been split into pieces of length s
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With