You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#是否有高级库可按指定距离获取3D直线上的点坐标?

在C#中实现3D直线上指定距离点的计算

C#基础类库没有直接提供你示例中FindPoint这样的现成方法,但借助.NET自带的System.Numerics.Vector3(或Unity等引擎中的Vector3类),可以轻松封装出该功能,不需要额外高级库。

核心实现思路

  1. 计算两点间的方向向量:用第二个点坐标减去原点坐标,得到从原点指向第二个点的向量。
  2. 归一化方向向量:将向量转换为长度为1的单位向量,保证后续移动距离的准确性。
  3. 计算目标点:原点坐标加上「单位方向向量 × 指定距离」,得到沿直线移动后的点。

具体代码实现

确保项目引用System.Numerics程序集后,可封装如下方法:

using System.Numerics;

public static Vector3 FindPoint(Vector3 origin3dPoint, Vector3 second3dPoint, float distanceFromOrigin)
{
    // 计算方向向量
    Vector3 direction = second3dPoint - origin3dPoint;
    // 处理两点重合的异常情况
    if (direction.LengthSquared() < float.Epsilon)
    {
        throw new ArgumentException("两个输入点不能重合");
    }
    // 归一化方向向量
    direction = Vector3.Normalize(direction);
    // 计算并返回目标点
    return origin3dPoint + direction * distanceFromOrigin;
}

适配你的业务场景

针对梯形斜腿工具架的需求,比如要计算两点连线3/4长度位置的点,可先算出两点间总距离,再传入对应比例的距离值:

Vector3 bottomLegPoint = new Vector3(0, 0, 0); // 斜腿底部点
Vector3 topLegPoint = new Vector3(1.5f, 3, 0.8f); // 斜腿顶部点
float totalLegLength = Vector3.Distance(bottomLegPoint, topLegPoint);
// 获取3/4长度位置的点,用于计算横撑
Vector3 crossbarPoint = FindPoint(bottomLegPoint, topLegPoint, totalLegLength * 0.75f);

注意事项

  • 若distanceFromOrigin传入负数,会得到原点反方向直线上的点,满足直线无限延伸的需求。
  • 若使用Unity引擎,可直接用内置的Vector3类,逻辑完全一致,无需额外引入System.Numerics。

内容的提问来源于stack exchange,提问作者Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 18:05:34