Skip to content

Commit

Permalink
add project point to plane
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Jan 12, 2023
1 parent 3c1addd commit 46f71cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/GShark.Test.XUnit/Geometry/Point3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public void It_Checks_If_A_Point_Lies_On_A_Plane()
// Assert
pt.IsOnPlane(plane, 0.001).Should().BeTrue();
}

[Fact]
public void If_Check_Project_A_Point_On_A_Plane()
{
// Arrange
Point3 point = new Point3(10, 20, -5);
Point3 planeOrigin = new Point3(0, 10, 0);
Vector3 direction = new Vector3(0, 1, 0);
Plane plane = new Plane(planeOrigin, direction);
var expectedPt = new Point3(10, 10, -5);

// Act
var result = Point3.ProjectTo(point,plane);

// Assert
result.Should().BeEquivalentTo(expectedPt);
result.Equals(expectedPt).Should().Be(true);
}

[Fact]
public void It_Returns_The_Linear_Interpolation_Between_Two_Points()
Expand All @@ -71,6 +89,7 @@ public void It_Returns_The_Linear_Interpolation_Between_Two_Points()
var result = Point3.Interpolate(p1, p2, amount);

// Assert

result.Equals(expectedPoint).Should().Be(true);
}

Expand Down
15 changes: 15 additions & 0 deletions src/GShark/Geometry/Point3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,20 @@ public double DistanceTo(Point3 other)
}
return d;
}

/// <summary>
/// Project a point onto a plane.
/// </summary>
/// <param name="point">point</param>
/// <param name="plane">plane to project onto</param>
/// <returns name="the point projected to plane"></returns>
public static Point3 ProjectTo(Point3 point, Plane plane)
{
Vector3 v = plane.Origin - point;
Vector3 normal = plane.ZAxis;
double d = Vector3.DotProduct(v, normal);
return point + d * normal;
}

/// <summary>
/// Calculates the distance of a point to a line.
Expand Down Expand Up @@ -665,6 +679,7 @@ public int InPolygon(Polygon polygon)
}
return inside ? 1 : -1;
}

}
}

0 comments on commit 46f71cc

Please sign in to comment.