% MotionGenesis file: TriangleCMPosition.r
% Copyright (c) 2025-2026 Motion Genesis LLC.  All rights reserved.
%----------------------------------------------------------------------------
%TriangleCMPosition
%TriangleCMPosition
%
% Purpose:  Calculates a triangle's center of mass position from an arbitrary
%           point P, using position vectors from P to vertices on the triangle.
%
%Syntax 1:  TriangleCMPosition( p0>, p1>, p2> )
%Syntax 2:  TriangleCMPosition( p1>, p2> )
%
%   Input:  p0> -- position vector from point P to vertex 0.
%           p1> -- position vector from point P to vertex 1.
%           p2> -- position vector from point P to vertex 2.
%
%Output 1:  Position vector from P to the triangle's center of mass.
%Output 2:  Position vector from P to the triangle's center of mass, where
%           point P is vertex 0.
%
% Related:  SetMass          GetMass         SetInertia
%           GetCmPosition    GetCmVelocity   GetCmAcceleration
%           GetInertiaDyadic  GetInertiaMatrix  GetMomentOfInertia
%           GetProductOfInertia  GetRadiusOfGyration  GetInertiaVector
%	    TriangleArea       TriangleCMPosition     TriangleInertiaAboutVertex0
%	    TetrahedronVolume  TetrahedronCMPosition  TetrahedronInertiaAboutVertex0
%----------------------------------------------------------------------------
IF(     #NUM_ARGS# < 2 )  { echo("\n\a\kError: The command TriangleCMPosition has too few arguments.") }
ELSEIF( #NUM_ARGS# > 3 )  { echo("\n\a\kError: The command TriangleCMPosition has too many arguments.") }
ELSEIF( #NUM_ARGS# == 3 ) 
{ 
   (#1# + #2# + #3#)/3
%  TriangleCMPosition(#2# - #1#, #3# - #1#) + #1#  % Less efficient version.
}
ELSE 
{ 
   (#1# + #2#)/3 
}
% end of file
