C# 检查点是否位于三维直线上

C# 检查点是否位于三维直线上,c#,geometry,line,point,C#,Geometry,Line,Point,这个方法应该告诉你一个点是否在给定的线上。目前,只要方向向量的任何分量都不为0(我使用的是线的参数化表示),它就可以正常工作。如果没有0,如果点在直线上,我应该得到xResult=yResult=zResult。如果方向向量中有一个0,则这三个向量中至少有一个是0,因此不等于所有其他向量,但该点仍可能在直线上 处理零情况的最佳方法是什么,以确定点是否在给定线上 /// <summary> /// Returns true if the passed point is

这个方法应该告诉你一个点是否在给定的线上。目前,只要方向向量的任何分量都不为0(我使用的是线的参数化表示),它就可以正常工作。如果没有0,如果点在直线上,我应该得到xResult=yResult=zResult。如果方向向量中有一个0,则这三个向量中至少有一个是0,因此不等于所有其他向量,但该点仍可能在直线上

处理零情况的最佳方法是什么,以确定点是否在给定线上

    /// <summary>
    /// Returns true if the passed point is on the line, false otherwise
    /// </summary>
    /// <param name="passedPoint"></param>
    /// <returns></returns>
    public Boolean IsOnLine(Line passedLine)
    {
        Boolean pointIsOnLine = false;

        //Get components of this point
        Dimension xPoint = new Dimension(DimensionType.Millimeter, X.Millimeters);
        Dimension yPoint = new Dimension(DimensionType.Millimeter, Y.Millimeters);
        Dimension zPoint = new Dimension(DimensionType.Millimeter, Z.Millimeters);

        //Get components of the base point of the line
        Dimension xBasePoint = new Dimension(DimensionType.Millimeter, passedLine.BasePoint.X.Millimeters);
        Dimension yBasePoint = new Dimension(DimensionType.Millimeter, passedLine.BasePoint.Y.Millimeters);
        Dimension zBasePoint = new Dimension(DimensionType.Millimeter, passedLine.BasePoint.Z.Millimeters);

        //Find difference between passed point and the base point
        Dimension xDifference = xPoint - xBasePoint;
        Dimension yDifference = yPoint - yBasePoint;
        Dimension zDifference = zPoint - zBasePoint;

        DimensionGenerator dg = new DimensionGenerator(DimensionType.Millimeter);

        //Instantiate the 3 result variables
        Dimension xResult = dg.MakeDimension(-1);
        Dimension yResult = dg.MakeDimension(-1);
        Dimension zResult = dg.MakeDimension(-1);


        //Solve for the multiplier using each direction and make sure they are all equal.
        //If any component of the direction vector is 0, the result will be zero and should therefore be directly assigned to 0 to avoid dividing by 0
        if(passedLine.XComponentOfDirection.Millimeters == 0)
        {
           xResult = dg.MakeDimension(0);
        }

        if(passedLine.YComponentOfDirection.Millimeters == 0)
        {
            yResult = dg.MakeDimension(0);

        }

        if(passedLine.ZComponentOfDirection.Millimeters == 0)
        {
            zResult = dg.MakeDimension(0);
        }

        else
        {

            xResult = dg.MakeDimension(xDifference.Millimeters / passedLine.XComponentOfDirection.Millimeters);
            yResult = dg.MakeDimension(yDifference.Millimeters / passedLine.YComponentOfDirection.Millimeters);
            zResult = dg.MakeDimension(zDifference.Millimeters / passedLine.ZComponentOfDirection.Millimeters);

        }


        //If the 3 results are equal, the point is on the line. If they are not, the point is not on the line.
        if (xResult == yResult && xResult == zResult)
        {
            pointIsOnLine = true;
        }
        else
        {
            pointIsOnLine = false;
        }

        return pointIsOnLine;
//
///如果传递的点在直线上,则返回true,否则返回false
/// 
/// 
/// 
公共布尔IsOnLine(行通过行)
{
布尔点单直线=假;
//获取此点的组件
尺寸xPoint=新尺寸(尺寸类型:毫米,X毫米);
尺寸Y点=新尺寸(尺寸类型:毫米,Y毫米);
尺寸zPoint=新尺寸(尺寸类型:毫米,Z毫米);
//获取直线基点的组件
尺寸xBasePoint=新尺寸(尺寸类型.mm,通过线.基点.X.mm);
尺寸yBasePoint=新尺寸(尺寸类型.毫米,通过线.基点.Y.毫米);
尺寸zBasePoint=新尺寸(尺寸类型.毫米,通过线.基点.Z.毫米);
//查找通过点和基点之间的差异
维度xDifference=xPoint-xBasePoint;
尺寸Y差异=Y点-Y基点;
尺寸zDifference=zPoint-zBasePoint;
尺寸生成器dg=新尺寸生成器(尺寸类型.毫米);
//实例化3个结果变量
维度xResult=dg.MakeDimension(-1);
维度yResult=dg.MakeDimension(-1);
尺寸zResult=dg.MakeDimension(-1);
//使用每个方向求解乘数,并确保它们都相等。
//如果方向向量的任何分量为0,则结果将为0,因此应直接指定为0,以避免除以0
如果(passedLine.XComponentOfDirection.mm==0)
{
xResult=dg.MakeDimension(0);
}
if(passedLine.ycomponentTofDirection.mm==0)
{
yResult=dg.MakeDimension(0);
}
if(passedLine.ZComponentOfDirection.mm==0)
{
zResult=dg.MakeDimension(0);
}
其他的
{
xResult=dg.MakeDimension(xDifference.mm/通过线.xComponentToFaction.mm);
yResult=dg.Make尺寸(Y差值.毫米/通过线.Y组件方向.毫米);
zResult=dg.Make尺寸(zDifference.mm/通过线.zComponentTofDirection.mm);
}
//如果三个结果相等,则点在直线上。如果不相等,则点不在直线上。
if(xResult==yResult&&xResult==zResult)
{
pointIsOnLine=true;
}
其他的
{
pointIsOnLine=false;
}
返回点在线;

我建议用另一种方式进行。使用数学。如果任意两个向量之间的角度为0,则向量“共享一条线”

cos(θ)=(U*V)/(U*V)(点积)

应用于您的代码:

double inverse = ((xPoint*xBasePoint) + (yPoint*yBasePoint) + (zPoint*zBasePoint))/((sqrt(xPoint^2 +yPoint^2 + zPoint^2) + sqrt(xBasePoint^2 + yBasePoint^2 + zBasePoint^2));

  if(inverse == 1)
      //points are on the same line
   else
      //points are not on the same line or one point is (0,0,0)

另外,在分配xresult、yresult和zresult时,请查看if-else语句。如果passedLine.Z不为0,则该语句最终仍可能被零除(转到else语句)。

我建议使用另一种方法。使用数学。如果任意两个向量之间的角度为0,则向量“共享一条线”

cos(θ)=(U*V)/(U*V)(点积)

应用于您的代码:

double inverse = ((xPoint*xBasePoint) + (yPoint*yBasePoint) + (zPoint*zBasePoint))/((sqrt(xPoint^2 +yPoint^2 + zPoint^2) + sqrt(xBasePoint^2 + yBasePoint^2 + zBasePoint^2));

  if(inverse == 1)
      //points are on the same line
   else
      //points are not on the same line or one point is (0,0,0)

另外,在分配xresult、yresult和zresult时,请查看if-else语句。如果passedLine.Z不为0,则该语句最终仍可能被零除(转到else语句)。

我建议使用另一种方法。使用数学。如果任意两个向量之间的角度为0,则向量“共享一条线”

cos(θ)=(U*V)/(U*V)(点积)

应用于您的代码:

double inverse = ((xPoint*xBasePoint) + (yPoint*yBasePoint) + (zPoint*zBasePoint))/((sqrt(xPoint^2 +yPoint^2 + zPoint^2) + sqrt(xBasePoint^2 + yBasePoint^2 + zBasePoint^2));

  if(inverse == 1)
      //points are on the same line
   else
      //points are not on the same line or one point is (0,0,0)

另外,在分配xresult、yresult和zresult时,请查看if-else语句。如果passedLine.Z不为0,则该语句最终仍可能被零除(转到else语句)。

我建议使用另一种方法。使用数学。如果任意两个向量之间的角度为0,则向量“共享一条线”

cos(θ)=(U*V)/(U*V)(点积)

应用于您的代码:

double inverse = ((xPoint*xBasePoint) + (yPoint*yBasePoint) + (zPoint*zBasePoint))/((sqrt(xPoint^2 +yPoint^2 + zPoint^2) + sqrt(xBasePoint^2 + yBasePoint^2 + zBasePoint^2));

  if(inverse == 1)
      //points are on the same line
   else
      //points are not on the same line or one point is (0,0,0)

另外,在分配xresult、yresult和zresult时,请查看if-else语句。如果passedLine.Z不是0,则它仍然可以被零除(转到else语句).

如果你处理的是不精确的数字,比较相等是个坏主意。我会尝试以下方法:取直线基点到点的向量,并取直线的方向向量。计算它们的叉积。如果点位于直线上,两个向量将共线,它们的叉积将为b所以计算向量的平方长度(避免不必要的平方根),如果它低于某个阈值,那么你的点就在所讨论的线上


但是,如果您想更接近自己的代码,请注意,您有三个
if
,后跟一个
else
。因此,如果第一个to条件之一适用,else块仍将被执行。这可能会给您带来麻烦。另外请注意,如果您以毫米为单位进行所有计算,可能会节省大量代码b如果你处理的是不精确的数字,比较是否相等是个坏主意。我会尝试以下方法:用t