Cookiemaster
New Member
- Reaction score
- 36
What does it do?
It "constructs" a triangle with the given points (AX,AY),(BX,BY),(CX,CY) and checks if point (DX,DY) is in it, returning a boolean.
Using triangles, you could also check if a point is in a certain-shaped area.
Simply use "or" conditions together with the triangle check, to check if a point is in any of the triangles, which would mean the point is in the shape.
The functions.
First, for it to work properly, it needed a function that returns an angle between two points as a radiant value between 0 and 2Pi. (Unlike the -Pi~Pi Atan2(y,x) returns.)
For that; there is this function:
With that, the triangle check is possible with simply this function:
Because people in GUI are too lazy to learn jass;
I've made a function that takes points and calls IsCoordInTriangle.
It's basically the same, but uses locations (A,B,C,D) instead of coords (AX,AY,BX,BY,CX,CY,DX,DY):
This can be used in GUI in this way:
P.S. LEAKS@@!!@!
Implementing.
Create a new trigger, convert it to custom text (under edit), and replace all the text with the following: (If you don't want to use the IsLocationInTriangle function, then just remove it yourself.)
Additionally, this could be slightly faster by creating two variables to replace 2*Pi and 3*Pi in the first two functions. For that; refer to this version that uses vJass: (And thus requires something that can compile it, such as Newgen.)
Inevitable Q&A:
Q: How does this work?
A: It checks the angles between the given points, compares them and with that, decides on which side of each line the point is. Then it can simply say if the point is in or outside of the triangle.
Q: Does it matter in which order you enter the points of the triangle?
A: No.
Q: Why isn't the location removed/destroyed/whatever in the location-version?
A: Maybe they might want to use the location somewhere in the trigger that does the check.
Q: YOUR GUI-TRIGGER LEAKS FOUR POINTS
A: OK
Q: How do I make round things (cones/circles/etc.) with this?
A: Not.
If the functions could in some way be slightly faster, please tell me so.
It "constructs" a triangle with the given points (AX,AY),(BX,BY),(CX,CY) and checks if point (DX,DY) is in it, returning a boolean.
Using triangles, you could also check if a point is in a certain-shaped area.
Simply use "or" conditions together with the triangle check, to check if a point is in any of the triangles, which would mean the point is in the shape.
The functions.
First, for it to work properly, it needed a function that returns an angle between two points as a radiant value between 0 and 2Pi. (Unlike the -Pi~Pi Atan2(y,x) returns.)
For that; there is this function:
With that, the triangle check is possible with simply this function:
JASS:
//Coords A, B and C are the corners of the triangle, coord D is the tested coord.
function IsCoordInTriangle takes real AX, real AY, real BX, real BY, real CX, real CY, real DX, real DY returns boolean
local real AB = GetAngleBetweenCoords(AX,AY,BX,BY)
local real BC = GetAngleBetweenCoords(BX,BY,CX,CY)
local real CA = GetAngleBetweenCoords(CX,CY,AX,AY)
local boolean clockwise = (((AB < BC and AB < CA) and BC > CA) or ((BC < AB and BC < CA) and AB > CA) or ((CA < AB and CA < BC) and AB > BC))
local real AD = GetAngleBetweenCoords(AX,AY,DX,DY) + (3*bj_PI - AB)
local real BD = GetAngleBetweenCoords(BX,BY,DX,DY) + (3*bj_PI - BC)
local real CD = GetAngleBetweenCoords(CX,CY,DX,DY) + (3*bj_PI - CA)
set AD = AD - ( I2R(R2I(AD/(2*bj_PI))) * (2*bj_PI) )
set BD = BD - ( I2R(R2I(BD/(2*bj_PI))) * (2*bj_PI) )
set CD = CD - ( I2R(R2I(CD/(2*bj_PI))) * (2*bj_PI) )
return (AD>bj_PI and BD>bj_PI and CD>bj_PI and clockwise == false) or (AD<=bj_PI and BD<=bj_PI and CD<=bj_PI and clockwise == true)
endfunction
Because people in GUI are too lazy to learn jass;
I've made a function that takes points and calls IsCoordInTriangle.
It's basically the same, but uses locations (A,B,C,D) instead of coords (AX,AY,BX,BY,CX,CY,DX,DY):
JASS:
//Locations A, B and C are the corners of the triangle, location D is the tested point.
function IsLocationInTriangle takes location A, location B, location C, location D returns boolean
local real AX = GetLocationX(A)
local real AY = GetLocationY(A)
local real BX = GetLocationX(B)
local real BY = GetLocationY(B)
local real CX = GetLocationX(C)
local real CY = GetLocationY(C)
local real DX = GetLocationX(D)
local real DY = GetLocationY(D)
return IsCoordInTriangle(AX,AY,BX,BY,CX,CY,DX,DY)
endfunction
This can be used in GUI in this way:
Trigger:
- Actions
- -------- LocationCorner is an array point variable. --------
- Set LocationCorner[1] = (Random point in (Playable map area))
- Set LocationCorner[2] = (Random point in (Playable map area))
- Set LocationCorner[3] = (Random point in (Playable map area))
- -------- LocationCheck is a point variable. --------
- Set LocationCheck = (Center of (Playable map area))
- -------- ReturnedBoolean is a boolean variable. --------
- Custom script: set udg_ReturnedBoolean = IsLocationInTriangle(udg_LocationCorner[1],udg_LocationCorner[2],udg_LocationCorner[3],udg_LocationCheck)
- -------- You could basically use any variable as substitute, but don't forget that variables made through the variable editor have udg_ as prefix. --------
- If (All Conditions are True) then do (Then Actions) else do (Else Actions)
- If - Conditions
- ReturnedBoolean Equal to True
- Then - Actions
- -------- The point LocationCheck is in the triangle formed by LocationCorner[1~3]. --------
- Else - Actions
- -------- The point LocationCheck is not in the triangle formed by LocationCorner[1~3]. --------
- If - Conditions
Implementing.
Create a new trigger, convert it to custom text (under edit), and replace all the text with the following: (If you don't want to use the IsLocationInTriangle function, then just remove it yourself.)
JASS:
function GetAngleBetweenCoords takes real AX, real AY, real BX, real BY returns real
local real angle = Atan2(BY-AY,BX-AX)
if angle < 0 then
return angle + 2*bj_PI
endif
return angle
endfunction
function IsCoordInTriangle takes real AX, real AY, real BX, real BY, real CX, real CY, real DX, real DY returns boolean
local real AB = GetAngleBetweenCoords(AX,AY,BX,BY)
local real BC = GetAngleBetweenCoords(BX,BY,CX,CY)
local real CA = GetAngleBetweenCoords(CX,CY,AX,AY)
local boolean clockwise = (((AB < BC and AB < CA) and BC > CA) or ((BC < AB and BC < CA) and AB > CA) or ((CA < AB and CA < BC) and AB > BC))
local real AD = GetAngleBetweenCoords(AX,AY,DX,DY) + (3*bj_PI - AB)
local real BD = GetAngleBetweenCoords(BX,BY,DX,DY) + (3*bj_PI - BC)
local real CD = GetAngleBetweenCoords(CX,CY,DX,DY) + (3*bj_PI - CA)
set AD = AD - ( I2R(R2I(AD/(2*bj_PI))) * (2*bj_PI) )
set BD = BD - ( I2R(R2I(BD/(2*bj_PI))) * (2*bj_PI) )
set CD = CD - ( I2R(R2I(CD/(2*bj_PI))) * (2*bj_PI) )
return (AD>bj_PI and BD>bj_PI and CD>bj_PI and clockwise == false) or (AD<=bj_PI and BD<=bj_PI and CD<=bj_PI and clockwise == true)
endfunction
function IsLocationInTriangle takes location A, location B, location C, location D returns boolean
local real AX = GetLocationX(A)
local real AY = GetLocationY(A)
local real BX = GetLocationX(B)
local real BY = GetLocationY(B)
local real CX = GetLocationX(C)
local real CY = GetLocationY(C)
local real DX = GetLocationX(D)
local real DY = GetLocationY(D)
return IsCoordInTriangle(AX,AY,BX,BY,CX,CY,DX,DY)
endfunction
Additionally, this could be slightly faster by creating two variables to replace 2*Pi and 3*Pi in the first two functions. For that; refer to this version that uses vJass: (And thus requires something that can compile it, such as Newgen.)
JASS:
globals
constant real II_PI = 2*bj_PI
constant real III_PI = 3*bj_PI
endglobals
function GetAngleBetweenCoords takes real AX, real AY, real BX, real BY returns real
local real angle = Atan2(BY-AY,BX-AX)
if angle < 0 then
return angle + II_PI
endif
return angle
endfunction
function IsCoordInTriangle takes real AX, real AY, real BX, real BY, real CX, real CY, real DX, real DY returns boolean
local real AB = GetAngleBetweenCoords(AX,AY,BX,BY)
local real BC = GetAngleBetweenCoords(BX,BY,CX,CY)
local real CA = GetAngleBetweenCoords(CX,CY,AX,AY)
local boolean clockwise = (((AB < BC and AB < CA) and BC > CA) or ((BC < AB and BC < CA) and AB > CA) or ((CA < AB and CA < BC) and AB > BC))
local real AD = GetAngleBetweenCoords(AX,AY,DX,DY) + (III_PI - AB)
local real BD = GetAngleBetweenCoords(BX,BY,DX,DY) + (III_PI - BC)
local real CD = GetAngleBetweenCoords(CX,CY,DX,DY) + (III_PI - CA)
set AD = AD - ( I2R(R2I(AD/(II_PI))) * (II_PI) )
set BD = BD - ( I2R(R2I(BD/(II_PI))) * (II_PI) )
set CD = CD - ( I2R(R2I(CD/(II_PI))) * (II_PI) )
return (AD>bj_PI and BD>bj_PI and CD>bj_PI and clockwise == false) or (AD<=bj_PI and BD<=bj_PI and CD<=bj_PI and clockwise == true)
endfunction
function IsLocationInTriangle takes location A, location B, location C, location D returns boolean
local real AX = GetLocationX(A)
local real AY = GetLocationY(A)
local real BX = GetLocationX(B)
local real BY = GetLocationY(B)
local real CX = GetLocationX(C)
local real CY = GetLocationY(C)
local real DX = GetLocationX(D)
local real DY = GetLocationY(D)
return IsCoordInTriangle(AX,AY,BX,BY,CX,CY,DX,DY)
endfunction
Inevitable Q&A:
Q: How does this work?
A: It checks the angles between the given points, compares them and with that, decides on which side of each line the point is. Then it can simply say if the point is in or outside of the triangle.
Q: Does it matter in which order you enter the points of the triangle?
A: No.
Q: Why isn't the location removed/destroyed/whatever in the location-version?
A: Maybe they might want to use the location somewhere in the trigger that does the check.
Q: YOUR GUI-TRIGGER LEAKS FOUR POINTS
A: OK
Q: How do I make round things (cones/circles/etc.) with this?
A: Not.
If the functions could in some way be slightly faster, please tell me so.