Log In  

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

P#95890 2021-08-11 20:48

You're close, but in this case using the absolute value makes things worse for the reason you give.

Try (self.t2-self.t1)/2 + self.t1

Or to put it another way: (there-here)/2 + here

P#95892 2021-08-11 21:18
:: Peej

Brilliant, that works a treat.
Thanks so much, Bikibird

P#95894 2021-08-11 21:55

@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.

P#95896 2021-08-11 22:05

@jasondelaat, slightly less intuitive, but more efficient. Nice!

P#95900 2021-08-12 00:32
:: Peej
1

@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

P#95924 2021-08-12 19:33

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2021-11-27 14:15:08 | 0.083s | Q:19