Skip to content

Commit

Permalink
Merge pull request #405 from chuongmep/methodcenter
Browse files Browse the repository at this point in the history
Add Get Centroid method
  • Loading branch information
sonomirco authored Feb 6, 2023
2 parents 718b510 + 2fb40e2 commit 4c665f7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/GShark.Test.XUnit/Geometry/Point3Tests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using FluentAssertions;
using GShark.Geometry;
using haxe.ds;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -126,6 +125,22 @@ public void It_Divides_A_Point3d_By_A_Number()
// Assert
divisionResult.Equals(expectedPoint).Should().Be(true);
}

[Fact]
public void It_Returns_Centroid_Of_Collection_Points()
{
// Arrange
Point3 p1 = new Point3(1, 2, 3);
Point3 p2 = new Point3(4, 5, 6);
Point3 p3 = new Point3(7, 8, 9);
Point3 p4 = new Point3(10, 11, 12);
List<Point3> point3s = new List<Point3> { p1, p2, p3, p4 };
//Act
Point3 centroid = Point3.Centroid(point3s);
Point3 expectedCentroid = new Point3(5.5, 6.5, 7.5);
// Assert
centroid.Equals(expectedCentroid).Should().Be(true);
}

[Fact]
public void It_Returns_True_If_Two_Points_Are_Equal()
Expand Down
16 changes: 15 additions & 1 deletion src/GShark/Geometry/Point3.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GShark.Core;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GShark.Geometry
{
Expand Down Expand Up @@ -496,7 +497,20 @@ public static Point3 PointBetween(Point3 p1, Point3 p2)
{
return Interpolate(p1, p2, 0.5);
}


/// <summary>
/// Calculate the centroid of an arbitrary collection of points
/// </summary>
/// <param name="points">A collection of points.</param>
/// <returns>The centroid of the points</returns>
public static Point3 Centroid(IEnumerable<Point3> points)
{
IEnumerable<Point3> enumerable = points as Point3[] ?? points.ToArray();
return new Point3(
enumerable.Average(point => point.X),
enumerable.Average(point => point.Y),
enumerable.Average(point => point.Z));
}
/// <summary>
/// Constructs the string representation for the current point.
/// </summary>
Expand Down

0 comments on commit 4c665f7

Please sign in to comment.