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.
ResourceFunction["BarycentricCoordinates"] gives the values λ1, λ2, …, λn such that and .
ResourceFunction["BarycentricCoordinates"] works in any dimension.

Examples

Basic Examples (2) 

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 (4) 

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 (1) 

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 (2) 

The dimensions of the points must agree with each other:

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

The dimension of the coordinate system should be one more than the dimension of the points:

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

Neat Examples (1) 

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]=

Publisher

SHuisman

Version History

  • 1.0.0 – 23 September 2019

Related Resources

License Information