AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

computeCentroid

该函数用于计算凸包的重心。

语法

computeCentroid(points)

参数

  • pointsArray,一个包含 Point 对象的数组,表示凸包的顶点。

返回值

Point 对象,表示凸包的重心。

示例

const points = [
  new Point(0, 0),
  new Point(2, 0),
  new Point(1, 1),
  new Point(0, 2),
  new Point(2, 2),
];
const hull = ConvexHull.compute(points);
const centroid = ConvexHull.computeCentroid(hull);
console.log(centroid); // Point { x: 1, y: 1 }

实现原理

凸包的重心是所有顶点的平均值。计算重心的方法是将每个顶点的坐标相加,再除以顶点的总数。这可以用以下公式表示:

centroid_x = (x1 + x2 + ... + xn) / n
centroid_y = (y1 + y2 + ... + yn) / n

异常情况

如果 points 参数为空数组,则函数将抛出一个 Error,指示无法计算凸包的重心。如果 points 中包含的 Point 对象数量少于 3 个,则返回一个位于第一个顶点的坐标的 Point 对象,这是一种特殊情况。

注意事项

为获得更精确的结果,可以使用更多的顶点。但是,如果顶点太多,计算可能会变慢。