Hello all...

I have a maths problem.

I have two points, t1 & t2 that I want to find the centre of.

t1 & t2 are two point on the x axis.

Another object's x approaches this middle point.

I have this so far:

if flr(self.x+3) == abs(self.t1-self.t2)/2+abs(self.t1)

t1 minus t2 divided by 2 giving half the distance between the two, added on to t1.

This works if t1 is smaller than t2.

But it doesn't work if t2 is smaller than t1, because the the 'middle point' is off to the right of t2!!

Can anyone suggest a better formula for getting the middle of two points regardless of which one is smaller?

I'm stumped.

Many thanks for your time and I hope this makes sense....

Peej

@Peej Since both points are on the x-axis these are basically just numbers so the mid-point is just the average: (t1 + t2) / 2

You can expand out/simplify @bikibird's solution to see that this is actually the same thing:

(there - here) / 2 + here = (there - here) / 2 + 2*here / 2 = (there - here + 2 * here) / 2 = (there + here) / 2 |

This will generalize to higher dimensions as well so if you need to find the mid-point of a 2D line not on the x-axis it would be:

x_mid = (x1 + x2) / 2 y_mid = (y1 + y2) / 2 |

where (x1, y1) are the coordinates of the first point and (x2, y2) are the coordinates of the second.

@jasondelaat I thought this was some kind of witchcraft at first but it works great and has saved me a few tokens to boot. Many thanks.

Thanks again @bikibird for starting the ball rolling.

Cheers

[Please log in to post a comment]