• 🏆 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!

[snippet] GetIntersectionDistance

Level 19
Joined
Feb 4, 2009
Messages
1,313
We've got a line given by P1(x1, y1) and P2(x2, y2) as well as a ray given by P3(x3, y3) and angle a.
This function returns the distance from P3 to the intersection point.
The distance returned is negative if the ray points away from the line.

JASS:
library GetIntersectionDistance

function GetIntersectionDistance takes real x1, real y1, real x2, real y2, real x3, real y3, real a returns real
    local real m = x1-x2
    if m==0. then//avoid division by zero
        return (x1-x3)/Cos(a)
    endif
    set m = (y1-y2)/m
    if m==Tan(a) then//parallel (avoid division by zero again: Sin(a)-Tan(a)*Cos(a)=0)
        return 0.
    endif
    return (m*(x3-x1)+y1-y3)/(Sin(a)-m*Cos(a))
endfunction

endlibrary
 

Attachments

  • GetIntersectionDistance.w3m
    12.9 KB · Views: 76
Last edited:
Level 19
Joined
Feb 4, 2009
Messages
1,313
I think someone should just make one library with all these functions in it .__.

I think removing the "Small Code Snippets"-thread was a bad idea (and I lost some of my functions with it 'cause I didn't backup them -.-)
also
Your library needs to contain minimal functions specific to the library.[...]
You need to split these functions up into different libraries too... don't have a huge library of line snippets, they're just little mathematical operations ; P.

maybe a library with all kind of intersection functions would be more appropiate
I guess a mod will respond as soon as he sees 20 intersection-related threads x)
 
Top