Jump to content
Tankbuster

isNearly? function request

Recommended Posts

Does anyone have anything in their library that could do this?

 

I'm checking the direction of road pieces and want to get those that more or less, are a given direction.

 

I know that road pieces don't ahve a direction so I'm getting the direction between my road piece and the next one. I'm then comparing that to the direction calculated from the original road peice to a nearby town. So basically - a road piece that heads towards the town.

 

But clearly, the chance of the direction of that road piece being exactly the same as that to the town are vanishingly small, so I need an isNearly function, ideal syntax is below;

 

[direction a, direction b, allowable deviation] call fnc_isNearly where allowable deviation is the allowed difference in direction. Returns a boolean. It would need to gracefully cope with directions near to 359.

 

Share this post


Link to post
Share on other sites

As I write this, I realise a few things. It doesn't need to be restricted to direction, any number can be supplied. But wait... direction is (sort of base 359) so, it wouldn't be able to accept any numbering scheme.

Share this post


Link to post
Share on other sites

Maybe I misunderstood but would  this do what you need?

 

GOM_fnc_isNumInRange = {

	params ["_toCheck","_reference","_variance"];

	systemchat format ["Checking if %1 is within %2 of %3!",_toCheck,_variance,_reference];
	_result = ((abs (_reference - _toCheck)) <= _variance);
	systemchat format ["Result: %1",["False","True"] select _result];
	_result

};

[round random 360,180,30] call GOM_fnc_isNumInRange;

Cheers

Share this post


Link to post
Share on other sites

That does look like it will do. I'll check when I get off the phone and on the PC. Thanks mate

  • Like 1

Share this post


Link to post
Share on other sites

Perfect! Thanks @Grumpy Old Man

But how can we solve the [359,0,10] call GOM_fnc_isNumInRange // false

Can the function be told that it's dealing with compass degrees?

 

Share this post


Link to post
Share on other sites

Try this, it'll check the numbers to the range [0, 360]:

GOM_fnc_isNumInRange = {
	params ["_toCheck", "_reference", "_variance"];

	systemchat format ["Checking if %1 is within %2 of %3!", _toCheck, _variance, _reference];
	
	_difference = abs (_reference - _toCheck);
	_result = ((360 - _difference) % 360) <= _variance || (_difference % 360) <= _variance;
	
	systemchat format ["Result: %1", ["False","True"] select _result];
	_result
};


 

  • Like 3

Share this post


Link to post
Share on other sites

@HallyG is quicker than me!
_difference = abs (_reference - _toCheck) mod 360;

_result = (360 - _difference) min  _difference;

 

  • Like 2

Share this post


Link to post
Share on other sites

That's great work. Thank you all so much.

 

I can now remove my brute force solution. I'm sure you all know how horrid that looked. :)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×