Wolfram Research

Function Repository Resource:

BarycentricCoordinates

Source Notebook

Find the barycentric coordinates of a point

Contributed by: Sander Huisman

ResourceFunction["BarycentricCoordinates"][{p1,p2,},p]

finds the barycentric coordinates of point p in the coordinate system defined by the points pi.

Details and Options

For a point in n dimensions, the first argument should be n+1 points.
Find the values λ1, λ2, …, λn such that i=1nλipi=p and i=1nλi=1.
ResourceFunction["BarycentricCoordinates"] works in any dimension.

Examples

Basic Examples

Find the barycentric coordinates for the point {0.3,0.4} for the coordinate system {{1,1},{-1,1},{0,-1}}:

In[1]:=
pts = {{1, 1}, {-1, 1}, {0, -1}};
p = {0.3, 0.4};
ResourceFunction["BarycentricCoordinates"][pts, p]
Out[3]=

Check the finding:

In[4]:=
{0.5, 0.2, 0.3}.pts
Out[4]=

Scope

Exact input leads to exact output:

In[5]:=
ResourceFunction["BarycentricCoordinates"][{{-3}, {5}}, {5/2}]
Out[5]=

BarycentricCoordinates works in 1D:

In[6]:=
ResourceFunction["BarycentricCoordinates"][{{-3}, {5}}, {2.5}]
Out[6]=

BarycentricCoordinates also works in higher dimensions:

In[7]:=
ResourceFunction[
 "BarycentricCoordinates"][{{-3.98, -3.56, 2.78, -1.}, {2.68, -0.44, -2.68, -2.08}, {-2.46, -1.6, -4.08, 4.78}, {-4.84, 0.04, -1.78, -1.40}, {1.18, -3.6, -0.36, -4.36}}, {-0.517, \
-1.702`, -1.699`, -1.068`}]
Out[7]=

BarycentricCoordinates also works in very high dimensions:

In[8]:=
n = 100;
pts = RandomReal[{-5, 5}, {n + 1, n}];
p = RandomReal[{-1, 1}, n];
result = ResourceFunction["BarycentricCoordinates"][pts, p]
Out[11]=

Check that the barycentric coordinates add up to 1:

In[12]:=
Total[result]
Out[12]=

Recreate the point p and subtract it from the original:

In[13]:=
Chop[result.pts - p]
Out[13]=

Properties and Relations

The barycentric coordinates add up to 1:

In[14]:=
Total[ResourceFunction[
  "BarycentricCoordinates"][{{-3.98, -3.56, 2.78, -1.}, {2.68, -0.44, -2.68, -2.08}, {-2.46, -1.6, -4.08, 4.78}, {-4.84, 0.04, -1.78, -1.40}, {1.18, -3.6, -0.36, -4.36}}, {-0.517, \
-1.702`, -1.699`, -1.068`}]]
Out[14]=

Possible Issues

Dimensions of the points have to agree with each other:

In[15]:=
ResourceFunction[
 "BarycentricCoordinates"][{{1, 2, 3}, {3, 4}, {3, 4, 6}}, {1, 0.5, 1}]
Out[15]=

Dimensions of the points have to agree with each other:

In[16]:=
ResourceFunction[
 "BarycentricCoordinates"][{{1, 2, 3}, {3, 4, 6}, {1, -1, -4}}, {1, 0.5, 1}]
Out[16]=

Neat Examples

Interactively move the points of the coordinate system:

In[17]:=
Manipulate[
 Graphics[{EdgeForm[Black], FaceForm[], Polygon[p[[;; 3]]], Red, Text[#, p[[#]] + {0, 0.25}] & /@ Range[3]}, PlotRange -> 3, PlotLabel -> ResourceFunction["BarycentricCoordinates"][p[[;; 3]], p[[4]]]], {{p, {{1, 1.4}, {-1, 1}, {0, -2}, {0.3, 0.4}}}, Locator}]
Out[17]=

Resource History

Related Resources

License Information