# Point in triangle (near edge)

6 replies to this topic

### #1Alienizer

Member

• Members
• 435 posts

Posted 26 August 2012 - 09:43 PM

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

### #2Vilem Otte

Valued Member

• Members
• 345 posts

Posted 27 August 2012 - 01:22 AM

Yep, there is...

For precise mathematical definition see wiki - http://en.wikipedia....es_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.
My blog about game development (and not just game development) - http://gameprogramme...y.blogspot.com/

If you don't know how to speed up application, go "roarrrrrr!", hit the compiler with the club and use -O3 :D

### #3rouncer

Senior Member

• Members
• 2722 posts

Posted 27 August 2012 - 06:56 AM

are u doing a line selector in a modellng program? i think ive even asked this question before, that was why.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #4Alienizer

Member

• Members
• 435 posts

Posted 27 August 2012 - 03:41 PM

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!

### #5Alienizer

Member

• Members
• 435 posts

Posted 27 August 2012 - 03:42 PM

rouncer, on 27 August 2012 - 06:56 AM, said:

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.

### #6Vilem Otte

Valued Member

• Members
• 345 posts

Posted 28 August 2012 - 10:33 PM

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).
My blog about game development (and not just game development) - http://gameprogramme...y.blogspot.com/

If you don't know how to speed up application, go "roarrrrrr!", hit the compiler with the club and use -O3 :D

### #7Alienizer

Member

• Members
• 435 posts

Posted 29 August 2012 - 04:14 AM

WOW thanks! it is a big project!

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users