Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / if variable# = othervariable# within a specified range

Author
Message
Section 812
18
Years of Service
User Offline
Joined: 24th Feb 2008
Location:
Posted: 15th Jan 2013 19:21
This is difficult to explain but here it goes.

basically what I want is to say is :

[ if variable# = othervariable# within a range of + or - 10 ]

I want the variable to have some leeway of 10

example: guy looking at a box = ok, guy looking 1 to 10 degrees of the box = ok, guy looking more than 10 degrees of box = not ok.

I hope I have explained it well.

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Jan 2013 20:18 Edited at: 15th Jan 2013 20:43
This is an out of bounds check, checking is a variable is out side of a lower or upper limit.

You can simply check if:

1. the variable is less than the lower limit:
(variable < lower)
2. the variable is greater than the upper limit:
(variable > upper)
3. put these together:
outOfBounds = (variable < lower) || (variable > upper)

If outOfBounds returns a 1 then your variable is out side of the limits.

4. Add the other variable to the lower and upper limits

Note: This: '||' is another way of writing 'or'
Section 812
18
Years of Service
User Offline
Joined: 24th Feb 2008
Location:
Posted: 15th Jan 2013 20:57
Thank you Sasuke, You are a wise warrior , with high cheekbones .

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th Jan 2013 22:17
if abs(a-b) < 10


Shh... you're pretty.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Jan 2013 23:09
I thought that at first but this bit confused me
Quote: "range of + or - 10"


I was thinking: if (a<(b-10))||(a>(b+10))
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 15th Jan 2013 23:20
Quote: "if (a<(b-10))||(a>(b+10)) "

=
Quote: "abs(a-b) < 10"


Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 15th Jan 2013 23:34
Actually for it to be the same it would need be greater than: abs(a-b) > 10.

The reason I didn't use it is because I thought he wanted to specify the lower and upper limits and '10' was just and example.
Section 812
18
Years of Service
User Offline
Joined: 24th Feb 2008
Location:
Posted: 15th Jan 2013 23:46
ok I've tried both, but I must be doing something wrong because ok# is not returning 1.



Lukas W
22
Years of Service
User Offline
Joined: 5th Sep 2003
Location: Sweden
Posted: 16th Jan 2013 12:33


The Taffer (+TaffED)
A thief inspired game along with the mission editor (DBPro). [on hiatus]
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th Jan 2013 14:31 Edited at: 16th Jan 2013 14:32
Quote: "Actually for it to be the same it would need be greater than: abs(a-b) > 10.
The reason I didn't use it is because I thought he wanted to specify the lower and upper limits and '10' was just and example."


Quote: "basically what I want is to say is: if variable# = othervariable# within a range of + or - 10 "


Okay I agree using "10" is bad form, let's replace it with c.
Now let's look at your equation (edited to check in-bounds): if (a > b-c) && (a < b+c)
a > b-c = b-a < c (method:
)
a < b+c = a-b < c (method:
)
therefore: (a > b-c) && (a < b+c) = (b-a < c) && (a-b < c) = abs(a-b) < c

Shh... you're pretty.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 16th Jan 2013 16:21
Haha, I like how we're really getting in depth in this

I meant you can't use abs(a-b) < c cause you can't define both ends of the range. So you'd need c for lower end and d for the upper end, so (a > b-c) && (a < b+d)
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 16th Jan 2013 17:35
Quote: "cause you can't define both ends of the range."

yes, but for the purpose Section 812 needs both ends need to be the same

Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 16th Jan 2013 23:13
Quote: "The reason I didn't use it is because I thought he wanted to specify the lower and upper limits and '10' was just an example."


Hence what I was talking about but OBese87 code is bang on for this
Section 812
18
Years of Service
User Offline
Joined: 24th Feb 2008
Location:
Posted: 17th Jan 2013 00:45
yes I did mean to have both ends the same.

I am grateful none the less, because too much information is always better than too little.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 17th Jan 2013 01:25 Edited at: 17th Jan 2013 01:25
I'm sure there is an efficient way to do this for angles and I think it involves 180 somewhere, but I can't figure it out.
Any ideas guys?

Shh... you're pretty.

Login to post a reply

Server time is: 2026-07-07 18:44:41
Your offset time is: 2026-07-07 18:44:41