middle point formula

Blitz3D Forums/Blitz3D Programming/middle point formula

maximo(Posted 2005) [#1]
Can someone help me with forumula to calculate middle point on a line? Given two points p1(x1,y1) and p2(x2,y2) I need to calculate middle point between them.

I remeber this from school but can't get it to work in every situation.

Can you give me example/formula that works?


fredborg(Posted 2005) [#2]
centerx = (x1+x2)/2.0
I'll leave the y coordinate to you...


maximo(Posted 2005) [#3]
Doooh, what was I thinking, it's so simple :)

Thanks fredborg drinks is on me if I ever run into you :)


Jams(Posted 2006) [#4]
Can you believe i actually had to do a google search for this thread :S.... duh!


Yan(Posted 2006) [#5]
This has been asked a few times here, so you're not the only ones... :o)


sswift(Posted 2006) [#6]
Here's how to do it the hard way!


First calculate the length of the line, like so:
D# = Sqr((X2#-X1#)^2 + (Y2#-Y1#)^2)

Then calculate a vector from the line:
Vx# = X2#-X1#
Vy# = Y2#-Y1#

Then normalize that vector:
Nx# = Vx# / D#
Ny# = Vy# / D#

Then divide the length by 2:
D# = D# / 2.0

Then multiply the normal by that length:
Vx# = Nx# * D#
Vy# = Ny# * D#

And finally, add that vector to the start point to determine the location of the center of the line:
X3# = X1# + Vx#
Y3# = Y1# + Vy#


big10p(Posted 2006) [#7]
You have too much time on your hands, sswift. :P

(I can't call you 'swifty' anymore because there is now another forum member with that name. Boo.)