- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Scrollpocalypse
- Fast Quick
- 12345
- Said Different
- Ai Dios Mio
- Unrewarding
- Good Time
- Hello, New Mexico!
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
You might do that, but I wouldn't, or at least not with a strict application of "simple distance calculation". I would compare
deltaX*deltaX + deltaY*deltaYtoradius*radiusto avoid calculating the square root that's required for a "simple distance calculation".Admin
Am I missing something? This is not looking for a box, it is looking for a specific point?
Admin
Yeah, pretty much what I did, except for integer overflow, and floating point was too slow…
Admin
Yes, but the nested for loops are iterating over every point in a radius x radius box to see if the desired point is in there. You could get a false positive if the point is in the corner of the box, outside the radius circle.
Admin
A 'simple' distance calculation is not WTFy enough... try this.
double deltaX = PositionX - point.X; double deltaY = PositionY - point.Y; double angle = double.Atan2(deltaX, deltaY); double distance = deltaX * Math.Sin(angle) + deltaY * Math.Cos(angle);
Admin
+1 for knowing the Taxicab metric
Admin
From center point, send an infinite number of messengers to all possible directions. Give all of them food and water to survive 2 x radius travel. Tell them to turn back if they find the Point. If someone gets back, return true, otherwise return false.
Admin
If you need the box radius like this loop does, just use something like this
abs(PositionX - point.X) <= radius && abs(PositionY - point.Y) <= radius
Admin
Isn't this more of a Chebyshev distance check than a taxicab one? The taxicab or Manhattan "circle" would have diagonal sides, not axis-aligned ones.
Admin
Well, since we are sharing entries here is mine:
Admin
Also, for some reason the code tag cut off my first line. It's supposed to be: struct Point
Admin
I'm not going to try to post any full code here, but I thought the following was bad:
A rectangle of pixels about 1600x800; the bottom center was the origin. Data supplied as a line of radar weather data; angle + row of weather density pixels. Solution was to do polar to rectangular conversions on each angle and "pixel" on the line. Problems: The origin was recomputed for every single angle in the supplied data. There were gaps between angles far from the origin. It was all computed in double precision despite the general lack of precision of the original data and the integral nature of the pixels. Worse yet, it was real-time data and the compute slot of the display was constantly overrunning. (No surprise there.)
I started over and pre-computed angle and distance of all pixels, including anti-aliased pixels between angle sweeps. Quick lookup and all the trig was gone. Got it under the time-slice time with time to spare.
Admin
Yes, you are right.
Admin
That's one heck of a Speedup Loop. ( https://thedailywtf.com/articles/The-Speedup-Loop )
Admin
Admin
When the coding and understanding of a solution is this bad, you have to point your finger at the hiring process and oversight instead of the coder himself.
Admin
One of the more interesting metrics since we're talking about calculations is how floating point math almost never uses the FPU anymore. It's almost always more efficient to do it using the SIMD instructions so the distance formula is best done using a single SIMD multiply. Especially if you have AVX available and thus can operate on 4 doubles at the same time. You can eliminate square root and do a 3D distance calculation with a single AVX multiply and then likely a masked addition and thus in two instructions do the calculation.
Even the FPU would require many more instructions.
Admin
Ah yes, the radius of a square.
When I tried out ChatGPT 2 something years ago, it actually produced similar code for a simple math problem. Turns out after googling that the code was heavily upvoted on LinkedIn (the platform of incompetence honestly) and after googling a bit more, I found out that 10% of the training material was LinkedIn crap (40% was reddit, I guess that offset the worst offenders mostly - dunno). So yeah, in the old days incompetent devs copied this stuff from stackoverflow, these days they copy it from token generators. Same same, nothing changes, incompetence just seems to have many pretty colors.
Admin
Now THIS is what thedailywtf.com is for :-)
Admin
Using Pythagorus is a simple distance calculation.
Admin
This is a countable infinity. The points on a plane are uncountable, so there is a chance you'll miss the point.
The above argument does not apply if the x and y coordinates are both integers.
Admin
Except it does not calculate the distance, it calculates the distance squared (which is why you have to square the radius).
Admin
If x and y are both integers, you don't need to send out infinite messengers, just send one to each point with radius.
Admin
Unless I'm missing something obvious, the original question assumes: 1] That the input point is from the centre of 0,0 2] The radius extends from the same centre
If those are correct, then the calculation is very simple: InRadius = (point.x ^2 + point.y^2) <= radius^2
Admin
Yes, but like that's far too efficient and clean and actually has no side effects and gets the job done. Hell, it does not even cause the stack to overflow like mine would.