Warcraft III resources & community, 2003–2006 · archived

IsPointInLine()

not rated
Submitted by N-a-z-g-u-lFunctions0 downloads
This function returns, if the given point is in a line. You have to give the width of the line to the function. I am going to use this for magic fences, which are going to be lightnings from one fence "station" to another one. As this requires many function calls, any suggestions are welcomed! The function is based on different other codes from wc3sear.ch^^ (Grater: http://www.wc3sear.ch/index.php?p=JASS&ID=152)

Source

// Note: The taken width is the distance in one direction, the whole width of the quad will be 2*width.

function IsPointInLineWidth takes real x, real y, real x1, real y1, real x2, real y2, real width returns boolean
	// This function works like this: At both ends of the line 90° polar-offset coordinates into both directions create one quad,
	// in which the taken coordinates have to be.

	// The coordinate-changes after polar-offset
	local real temp = (bj_RADTODEG * Atan2(y2-y1, x2-x1) + 90) * bj_DEGTORAD
	local real xadd = width * Cos(temp)
	local real yadd = width * Sin(temp)

	// Setting the coordinates used for the DoesQuadContainOrigin-Check, origin = P(0|0)
	local real x1q = x1-x+xadd
	local real y1q = y1-y+yadd
	local real x2q = x1-x-xadd
	local real y2q = y1-y-yadd
	local real x3q = x2-x-xadd
	local real y3q = y2-y-yadd
	local real x4q = x2-x+xadd
	local real y4q = y2-y+yadd

	// DoesQuadContainOrigin by Grater (only using a boolean instead of an integer)
	local boolean FirstCheck = (x1q-x2q)*y1q < (y1q-y2q)*x1q
	if (x2q-x3q)*y2q < (y2q-y3q)*x2q != FirstCheck then
		return false
	endif
	if (x3q-x4q)*y3q < (y3q-y4q)*x3q != FirstCheck then
		return false
	endif
	return (x4q-x1q)*y4q < (y4q-y1q)*x4q == FirstCheck
endfunction

function IsPointInLineWidthSimple takes real x, real y, real x1, real y1, real x2, real y2, real width returns boolean
	// This function is useful, if you want to check lots of units, because it does not have to call IsPointInLineWidth everytime.
	// The check works like checking if a coordinate is outside a given rect,
	// with the only difference that no Max / Min values are given and it the function has to check both values.
	// To do not cut the quad we have to make the rect a bit bigger (+width).

	// I really recommend to use this function unless you limit the checked coordinates before.

	if x > x1+width and x > x2+width then
		return false
	endif
	if x < x1-width and x < x2-width then
		return false
	endif
	if y > y1+width and y > y2+width then
		return false
	endif
	if y < y1-width and y < y2-width then
		return false
	endif
	return IsPointInLineWidth( x, y, x1, y1, x2, y2, width )
endfunction

Comments

4
well, youre right... i were just too lazy to search for the name again^^

btw, i have some problems with my lightnings, they overlay everything, even objects standing in front of them...
you should probably credit the origional authors of the various parts, if you CnPed.
sorry, no, i can't... thats the part i found on wc3sear.ch
the *q variables safe the coordinates of a quad along the line width the given width, the counter-part is from a function checking if a coordinate is in the given quad... (and the function where i have this from is based on AIAndy, thats what i still remember^^)
Hmm, can you add some comments so we know what you hope to accomplish with all those (x/y)(1-4)q variables (and their if statements)