Jump to content
Sign in to follow this  
malu05

Better Code for 3D Distance Calculation.

Recommended Posts

malu05

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 this post


Link to post
Share on other sites
malu05

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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×