• 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.

(GUI) Pick units in line

Status
Not open for further replies.
Level 37
Joined
Mar 6, 2006
Messages
9,243
JASS:
// Returns a boolean that tells whether the point is inside a rectangle.
    // (px , py) = Point to check
    // (cx , cy) = lower left corner of the rect
    // (ax , ay) = upper left corner
    // (bx , by) = lower right corner
    function IsPointInRect takes real px , real py , real cx , real cy , real ax , real ay , real bx , real by returns boolean        
        local real dot1 = (px-cx)*(ax-cx) + (py-cy)*(ay-cy)
        local real dot2 = (ax-cx)*(ax-cx) + (ay-cy)*(ay-cy)
        local real dot3 = (px-cx)*(bx-cx) + (py-cy)*(by-cy)
        local real dot4 = (bx-cx)*(bx-cx) + (by-cy)*(by-cy)
        
        return dot1 >= 0 and dot1 <= dot2 and dot3 >= 0 and dot3 <= dot4
    endfunction

or you can calculate points evenly split out from the caster, and pick units in range of those points and use IsUnitInRangeXY(unit, x, y , dist)
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Maker yours only works for horizontal/vertical rectangles. If you're trying to pick units in front of a unit, then there will likely be more complicated situations than horizontal/vertical.

This is the system. I'd wait for a moderator to approve it as there may be some flaws in it.

I don't know why that thread is closed. It is a perfectly legitimate solution, and Mag clearly overlooked the fact that rect objects cannot be tilted and must have static orientation, either horizontal or vertical.
 

nGy

nGy

Level 11
Joined
Apr 15, 2011
Messages
123
:D
i guess that the thread is closed is my own fault
when i first read mag's post i felt so embarassed for overlooking such a simple thing that i deleted the ressource instantly without thinking XD
then i updated it and now the thread is closed... i'll ask the moderators about opening it again
 
Last edited:
Level 10
Joined
Oct 5, 2008
Messages
355
I still need to test this out, but i think that this could be a usefull function for the problem, althought it is written in jass, you can only implement it in the map and call it with the custom script:
set udg_[Insert boolean here] = GetIsInOblong(x1,x2,x3,y1,y2,y3,width)
while being P1(x1/y1) and P2(x2,y2) the ends of the line and P3(x3,y3) the point to check. width is the width of the line.

JASS:
function GetIsInOblong takes real x1, real x2, real x3, real y1, real y2, real y3, real width returns boolean

//x1,x2,y1,y2 refers to the beginning of the middle line of the oblong, x3 and y3 to the point that needs to be checked
	local real a = 0.00
	local real b = 0.00
	local real lenght = SquareRoot((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1))
	local integer e = 0
	if x2-x1 == 0.00 then
		set e = e + 1
	endif
	if y2-y1 == 0.00 then
		set e = e + 2
	endif
//In one of these cases we got oblongs parallel to the x or y axis, the orthognal counterpart will have a lenght of 1
//ths system defines a coordinate system of following vectors: the vector from P1 to P2 and its orthogonal counterpart
// a and b are the coordinates of point P3-P1 in this coordinate system
// P1 (x1/y1) is the origin of the coordinate system
//The lenght of the vectors and the coordinates are in indicator to check if the point is in the oblong or not.
	if e > 0 then
		if e == 1 then
			set a = (y3 - y1) / (y1 - y2)
			set b = x3 - x1
		elseif e == 2 then
			set a = (x3 - x1) / (x1 - x2)
			set b = y3 - y1
		else
			return false
		endif
	else
		set a = ((y3 - y1) / (y2 - y1) + (x3 - x1) / (x2 - x1)) / 2.00
		set b = ((y3 - y1) / (y2 - y1) + (x3 - x1) / (x2 - x1)) / 2.00 / lenght
	endif
	return a <= 1 and a >= 0 and b <= width and b >= -1.00 * width

endfunction

Edit: oh, overlooked that were are on asymetrical quadruples now, sorry, there i have to look for another solution ^^

Edit 2: Sorry, the code has a failure, i created in my calculations a wrong normal line

Edit 3: this should be the right snippet:


JASS:
function GetIsInOblong takes real x1, real x2, real x3, real y1, real y2, real y3, real width returns boolean

//x1,x2,y1,y2 refers to the beginning of the middle line of the oblong, x3 and y3 to the point that needs to be checked
	local real a = 0.00
	local real b = 0.00
	local real lenght = SquareRoot((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1))
	local integer e = 0
	if x2-x1 == 0.00 then
		set e = e + 1
	endif
	if y2-y1 == 0.00 then
		set e = e + 2
	endif
//In one of these cases we got oblongs parallel to the x or y axis, the orthognal counterpart will have a lenght of 1
//ths system defines a coordinate system of following vectors: the vector from P1 to P2 and its orthogonal counterpart
// a and b are the coordinates of point P3-P1 in this coordinate system
// P1 (x1/y1) is the origin of the coordinate system
//The lenght of the vectors and the coordinates are in indicator to check if the point is in the oblong or not.
	if e > 0 then
		if e == 1 then
			set a = (y3 - y1) / (y1 - y2)
			set b = x3 - x1
		elseif e == 2 then
			set a = (x3 - x1) / (x1 - x2)
			set b = y3 - y1
		else
			return false
		endif
	else
		set b = ((y3 - y1) * (x2 - x1) - (x3 - x1) * (y2 - y1)) / ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
		set a = (x3 - x1 - (b * (y2 - y1))) / (x2 - x1)
		set b = b / lenght
		
	endif
	return a <= 1 and a >= 0 and b <= width and b >= -1.00 * width

endfunction
 
Last edited:
Status
Not open for further replies.
Top