$12256 / $11500

Monday, December 19, 2011 - 10:37

I want my program to determine in which 16th of a circle an object is from another.

Determining which octant it is based on whether the x or y difference is larger, and which ones are negative/positive, is trivial. I'm hoping that there's a similar way to do so for 16ths.

Basically, I'm a minimalist and perfectionist, I'd prefer to avoid using a technically imperfect slope lookup table or trigonometric functions, though I'm afraid that's impossible. Still, hoping someone here can prove me wrong.

joined 13 years 5 months agoI think you can either use an approximated lookup table OR use the trig functions, but you won't be able to avoid both. Best I could come up with is to have an approximated lookup table of tangent values for 22.5, 45, and 67.5 degrees (.4142, 1, and 2.4142, respectively), and compare abs(y/x) to each (warning, awful pseudocode):

Obviously some glaring flaws in it (doesn't account for (x == 0), for starters) but it might be a starting point.

Hope it helps!

-Moik

My project: Bits & Bots

joined 14 years 1 month agoOh, it's no problem if I'm not unwilling to do so. I had already calculated the relative x/y factors at these angles before realizing I only needed the slopes of the octant lines for handling movement; convert these to slopes and find whether the line between my two objects is above or below the line passing through its octant. It's just an issue of my having an irrational dislike for this method.

edit: Ach, neither [i][/i] nor <i></i> applied italics? o_O

joined 0 sec agoHave a look at the atan2(y,x) function. It's the inverse tangens function:

http://en.wikipedia.org/wiki/Atan2

Should deliver the angle in radians (pi range) from which you can calculate a value between 0 to 15.

joined 13 years 2 months agohey i found an approach for 8 directions which works great. this is coded in a script language that uses a state machine, but the logic is all here:

if "($xdistance > 0) and ($ydistance > 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "down_right"if "($xdistance < 0) and ($ydistance > 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "down_left"if "($xdistance > 0) and ($ydistance < 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "up_right"if "($xdistance < 0) and ($ydistance < 0) and ($absx > 0.414*$absy) and ($absx < 2.414*$absy)" "up_left"if "($xdistance > 0) and ($absx > $absy)" "right"if "($xdistance < 0) and ($absx > $absy)" "left"if "($ydistance > $absx) and ($absx < 0.5*$absy)" "down"if "($ydistance < $absx) and ($absx < 0.5*$absy)" "up"$absx and $absy are variables for the absolute values of x and y distance, respectively.

X is negative to the left, positive to the right, Y is negative up, positive down.

when the IF condition is met, the script jumps to the state specified in the last word, in this case, the directions.

thank you all (and a couple other team mates who wanted this badly :P )

"I was dancing with two kunai and a giant shuriken jumped out of the cake. Fun stuff!"

http://oxidmedia.deviantart.com/