Skip to content

Commit

Permalink
Merge pull request #404 from chuongmep/addtripleproduct
Browse files Browse the repository at this point in the history
Add TripleProduct Method
  • Loading branch information
d3ssy committed Feb 9, 2023
2 parents 4c665f7 + 69130e9 commit e1d76b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/GShark.Test.XUnit/Geometry/Vector3Tests.cs
Expand Up @@ -236,6 +236,24 @@ public void It_Returns_True_If_Vectors_Are_Equal()
// Assert
(vec1 == vec2).Should().BeTrue();
}


[Theory]
[InlineData(new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 }, new double[] { 7, 8, 9 }, 0)]
[InlineData(new double[] { 2, 3, -4 }, new double[] { 5, 1, 6 }, new double[] { -7, 8, 9 }, -527)]
public void It_Returns_Result_Of_Triple_Product(double[] v1, double[] v2, double[] v3, double expected)
{
// Arrange
Vector3 testVector1 = new Vector3(v1[0], v1[1], v1[2]);
Vector3 testVector2 = new Vector3(v2[0], v2[1], v2[2]);
Vector3 testVector3 = new Vector3(v3[0], v3[1], v3[2]);

// Act
double result = testVector1.TripleProduct(testVector2, testVector3);

// Assert
result.Should().Be(expected);
}

[Fact]
public void It_Checks_If_Vectors_Are_Perpendicular()
Expand Down
17 changes: 17 additions & 0 deletions src/GShark/Geometry/Vector3.cs
Expand Up @@ -706,6 +706,23 @@ public static double DotProduct(Vector3 vector1, Vector3 vector2)
return result;
}


/// <summary>
/// The triple product of this vector and the two specified vectors.
/// </summary>
/// <param name="middle">The second vector.</param>
/// <param name="right">The third vector.</param>
/// <returns>The real number equal to the triple product.</returns>
///<remarks>
/// The scalar triple product is defined as the dot product of one of the vectors
/// with the cross product of the other two. Geometrically, this product is the (signed)
/// volume of the parallelepiped formed by the three vectors given.
/// </remarks>
public double TripleProduct(Vector3 middle, Vector3 right)
{
return DotProduct(this, CrossProduct(middle, right));
}

///<summary>
/// Determines whether this vector is perpendicular to another vector, within a provided angle tolerance.
///</summary>
Expand Down

0 comments on commit e1d76b6

Please sign in to comment.