• 💀 Happy Halloween! 💀 It's time to vote for the best terrain! Check out the entries to Hive's HD Terrain Contest #2 - Vampire Folklore.❗️Poll closes on November 14, 2023. 🔗Click here to cast your vote!
  • 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 HD Level Design Contest #1 is OPEN! Contestants must create a maze with at least one entry point, and at least one exit point. The map should be made in HD mode, and should not be openable in SD. Only custom models from Hive's HD model and texture sections are allowed. The only exceptions are DNC models and omnilights. This is mainly a visual and design oriented contest, not technical. The UI and video walkthrough rules are there to give everyone an equal shot at victory by standardizing how viewers see the terrain. 🔗Click here to enter!

Line Location Distance.

Level 4
Joined
Dec 14, 2004
Messages
85
I was just working on my map and made some of these up. Thought I might as well post them if someone else wanted them and didn't know how to make them.

What this does is, the bottom function takes 3 locations, the 1st two form a line, and then it tells you how far the 3rd point is from the line that the 1st and 2nd make.

The above function finds which location along the line that the 1st and 2nd location make is nearest to the 3rd location.

JASS:
function GetClosestLocation takes location A, location B, location P returns location
    local location AP = Location(GetLocationX(P) - GetLocationX(A), GetLocationY(P) - GetLocationY(A))
    local location AB = Location(GetLocationX(B)- GetLocationX(A), GetLocationY(B) - GetLocationY(A))
    local location LocReturn
    local real ab2 = (GetLocationX(AB) * GetLocationX(AB)) + (GetLocationY(AB) * GetLocationY(AB))
    local real t = ((GetLocationX(AP) * GetLocationX(AB)) + (GetLocationY(AP) * GetLocationY(AB)))/ab2
    
    if t < 0.0 then
        set t = 0.0
    elseif t > 1.0 then
        set t = 1.0
    endif
    
    call MoveLocation(AB, GetLocationX(AB) * t, GetLocationY(AB) * t)
    
    set LocReturn = Location(GetLocationX(A) + GetLocationX(AB), GetLocationY(A) + GetLocationY(AB))
    call RemoveLocation(AP)
    call RemoveLocation(AB)
    set AP = null
    set AB = null
    
    return LocReturn
endfunction 

function LineLocationDistance takes location A, location B, location P returns real
    local location ClosestPoint = GetClosestLocation(A,B,P)
    local real ReturnThis = DistanceBetweenPoints(P, ClosestPoint)
    call RemoveLocation(ClosestPoint)
    set ClosestPoint = null
    return ReturnThis
endfunction

Now this isn't the version I actually use. Because I have made a VectorLoc struct in vJass to do this. So to get rid of the leak I would change LocReturn to a global variable.

I posted it this way so they could understand my explanation.
 
Last edited:
Top