- Feature Articles
- CodeSOD
- Error'd
-
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
Edit 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);