# calculate coordinates on line between 2 coordinates

hi

i have 2 coordinates being ( x, y, y ) and ( i, j, k )

i divide the straight line that connect ( x, y, y ) and ( i, j, k ) into for example 6 equal parts
and would like to calculate the coordinates of those points

i think i found some solutions for 2d:

http://www.cocos2d-iphone.org/forum/topic/33889

but does anybody know how to do this for 3d ?

thanks for any help
greetings
sc3

Given two points in 3d, p1 and p2, define a vector v as p2 - p1.
Normalize v so that the length is one.
Then the infinite number of points on the line segment between p1 an p2 is defined by p1 + dv where d is any value between zero and the distance between
p1 and p2.

This is actually mathematically quite simple when you think about it.

The line AB as we shall call it is simply a vector comprised of an X, Y and Z component.

dX (the x component) = Bx - Ax
dY = By - Ay
dZ = Bz - Az

Let’s argue you were looking for the midpoint. Well, the midpoint is exactly halfway between A and B, and as A to B is a vector made up of components, the midpoint is half each component. (A + .5 * (B - A))

So, the same applies for any fraction of the vector. Thus, splitting it into six parts is just taking each sixth of the vector.
With mathutils (the default blender maths lib) we don’t even have to bother doing component operations, we can multiply the entire vector by a scalar:

``````
from mathutils import Vector

a = Vector((0, 0, 0))
b = Vector((3, 3, 3))

difference = (b - a)

a_sliced = [(difference / 6) * (i + 1) for i in range(6)]

print(a_sliced)

``````

thanks for the responses
i will try those out tomorrow

you can also use the lerp function:

``````slices = 6

v1 = Vector((0,0,0))
v2 = Vector((6,0,0))

v_sliced = [v1.lerp(v2, (i+1)/slices) for i in range(slices)]
``````

thanks @CoDEmanX
the lerp function is essential if you go from positive to negative coordinates

regards