# Better Code for 3D Distance Calculation.

## Recommended Posts

Im trying to optimize my traveling functions.

One of the functions that is playing systemHog is this distance calculation function.

The Array with points (X Y and Z) is defined in the "\$Point" variable.

While the position i want to find the nearest point to is defined in the function.

This function seem quite slow, and I wonder if there is a better way to get the smallest value in a array than this:

```;;================================================================================
;;GetNearestPoint(\$OBJx,\$OBJy,\$OBJz)
;;***************
;; Used to get the point nearest to the Position Defined
;; X,Y and Z is the location that you want to find the nearest point to.
;;--------------------------------------------------------------------------------
;;(\$OBJx,\$OBJy,\$OBJz) Is the Position you want to find the closest point to.
;;
;;Point Array Format
;;\$Point[\$i][1] X
;;\$Point[\$i][2] Y
;;\$Point[\$i][3] Z
;;================================================================================
func GetNearestPoint(\$OBJx,\$OBJy,\$OBJz)
local \$NPCDistancelists[1][1]
local \$predist
local \$count = 1
local \$checker = 0
for \$i = 1 to \$WPAmmount step +1  ;Calculation Loop
redim \$NPCDistancelists[\$count+1][2]
\$dx = \$OBJx-\$Point[\$i][1]
\$dy = \$OBJy-\$Point[\$i][2]
\$dz = \$OBJz-\$Point[\$i][3]
\$NPCDistancelists[\$count][0] = \$count
\$NPCDistancelists[\$count][1] = sqrt(\$dx*\$dx + \$dy*\$dy + \$dz*\$dz) ;Distance Calculation
\$count = \$count + 1
Next ;Sort to find the smallest value.
_ArraySort( \$NPCDistancelists,0,1,0,1)
return \$NPCDistancelists[1][0] ;Return The point ID
EndFunc ;==>GetNearestPoint```

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

##### Share on other sites

Seems like ReDim is moving each entry since the more values that are in the array the slower redim is.

So by removing the Redim function and just dim it at the length it was supposed to be it goes alot faster.

This update fixed it:

```;;================================================================================
;;GetNearestPoint(\$OBJx,\$OBJy,\$OBJz)
;;***************
;; Used to get the point nearest to the Position Defined
;; X,Y and Z is the location that you want to find the nearest point to.
;;--------------------------------------------------------------------------------
;;(\$OBJx,\$OBJy,\$OBJz) Is the Position you want to find the closest point to.
;;
;;Point Array Format
;;\$Point[\$i][1] X
;;\$Point[\$i][2] Y
;;\$Point[\$i][3] Z
;;================================================================================
func GetNearestPoint(\$OBJx,\$OBJy,\$OBJz)
local \$NPCDistancelists[WPAmmount +1][1]
local \$predist
local \$count = 1
local \$checker = 0
for \$i = 1 to \$WPAmmount step +1  ;Calculation Loop
\$dx = \$OBJx-\$Point[\$i][1]
\$dy = \$OBJy-\$Point[\$i][2]
\$dz = \$OBJz-\$Point[\$i][3]
\$NPCDistancelists[\$count][0] = \$count
\$NPCDistancelists[\$count][1] = sqrt(\$dx*\$dx + \$dy*\$dy + \$dz*\$dz) ;Distance Calculation
\$count = \$count + 1
Next ;Sort to find the smallest value.
_ArraySort( \$NPCDistancelists,0,1,0,1)
return \$NPCDistancelists[1][0] ;Return The point ID
EndFunc ;==>GetNearestPoint```

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

## Create an account

Register a new account

×

• Wiki

• Back

• Git