File Exchange

image thumbnail

Distance between a point and a triangle in 3D

version (2.99 KB) by Gwendolyn Fischer
pointTriangleDistance calculates the distance between a point and a triangle in 3D.


Updated 14 Oct 2010

View Version History

View License

Calculate the distance of a given point P from a triangle TRI.
Point P is a row vector of the form 1x3. The triangle is a matrix
formed by three rows of points TRI = [P1;P2;P3] each of size 1x3.
dist = pointTriangleDistance(TRI,P) returns the distance of the point P to the triangle TRI.
[dist,PP0] = pointTriangleDistance(TRI,P) additionally returns the closest point PP0 to P on the triangle TRI.

Cite As

Gwendolyn Fischer (2021). Distance between a point and a triangle in 3D (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)


Thank you for sharing the function. I have a question about it.
To analyze the deformation of a surface, for example, I would like to monitor this surface (facade of a building) every 2 hours. So every 2 hours I get a new point cloud which I have to compare with a reference meshed surface and each point cloud consists of several points (about 400000 points), which have only x-, y- and z-coordinates. How can I use this function to represent the distance between the surface and the point cloud as a colored figure?
Thank you for your help and your suggestion.

may i know how to run the code?

Vladimír Duris

Sathish Thiyagarajan

Great function! I've found that using a different version of the dot product i.e. sum(E0.*E0) vs dot(E0,E0) speeds up execution considerably in Matlab 2016b.



Very nice, and faster than my implementation. However, there's an error somewhere in there. Try plugging the following triangle and point in:



tridist =
ans =

But tridist should be the same as the norm from the initial point to the closest point (output from the function as PP0). Plotting it, it looks like PP0 is getting to the right spot, so there must be an issue with that particular path through the code. Notice that if you truncate the values I gave above, the correct answer is reached (i.e. if you only use 4 places past the decimal).

Using R2010a


good job,thank you, i learn a lot.

Thomas O'Shea


Good work.

MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!