24 votes
Accepted

Intersection over Union for rotated rectangles

1. Review The large bounty suggests that getting a review of this code is important. But there are some other things you could have done to make reviewing this code more productive: Give us some ...
Gareth Rees's user avatar
  • 49.6k
19 votes
Accepted

Determine if a triangle is equilateral, isosceles, or scalene

A triangle has, by definition, three sides. I find it therefore weird to take a single sides argument, which could be of any size. This opens you up to obscure bugs,...
Graipher's user avatar
  • 40.9k
15 votes
Accepted

Triangle area computation and linear transformations

Here are a few of the high- and low-level ideas: too many comments. Remember, comments tend to age and outdate, they need to be maintained as the code changes. And, if they are over-used, they hurt ...
alecxe's user avatar
  • 17.2k
12 votes
Accepted

Program to see how many points lie strictly inside a convex polygon

Data types Why not use a simple point struct instead of std::pair<int, int>? It doesn't have to do anything, just a ...
hoffmale's user avatar
  • 6,468
11 votes

Generate sample coordinates inside a Polygon

Rejection sampling was proposed in comments on the other answer. The problem with rejection sampling is that the area of a polygon can be an arbitrarily small fraction of its bounding box, for example:...
Gareth Rees's user avatar
  • 49.6k
11 votes
Accepted

Check if 2 rectangles fit in another one, given their length and height

Any two rectangles can fit into a third rectangle (without overlapping with each other) only under 2 cases: As mentioned in the question, consider: x,...
Cherubim's user avatar
  • 391
11 votes

Area of the triangle generated by the hands of a clock

This is an addendum to Oscar's answer. Just doing some math. Let t = 0 be 12 o'clock, when all hands are coinciding and the area is 0. We need to calculate the ...
polfosol's user avatar
  • 213
10 votes

N dimensional cubes

Generation of vertices You could use itertools.product to generate vertices. You can ...
SylvainD's user avatar
  • 29.2k
10 votes
Accepted

Counting points on a given irregular grid that are inside or outside a circle

Well, you are manually testing every point \$(X_i, Y_j)\$ to see where it falls. You shouldn't, really. Remember the sage advice: If in doubt, sort. A better algorithm goes: Read all X-coordinates. ...
Deduplicator's user avatar
  • 19.1k
10 votes

Distance too close

Use types consistently I prefer double over Double, but it doesn't really matter which you use as long as it's consistent. ...
Peter Taylor's user avatar
  • 24.1k
9 votes

Order a list of points by closest distance

I don't like any of these answers. In particular, the accepted answer is simply wrong. Let's start by critiquing the interface: ...
Eric Lippert's user avatar
  • 14.9k
9 votes
Accepted

Farthest point algorithm in Python

First, a style comment. On the internet, especially in programming, and in particular on this website, English is the lingua franca. So you should avoid mixing other languages and English. This way ...
Graipher's user avatar
  • 40.9k
9 votes

Intersection over Union for rotated rectangles

There are two main reasons why your program is slow and using huge amounts of memory: You are using a 768×768 pixel image for each overlap calculation You are checking each anchor box against each ...
G. Sliepen's user avatar
8 votes

Parsing shorts from binary file and finding the closest and furthest points

Given that this was for an interview I'll refrain from saying that Javadoc is missing. private static final Point closest = new Point(-200, 300); That's a ...
Bobby's user avatar
  • 7,712
8 votes
Accepted

Template class for Andrew's Monotone Chain Algorithm Convex Hull

SPOILER ALERT: you don't want a class at all; you want a function. But first, the style nitpicks! :) namespace ConvexHull { void foo(); ... } You'll find ...
Quuxplusone's user avatar
  • 19.3k
8 votes

Area of the triangle generated by the hands of a clock

So the first place to look is your area function. This currently uses 15 trig operations to calculate the area of a triangle (3 ...
Oscar Smith's user avatar
  • 3,597
8 votes

Area of the triangle generated by the hands of a clock

Avoid using namespace std; Importing all names of a namespace is a bad habit to get into, and can cause surprise when names like ...
Toby Speight's user avatar
  • 74.4k
7 votes

Python implementation of the Ramer-Douglas-Peucker Algorithm

This is quite an old question, yet I'd like to answer because it comes up if one searches for "Python Ramer-Douglas-Peucker" on the internet. First, the implementation is incorrect! The OP ...
Unapiedra's user avatar
  • 246
7 votes
Accepted

Compute the perimeter of a polygon

consec will wrap around and pair the last element of your list as consecutive with the first. This isn't documented in the comment and IMO a little bit problematic. ...
Vogel612's user avatar
  • 25.2k
7 votes

Area of the triangle generated by the hands of a clock

The area of the triangle given points A, B, C is half the magnitude of the cross product of vectors AB and AC. The points are defined in 3D, with a zero z component. The cross product will only be non-...
Cris Luengo's user avatar
  • 5,503
7 votes
Accepted

Area of the triangle generated by the hands of a clock

The first thing to do is to get rid of trig functions which are slow (15-30 cycles) and replace them with precomputed tables. Since the hands have only 60 possible positions each this is a 60-slot ...
bobflux's user avatar
  • 276
7 votes

Program to see how many points lie strictly inside a convex polygon

I'm unfamiliar with the algorithm, so I cannot speak for that. perdot() The function can be declared with the constexpr ...
esote's user avatar
  • 3,770
7 votes
Accepted

Where is the most desolate place in Colorado?

If this were a problem on the Euclidean plane, and not on the surface of the Earth, then it could be solved exactly by computing the Voronoi diagram for the set of airport locations, and intersecting ...
Gareth Rees's user avatar
  • 49.6k
7 votes

VBA functions to calculate sun and moon positions

At a bare minimum declare your variable types when Dimming variables*. If you don't, they're automatically Variant. The VBA ...
FreeMan's user avatar
  • 1,290
6 votes
Accepted

Kattis "Hitting the Targets" utilizing a polymorphic relashionship

Virtual destructor As a simple rule of thumb, if a base class contains any virtual functions, its destructor should also be ...
Jerry Coffin's user avatar
  • 33.5k
6 votes
Accepted

Get all points within given radius for a given vector

Here are some things that may help you improve your code. Use the appropriate #includes In order to compile and link, this code requires at least following line: ...
Edward's user avatar
  • 66.4k
6 votes

Lines, intersections and terrible unit tests

This is more of a long comment. essentially it builds both lines in slope-intercept form (y = mx + b) This doesn't work for vertical lines, because a vertical ...
I'll add comments tomorrow's user avatar
6 votes
Accepted

Lines, intersections and terrible unit tests

I don't see the benefits of having both static and non-static API: PointF? Intersect and static PointF? Intersect. It does not ...
Nikita B's user avatar
  • 13k
6 votes

Updating positions of enemies every frame

Some things you could try: Unless the waypoints change position, the objects will continue in the same direction each frame. This means you don't have to recalculate ...
Trent Bartlem's user avatar
6 votes

Count lines with more the X dots on it

1. Dynamic problem The post says: I have a Python-Tkinter UI that has a field with dots. A user can choose some of them, and program must show points for currently chosen dots after every new ...
Gareth Rees's user avatar
  • 49.6k

Only top scored, non community-wiki answers of a minimum length are eligible