0

I need check one condition regarding if the car is the similar (+/- 30 degrees) azimuth (compass direction) as the road.

Azimuth is an integer [0..359], 0 mean North

So if carAzimuth = 0 it is in the same general direction as other roads with roadAzimuth = [330, 345, 0, 15, 30]

enter image description here

So I create the formula to check for +/- 30

IF ( ABS(carAzimuth - roadAzimuth ) <= 30   OR
     ABS(carAzimuth - roadAzimuth ) >= 330  ) THEN // 360 -30   

I wonder if I can rewrite this without using ABS()

  • I don't think you can do this in a simpler way. But why would you want to? It looks good to me. – TonyK Jul 13 '16 at 23:28
  • @TonyK Im doing a db search and roadAzimuth has a index to speed up search. But if I use ABS() db cant use the index anymore because is a calculate value. So I need solve this using compare operator <>= and logic operator and or – Juan Carlos Oropeza Jul 14 '16 at 02:16
  • You can do this by computing the permissible values for roadAzimuth given carAzimuth and then restrict based on them. You'll have to use some extra logic to account for the fact that 360 degrees is the same as 0 degrees, and thus maybe have to branch to different queries depending on roadAzimuth, but you can do this with the index. – Jakob Hansen Jul 14 '16 at 05:08

1 Answers1

1

You could always use the definition of the absolute value. Take the function $ABS(x)=\left \{ \begin{array}{ll} -x & x\leq 0 \\ x & x > 0 \\ \end{array} \right. $

So if we let $d$ = carAzimuth-roadAzimuth we can write

( IF(d>0) THEN
(
    IF(d<=30 OR d>=330) 
)

OR

IF(d<0) THEN
(
    IF(-d<=30 OR -d>=330) 
) )

THEN //360-30
  • FYI the // 360 - 30 was supose to be a comment for why there is a 330 on the formula. Sorry if wasnt clear enough. My problem is I cant calculate d the same reason I cant calculate ABS() Im using this on a db query and that will cancel the option to use index to speed up the search. – Juan Carlos Oropeza Jul 14 '16 at 02:12