一条线,一个球的交集? [英] Intersection of a line and a Sphere?

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

问题描述

我有一个简单的对象,使您可以指定三个属性(X,Y,Z)(让调用这个对象的一个​​点,因为那是它是什么)。然后我有一个接受的第一个对象的两个实例,并返回在三维空间中的两个点之间的距离的方法的第二个目的。我还需要将接受两个点的方法 和双,再presenting行驶距离(从第一个点使用的参数),它返回一个点对象,其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;
    }

然后我使用了:

I'm then using :

    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.

推荐答案

这可与载体一点的帮助下完成。

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

比方说,你的出发点,称为P,而另一点是Q,并且距离为d。你想找到就行PQ的点从对向Q A距离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)
    

  • 现在,你需要找到载体重新presenting移动的距离:

  • 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天全站免登陆