Calculate nearest vert

Blitz3D Forums/Blitz3D Programming/Calculate nearest vert

big10p(Posted 2004) [#1]
Hi

Given a point on a triangle, what's the best way to calculate the nearest vertex to that point?

I'm thinking maybe there's a clever way of doing this or is it just a case of calculating the distance to each vertex in the tri, then comparing results to find the shortest distance?

Cheers.


wmaass(Posted 2004) [#2]
bump, inquiring minds want to know. Currently I just pick one on a given triangle -- getting more precise would be more desireable. I have someone's code I got from Blitzcoder that has a closestvert() function but I'm not sure it does what you want. Look up"Vandred Open Source World Editor by bgilb v0.5"


Bot Builder(Posted 2004) [#3]
Well, the shortest dist method should work pretty well.....

There are tons of geometric properties for triangles. There might be a shortcut, but I'm going to bet the old pythag therom will work out to be better. I can think of some ways using angles and such, but those angles, and how to use those angles are going to be an altogether more cpu and RAM taxing thing.


big10p(Posted 2004) [#4]
Ok, I'll give the straight Pythag method a go. I haven't got round to testing it as I'm not quite at that point in my app, yet.

I was thinking it might be too slow for what i want because I'm going to need to be calculating the nearest vert every frame, according to where the mouse pointer is, er, pointing. :P

I dont have a math brain so I just wondered if there was a faster way. Cheers!

[edit] Oh, I'll have a look at that function on blitzcode, too, wmaass. ;)


poopla(Posted 2004) [#5]
Don't use the '^' opperator.


big10p(Posted 2004) [#6]

Don't use the '^' opperator.



Do you mean do sqr(x*x + y*y +z*z) and not sqr(x^2 + y^2 +z^2)? I always use the multiplication method, anyway, but I didn't know it was any faster than using ^.


DJWoodgate(Posted 2004) [#7]
Yes * is faster than ^. Also unless you need the actual distance you do not need the sqr either.


big10p(Posted 2004) [#8]

Also unless you need the actual distance you do not need the sqr either.



Ooh, good point! I told you I didn't have a math brain. :P


Michael Reitzenstein(Posted 2004) [#9]
Well, you can divide the triangle up with three lines of the form ax + by = c which, all the way along, are equidistant to two points. That will split the triangle up into six zones, and and for each vertex, two of these zones will represent the region in which any points in it are closest to that vertex. It's only going to be useful if you have one triangle and are going to test hundreds of points in that triangle - I'd imagine that the setup time would be higher than just doing three distance checks. Once you have setup the lines, though, you're laughing.