Code archives/3D Graphics - Maths/PickedVertex()

This code has been declared by its author to be Public Domain code.

Download source code

PickedVertex() by KimoTech2005
Returns the nearest picked vertex.
; I havent tested it much yet, so probertly some bugs :)

Function PickedVertex()
surf=PickedSurface()
tri=PickedTriangle()
v1=TriangleVertex(surf,tri,1)
v2=TriangleVertex(surf,tri,2)
v3=TriangleVertex(surf,tri,3)

Dist1X#=Abs(VertexX#(surf,v1)-PickedX#())
Dist1Y#=Abs(VertexY#(surf,v1)-PickedY#())
Dist1Z#=Abs(VertexZ#(surf,v1)-PickedZ#())
Dist1#=Dist1X#+Dist1Y#+Dist1Z#

Dist2X#=Abs(VertexX#(surf,v2)-PickedX#())
Dist2Y#=Abs(VertexY#(surf,v2)-PickedY#())
Dist2Z#=Abs(VertexZ#(surf,v2)-PickedZ#())
Dist2#=Dist2X#+Dist2Y#+Dist2Z#

Dist3X#=Abs(VertexX#(surf,v3)-PickedX#())
Dist3Y#=Abs(VertexY#(surf,v3)-PickedY#())
Dist3Z#=Abs(VertexZ#(surf,v3)-PickedZ#())
Dist3#=Dist3X#+Dist3Y#+Dist3Z#

If Dist1#<Dist2# And Dist1#<Dist3# Then Return v1
If Dist2#<Dist1# And Dist2#<Dist3# Then Return v2
If Dist3#<Dist2# And Dist3#<Dist1# Then Return v3
Return v1

End Function

Comments

big10p2005
I havent tested it much yet, so probertly some bugs
Indeed there are. :)

Also not tested, but you may have better luck with this:
Function PickedVertex()

	mesh = PickedEntity() 
	surf = PickedSurface()
	tri = PickedTriangle()

	px# = PickedX()
	py# = PickedY()
	pz# = PickedZ()
	
	v0 = TriangleVertex(surf,tri,0)
	v1 = TriangleVertex(surf,tri,1)
	v2 = TriangleVertex(surf,tri,2)
	
	TFormPoint VertexX(surf,v0),VertexY(surf,v0),VertexZ(surf,v0),mesh,0
	dx# = TFormedX - px
	dy# = TFormedY - py
	dz# = TFormedZ - pz
	v0d# = dx*dx + dy*dy + dz*dz

	TFormPoint VertexX(surf,v1),VertexY(surf,v1),VertexZ(surf,v1),mesh,0
	dx# = TFormedX - px
	dy# = TFormedY - py
	dz# = TFormedZ - pz
	v1d# = dx*dx + dy*dy + dz*dz

	TFormPoint VertexX(surf,v2),VertexY(surf,v2),VertexZ(surf,v2),mesh,0
	dx# = TFormedX - px
	dy# = TFormedY - py
	dz# = TFormedZ - pz
	v2d# = dx*dx + dy*dy + dz*dz
	
	If (v0d < v1d) And (v0d < v2d) Then Return v0
	If (v1d < v0d) And (v1d < v2d) Then Return v1

	Return v2

End Function



KimoTech2005
thanks! but thats only my first code post.. :) just for try. Heh.


Code Archives Forum