[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can any of you code monkeys make this shorter? // returns an
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 30
Thread images: 1
File: UnitCircle.png (98 KB, 624x510) Image search: [Google]
UnitCircle.png
98 KB, 624x510
Can any of you code monkeys make this shorter?

// returns an int from N to NW clockwise

public static final double direction(Point foo, Point bar) {
double angle = toDegrees(atan2(bar.y - foo.y, bar.x - foo.x)); if(angle < 0) angle+=360;
if(angle > 247.5 && angle < 292.5) return 1; if(angle > 67.5 && angle < 112.5) return 5;
if(angle > 292.5 && angle < 337.5) return 2; if(angle > 112.5 && angle < 157.5) return 6;
if(angle > 337.5 || angle < 22.5) return 3; if(angle > 157.5 && angle < 202.5) return 7;
if(angle > 22.5 && angle < 67.5) return 4; if(angle > 202.5 && angle < 247.5) return 8;
}
>>
>>55479615
yes
>>
>>55479615
what the fuck anon.
>>
>>55479643

i need to know if something is specifically "north" or "east" or "south west" or etc

its for a really stupid application that requires an int direction as a parameter
>>
>>55479615
Retarded.
>>
>>55479615
First learn to use else

Then, learn to correctly use <,<=,==,!=,>=, and >
>>
>>55479665
All your regions cover the same size in angles and are sequential. Using modulo to account for angles <0 or >360, this is easy as fuck
>>
subtract 45/2, get residue modulo 360, divide by 45 and floor, then add 1 probably idk
>>
>>55479692
modulo is the answer

>>55479682
no need for else with returns idiot
also java doesn't have a multi-equality function like a < b < c, so unfortunately this looks terrible
>>
i've failed more math than math majors have to take, and you, op, are a retard.

underaged b&
>>
>>55479615
in Python (assuming 3, so / is float division and // is integer division)
def ops_homework(n):
return ((n-22.5)//45 + 3) % 8 + 1
>>
>>55479728
You are so stupid.
You are so stupid it baffles me.
Every time. Every fucking time I come to /g/ I'm disappointed by how stupid they are, but you're the worst I think I've ever seen.

I can handle retards who are aware how stupid they are, because they are able to learn, but you, you are stuck.

>no need for else with returns
Except that will never be the case, because checking if x < y and then rechecking for x >= y is NEVER faster than assuming that x >= y.

>multi-equality
That's not what I was saying you dumb fucking retard. What happens if I gave OPs method foo and bar such that angle = 22.5
>>
>>55479615
Here's it in Emacs Lisp. Translate it to Java yourself.

(defun direction (angle-in-degrees)
(cond ((< (- angle-in-degrees 22.5) 0) 3)
(t (mod (+ 3
(ceiling (/ (- angle-in-degrees 22.5)
45)))
8))))

;; provides expected results
(direction 290) ;; => 1
(direction 340) ;; => 3
(direction 210) ;; => 8
>>
you're welcome by the way
>>
>>55479615
Can I just question why you have that particular return order?

Like, could it be modified to return 1 for angles from 67.5 to 112.5, etc?
>>
>>55479615
P sure this is a troll.
>>
>>55480047
it would be the same exact algorithm otherwise with the python and lisp solution, just adding a different number before modulo 8
>>
>>55480076
Oh right, so basically if you do a division of the angle by 45, round it, then add 3 and subtract 8 if it is over 8 then you get the value.
>>
>>55480238
So
public static final double direction(Point foo, Point bar) {
double angle = toDegrees(atan2(bar.y - foo.y, bar.x - foo.x));
if(angle < 0) angle+=360;
int step = Math.round(angle / 45) + 3;
if (step > 8) return step - 8;
else return step;
}
should work if I am not entirely retarded. I can't be bothered trying in an IDE.
>>
>>55480264

public static final double direction(Point foo, Point bar) {
double angle = toDegrees(atan2(bar.y - foo.y, bar.x - foo.x)) % 360;
int step = Math.round(angle / 45) + 3;

return step % 8;
}
>>
>>55480282
public static final double direction(Point foo, Point bar) {
return (Math.round((toDegrees(atan2(bar.y - foo.y, bar.x - foo.x)) % 360) / 45) + 3) % 8;
}
>>
>>55480282
>>55480303
Actually, this is wrong because it will return 0 when step is 8, we want it to return 8. There is no 0 return value unless we modify that.
>>
>>55479615
I won't make your fucking code, but here's some pseudocode.

1. Divide angle by 45;
2. Discard the non-integer part;
3. If result smaller than 5, subtract 4; otherwise, add 4.
>>
>>55479615
its beautiful top notch work op I cry every tiem
>>
>>55479994
vim is better
>>
>>55481206
Actually,
0. Subtract 22.5 from the angle

Otherwise half of the results will be wrong by one.
>>
>>55479615
Fuck, this looks like one of those CS graduate meme programs.
>>
>>55479615
You need to be especially careful here anon because a traditional compass places North at 0 and increments clockwise such that West is 90, South 180 etc. Polar coordinates(atan2) place 0 where you would expect West to reside (right hand side of the compass) and increment widishins such that North would be 90(or pi/2 in sensible units) and South 270. I do not know how your "app" expects "direction" but double check that you are not working backwards round the clock so to speak.
Put the function to find the angle between the two points in the Point class and, if you insist on working in degrees, add 22.5 + some offset constant (so your numbers start at the correct point around the circle) before taking the modulo. You will have to add 1 to this result (another good reason why computer scientists traditionally index from 0)

You should have asked this in the dpt. Good luck anon, you've probably moved on from this thread but if not; report back when you get it working.
>>
>>55481911
I don't think you know where west is
>>
>>55482299
Oh my apologies, I meant East, should have proof read what I wrote. I hope it was clear from the rest of the post.
Thread replies: 30
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.