• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[snippet] IsPointOnVector

JASS:
library IsPointOnVector

//checks to see if point px is on vector [(vx1, vy1), (vx2, vy2)]
function IsPointOnVector takes real vx1, real vy1, real vx2, real vy2, real px, real py returns boolean
    return (/*
    first see if px, py is on either end of the vector
    */(vx1 == px and vy1 == py) or (vx2 == px and vy2 == py)) or/*
    if no, make sure that the change in x is not 0
    */((vx2 != px and vx1 != px) and/*
    make sure slopes are equal (lies on the line)
    */((vy2-vy1)/(vx2-vx1) == (py-vy1)/(px-vx1)) and/*
    make sure that it's between end points of vector
    */((vx2 > vx1 and vx1 <= px and vx2 >= px) or (vx2 <= px and vx1 >= px))/*
    */)
endfunction

endlibrary
 
Last edited:
I've got an alternative function for you :D
JASS:
function IsPointOnVector takes real x1, real y1, real x2, real y2, real px, real py returns boolean
    return Atan2(py-y1, px-x1) == Atan2(y2-y1, x2-x1) and ((x1<=px  and px<=x2) or (x1>=px and px>=x2)) and ((y1<=py and  py<=y2) or (y1>=py and py>=y2)) 
endfunction
1. compares angle from p1 to p2 and angle from p1 to p
2. checks if px is between x1 and x2
2. checks if py is between y1 and y2

yours returns false for IsPointOnVector(0, 0, 0, 2, 0, 1) and IsPointOnVector(0, 0, -1, -1, -0.5, -0.5) btw
I don't know much about vectors, maybe it is supposed to be that way but it is strange to me


edit:
benchmarked both and yours is 10% faster! good job!
 
Last edited:
Level 22
Joined
Dec 31, 2006
Messages
2,216
Or how about this :D

JASS:
function IsPointOnVector takes real x1, real y1, real x2, real y2, real px, real py returns boolean
    local real length = SquareRoot((px-x1)*(px-x1)+(py-y1)*(py-y1))
    local real length2 = SquareRoot((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
    return (length <= length2) and ((px-x1)/length == (x2-x1)/length2) and ((py-y1)/length == (y2-y1)/length2)
endfunction

Vector version (vector v is the vector [x2-x1, y2-y1] and vector p is the vector [px-x1, py-y1])
JASS:
function IsPointOnVector takes vector v, vector p returns boolean
    return p.length() < v.length and p.normalized() == v.normalized()
endfunction

Naah just kidding, use the one above by D4RK_G4ND4LF :D
 
Top