如何在3D中的两个点之间的线上找到一些其他点?

时间:2018-10-29 10:14:55

标签: c# math coordinates

C#中是否有一个函数可以为我提供3D两点之间一条直线上的所有点?

要计算这两个点之间的距离,请使用以下方法:

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);
}

坐标示例:

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;

比方说,这两个3d坐标之间的直线距离可以传递5秒钟。如何每1秒打印一次当前坐标?

1 个答案:

答案 0 :(得分:1)

您想要做的事情在answer

中有很好的描述

下面是代码示例,您可以通过它打印值:

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
}

希望这会有所帮助!