0
109 Aug 26, 2012 at 21:43

If there a way to know how far a point is from the closest edge on a triangle? Perhaps in 0..1 range?

#### 6 Replies

0
117 Aug 27, 2012 at 01:22

Yep, there is…

For precise mathematical definition see wiki - http://en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)#Barycentric_coordinates_on_triangles - in short, barycentric coordinates for triangle are defined as 3 coordinates (U, V, W) where U + V + W = 1, and each represents unit-distance between our defined point and each point forming triangle (e.g. where U = 1 and V, W = 0, we’re sitting on point A; V = 1 and U, W = 0 we’re on point B, etc.).

Basically these coordinates are very important for interpolating values (this is important for pixel shading, of course I’ll give a word about these in article I’m working on), for example they’re needed for computing per-pixel normals or depth on pixel in triangle (+ only multiply and addition operations are needed when using these).

Also they’re wide used in ray-triangle tests - basically you test whether point projected on triangle’s plane along ray has coordinates between 0 and 1, and U + V + W = 1 (if any of the cases is wrong, then you haven’t hit the triangle, otherwise you hit) … plus you can actually use these right after for texture coordinate computation and such things.

0
103 Aug 27, 2012 at 06:56

are u doing a line selector in a modellng program? i think ive even asked this question before, that was why.

0
109 Aug 27, 2012 at 15:41

Thanks Vilem Otte, but basically, I’m trying to figure out how I can tell if a point is on the edge (or the lines forming the triangle). Because if we view that triangle on the screen from far, it is small, but the lines forming the triangle is still a pixel size, so internally, the line is much bigger than if we view that trianglr full screen!

0
109 Aug 27, 2012 at 15:42

@rouncer

are u doing a line selector in a modellng program? i think ive even asked this question before, that was why.

Yes, I’m trying to do a pick line on a triangle.

0
117 Aug 28, 2012 at 22:33

This reminds me of problem I had while developing one of my semestral projects in C# - 3D graphics modeller/ray tracer (you can place basic primitives or loaded meshes into scene and move/rotate/scale with them … also if you wanna try it, don’t forget to add light object - bind material with emissivity to object). First of all - http://www.otte.cz/Moderu.tar.gz - here is the C# project and code … it’s quite large for semestral project (and don’t worry if you’re not at university yet, you don’t have to make this large projects, I just wanted to do so), and it’s really slow ray tracer (I didn’t have time to optimize it - it was created over 2 weekends, while also I had to do projects in Haskell).

But to the point - there is a code to calculate line-to-line distance - and also picking axis to move the body in editor. Basically the problem is same as yours, you want to find line-to-line distance between a ray from camera position in direction of “mouse position” on screen, and each edge of triangle in which you’re going to select an edge (e.g. probably triangle under your cursor, but you might also need to search it’s neighbors). All the math is actually in the code - and I think it’s also a bit commented (so you can find the stuff easily) + it’s in C# and thats very descriptive language (oh a strong effect of C++ can be seen on the code, as I name stuff like in C++ - don’t blame me for this please).

0
109 Aug 29, 2012 at 04:14

WOW thanks! it is a big project!