使用四点相交的线段

发布于 2024-12-01 19:01:00 字数 189 浏览 0 评论 0 原文

我有四个点 p0 、 p1 、p1' 、 p2' ,每个点由 x,y,z 分量定义,并且全部位于一条线上,如图

在此处输入图像描述

我想从四个点之间的交点获得线段(虚线部分)结果

任何建议,或使用 C# 的示例代码

I have four point p0 , p1 ,p1' , p2' , each defined by x,y,z component and all lay on one line as in the figure

enter image description here

I want to get the line segment (the dashed part) result from the intersection between the four points

any suggestion , or sample code using C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2024-12-08 19:01:00

这或多或少与 Howard 给出的答案相同,但被压入 C# 中......我希望这对您的代码库有所帮助。

这段代码片段应该可以解决这个问题(从 4 个点中找到中点,但前提是所有点都是共线的) - 另请注意,我不检查真正的交点,您可以通过检查答案和您的点来轻松地自己完成此操作。
我没有花时间以合理的方式实现 Vector3D 结构(运算符,...) - 您也可以轻松做到这一点。
另请注意,这不仅适用于 4 点,而且请牢记您的图表。

private struct Vector3D
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}
static class Vectors
{
    static public double ScalProd(Vector3D v1, Vector3D v2)
    {
        return v1.X*v2.X + v1.Y*v2.Y + v1.Z*v2.Z;
    }

    static public Vector3D Minus(Vector3D v1, Vector3D v2)
    {
        return new Vector3D {X = v1.X - v2.X, Y = v1.Y - v2.Y, Z = v1.Z - v2.Z};
    }

    static public Vector3D Normalize(Vector3D v)
    {
        var len = Math.Sqrt(ScalProd(v, v));
        return new Vector3D {X = v.X/len, Y = v.Y/len, Z = v.Z/len};
    }
}

private Vector3D[]  FindIntersectionOnCoLinearVectors(params Vector3D[] input)
{
    if (input.Length < 2) throw new Exception("you need a minimum of two vectors");
    var v0 = input[0];
    var direction = Vectors.Normalize(Vectors.Minus(input[1], v0));
    Func<Vector3D, double> projectOntoLineStartingAtv0 =
        v => Vectors.ScalProd(direction, Vectors.Minus(v, v0));
    var mapped = input.OrderBy(projectOntoLineStartingAtv0).ToArray();
    return new Vector3D[] {mapped[1], mapped[2] };
}

This is more or less the same answer as Howard gave but pressed into C# ... I hope this helps with your code-base.

This code snippet should do the trick (finding the mid-points from your 4, but only if all are colinear) - also note I don't check for real intersection, you can do this easily youself by inspecting the answer and your points.
I did not take the time and implement the Vector3D struct in a sensible manner (operators, ...) - you can do this easily too.
Also note that this will work for not only 4 points but keep your diagram in mind.

private struct Vector3D
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}
static class Vectors
{
    static public double ScalProd(Vector3D v1, Vector3D v2)
    {
        return v1.X*v2.X + v1.Y*v2.Y + v1.Z*v2.Z;
    }

    static public Vector3D Minus(Vector3D v1, Vector3D v2)
    {
        return new Vector3D {X = v1.X - v2.X, Y = v1.Y - v2.Y, Z = v1.Z - v2.Z};
    }

    static public Vector3D Normalize(Vector3D v)
    {
        var len = Math.Sqrt(ScalProd(v, v));
        return new Vector3D {X = v.X/len, Y = v.Y/len, Z = v.Z/len};
    }
}

private Vector3D[]  FindIntersectionOnCoLinearVectors(params Vector3D[] input)
{
    if (input.Length < 2) throw new Exception("you need a minimum of two vectors");
    var v0 = input[0];
    var direction = Vectors.Normalize(Vectors.Minus(input[1], v0));
    Func<Vector3D, double> projectOntoLineStartingAtv0 =
        v => Vectors.ScalProd(direction, Vectors.Minus(v, v0));
    var mapped = input.OrderBy(projectOntoLineStartingAtv0).ToArray();
    return new Vector3D[] {mapped[1], mapped[2] };
}
清晨说晚安 2024-12-08 19:01:00

您可以按如下方式进行:

步骤 1:转换为一维问题

  • 定义t(P) = (P-P0).(P1-P0) / (P1-P0).(P1-P0) 其中点表示标量积
  • t 是通过 P1P0 的直线上的线性度量,
  • 因此我们有t(P0)=0, t(P1)=1

步骤 2:解决一维问题

  • 我们假设 t(P0') <= t( P1') (否则交换以下行中的 P0'P1'
  • 现在可能出现以下情况
    • <代码>t(P1') < 0 =>没有交集
    • <代码>1 < t(P0') =>没有交集
    • t(P0') <= 0 <= t(P1') <= 1 =>交集是线段(P0,P1')
    • t(P0') <= 0 < 1 < t(P1') =>交集是线段(P0,P1)
    • 0 <= t(P0') <= t(P1') <= 1 =>交集是线段(P0',P1')
    • <代码>0 <= t(P0') <= 1 <= t(P1') =>交集是线段(P0',P1)
  • 或者,如果您只对 t 值感兴趣,则交集由 t0 = max 之间的线段给出(0, t(P0'))t1 = min(1, t(P1')) iff t0 <= t1

You may proceed as follows:

Step 1: Transformation into a 1D-problem

  • define t(P) = (P-P0).(P1-P0) / (P1-P0).(P1-P0) where the dot denotes the scalar product
  • t is a linear measure on the line through P1 and P0
  • thus we have t(P0)=0, t(P1)=1

Step 2: Solve the problem in 1D

  • We assume t(P0') <= t(P1') (otherwise swap P0' and P1' in the following lines)
  • Now the following cases are possible
    • t(P1') < 0 => no intersection
    • 1 < t(P0') => no intersection
    • t(P0') <= 0 <= t(P1') <= 1 => intersection is segment (P0,P1')
    • t(P0') <= 0 < 1 < t(P1') => intersection is segment (P0,P1)
    • 0 <= t(P0') <= t(P1') <= 1 => intersection is segment (P0',P1')
    • 0 <= t(P0') <= 1 < t(P1') => intersection is segment (P0',P1)
  • alternatively if you are only interested in the t-values, the intersection is given by the line segment between t0 = max(0, t(P0')) and t1 = min(1, t(P1')) iff t0 <= t1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文