Venelin
Venelin

Reputation: 3306

How to find some additional points on a line between two points in 3D?

Is there a function in C# which can give me all the points on a straight line between two points in 3D?

To calculate the distance between those two points, I use this:

public class Position {
    public float x;
    public float y;
    public float z;
}

public void CalculateDistance(Position position1, Position position2, int mapId){
    float deltaX = position1.x - position2.x;
    float deltaY = position1.y - position2.y;
    float deltaZ = position1.z - position2.z;

    float distance = (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
    Console.WriteLine("Distance is: " + distance);
}

Example coordinates:

Position pos1 = new Position();
pos1.x = 141.6586f;
pos1.y = 0.6852107f;
pos1.z = 153.2231f; 

Position pos2 = new Position();
pos2.x = 142.336f;
pos2.y = 0.8685942f;
pos2.z = 130.8394f;

Let's say, the distance in line between those two 3d coordinates can be passed for 5 seconds. How can I print the current coordinate for every 1 second?

Upvotes: 0

Views: 1365

Answers (1)

Yaroslav Bigus
Yaroslav Bigus

Reputation: 678

what you want to do is well described in this answer

And here is example of code how you can print your values:

var mx = pos2.x - pos1.x;
var my = pos2.y - pos1.y;
var mz = pos2.z - pos1.z;
for(var t=0; t < 10; t++) {
    var x = pos1.x + mx * t;
    var y = pos1.y + my * t;
    var z = pos1.z + mz * t;
    //TODO: use you 3D point
}

Hope this helps!

Upvotes: 2

Related Questions