线和球体的交点? [英] Intersection of a line and a Sphere?

查看:46
本文介绍了线和球体的交点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的对象,它允许您分配三个属性 (x,y,z)(让我们称这个对象为点",因为它就是这样).然后我有第二个对象,它的方法接受第一个对象的两个实例,并返回三维空间中两个点"之间的距离.我还需要一个可以接受两个点"的方法和一个双精度值,表示行进距离(从使用的第一个点"参数开始),返回一个带有 x、y、z 坐标的点"对象.

I have a simple object that allows you to assign three properties (x,y,z) (lets call this object a "point", because that is what it is). I then have a second object with a method that accepts two instances of the first object, and returns the distance between the two "points" in three dimensional space. I also need a method that will accept two "points" and a double, representing distance traveled (from the first "point" parameter used) that returns a "point" object with its x,y,z coordinates.

除了计算所提供的两点之间的原始线上的点坐标外,我对一切都没有意见,即与第一个点有一定的距离.

I'm ok with everything except the calculation of the point coordinates that are on the original line between the two points supplied, that is at a certain distance from the first point.

点"对象:

public class POR
{
    private double PORX;
    private double PORY;
    private double PORZ;

    public double X
    {
        get { return PORX; }
        set { PORX = value; }
    }
    public double Y
    {
        get { return PORY; }
        set { PORY = value; }
    }
    public double Z
    {
        get { return PORZ; }
        set { PORZ = value; }
    }
    public POR(double X, double Y, double Z)
    {
        PORX = X;
        PORY = Y;
        PORZ = Z;
    }

然后我正在使用:

    public double PorDistance(POR por1, POR por2)
    {
        return Math.Round(Math.Sqrt( Math.Pow((por1.X - por2.X),2) + Math.Pow((por1.Y - por2.Y),2) + Math.Pow((por1.Z - por2.Z),2)),2);
    }

要返回这两个点之间的距离,我需要类似的东西

to return the distance between those two points I need something like

public POR IntersectPOR (POR por1, POR por2, double distance)
{

}

其中距离是从 por1 到 por2 的距离.

where distance is the distance traveled from por1 towards por2.

推荐答案

这可以在 Vectors 的帮助下完成.

This can be done with a bit of help from vectors.

假设你的起点叫做P,另一个点是Q,距离是d.你想在 PQ 线上找到从 P 到 Q 距离为 d 的点.

Let's say your starting point is called P, and the other point is Q, and the distance is d. You want to find the point on the line PQ at a distance d from P towards Q.

  1. 首先,您需要找到行进方向.这是通过找到 Q - P

  1. First you need to find the direction of travel. That's done by finding Q - P

v = Point(Q.x - P.x, Q.y - P.y, Q.z - P.z)

  • 现在你需要找到那个方向的单位向量,所以

  • Now you need to find the unit vector in that direction, so

    scale = sqrt(v.x*v.x + v.y*v.y + v.z*v.z)
    unit = Point(v.x/scale, v.y/scale, v.z/scale)
    

  • 现在你需要找到代表行进距离的向量:

  • Now you need to find the vector representing the distance traveled:

    t = Point(unit.x*d, unit.y*d, unit.z*d)
    

  • 要找到最终位置,请将您行进的向量添加到您的起点:

  • To find the final position, add your traveled vector to your starting point:

    final = Point(P.x + t.x, P.y + t.y, P.z + t.z)
    

  • 这篇关于线和球体的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆