Loading editor...

Fermat point of a triangle

Loading commands, almost done…

The Fermat point of a triangle is the point that minimizes the total distance to all three vertices. For triangles with all angles less than 120°, this point is unique and has the special property that the lines from it to each vertex form 120° angles.

Here we use gradient descent to find this point by iteratively minimizing the sum of distances to the triangle's vertices.

Setting up the triangle

  • : \point 0 3
  • : \point 5 0
  • : t = \triangle p0 p1 p2 - p2 is auto-generated to complete the triangle

These commands set up the trainable point and parameters without changing the visualization:

  • : p = \point 2 -1 - this is our initial guess for the Fermat point
  • : \param p - this tells the system that point p is trainable
  • : points = \array p0 p1 p2
  • : learning-rate = \scalar 0.5

Visualizing the connections

  • : \line p points - visualize the connections

  • : dists = \distance p points
  • : loss = \reduce-sum dists - this is what we want to minimize
  • : \backprop loss
  • : \gradient-descent-step

Click the command multiple times to continue training and watch point p converge towards the Fermat point. Run the \fermat-point-of-a-triangle command in the geomatic editor to set up the initial state, then repeatedly run \fermat-train-step to optimize.