這個方法應該告訴你一個點是否在給定的行上。目前,只要沒有方向矢量的分量是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;
XML文檔和函數名稱與傳遞的參數相矛盾。 –