r/programminghelp • u/jjaraa • Nov 03 '18
Help with points in a plane
Hello, i'm trying to code a method to calculate the maximun number of points in a plane that can "communicate" at a maximum distance D using JAVA.
Each point will be an object, and they will have two int representing the x and y coordinates in the plane.
I discovered that if you choose any point in the plane, you can determine a circle of radius D and center in the chosen point, where all the points within the circle can communicate with the chosen point.
After that, alliterations can be used to determine all the communications of each point in the plane at a distance D and find what is the highest number of communications between all the points.
The problem is that doing this last calculation represents a great calculation time for the program.
My question is; Is there any other way to do it?
Or if you can give me any suggestions on how I intend to code it.
1
u/07734willy Nov 07 '18
Try to bin the points into "boxes" of width D, so you only have to compute the distance between points in the same bins and in adjacent bins, since any points in bins farther away are guaranteed to be farther than D units apart from each other. My Java is fairly rusty, and I don't have an IDE available to code in right now, so I'm just going to write up some python / psuedocode to convey my though process.
Now, I would argue this is not correct. From what I've understood of your algorithm, you're assuming the center of your optimal "connectivity circle" (I don't know what to call it) can be one of your points, but this may not be the case. Consider the scenario below, where each star is a point:
and assume your distance
Dis just barely big enough to encapsulate all the points if placed in the dead center. If you recenter this circle to be on top of one of your points, you'll miss some points at the corners. I argue that instead of placing your circle at every point, you need to place your circle on a new set of points-Take any two points at random. Now place your fix-size (
D) circle such that those points lie on its edge. There's two such ways you can do so (mirrored across the line that passes through said points), so you'll have to consider both scenarios. Do this for all possible unique pairs of points, and record the centers. This is your new set of points to measure connectivity / communication within. The reason this works is as follows-Consider the optimal set of points (the highest connectivity / number of comms). There are three scenarios
Two or more points lie on its edge. In this case, we would find this circle using our method above directly, and would arrive at the solution.
One point lies on the edge. In this case, we "rotate" the circle about that point, until another point intersects the edge, in which case we default to the above case. Since we stop rotating our connectivity circle when a point reaches the edge, no points leave the connectivity circle, so the number of connected / communicating points is equivalent to the original circle.
No points lie on the edge. In this case, we translate the circle in any direction until we point intersects the edge, in which case we default to the above case. Since we stop translating the circle when a point reaches an edge, as before, no points leave the connectivity circle, so it is equivalent to the original.
Now with that in mind, your next task is to find the centers of these circles. This shouldn't be too hard- I'd go through the trig / vector math here, but I'm almost certainly make a mistake. I'm sure a formula already exists somewhere on the internet anyways, so you'd probably be best just to google it and use that. Do note however, that some points may be too far apart to connect with a circle, and so you'll want to include an
ifstatement to throw out these circles, similar to myifin my sample program above.Alright, so now you have your new list of centers. Now what? Modify my program above to take two lists of points, and the
point_binsto contain two lists of points- centers, and actual (physical) points. You don't have to actually implement it like that, but just implement equivalent functionality. Then when you iterate over the bins, for eachcenter_point, count the number ofactual_points that are contained in the same bin, quite like the above sample program (justp1being your center, instead of a secondactual_point). Tally up your connected points for each center (you really don't even need a HashMap / defaultdict either, you could use a single variable to keep a running count of the max, I just used a defaultdict again in case you need to report which points produced the max connectivity).Of course there might be a few bugs or oversights, but overall I think this algorithm will work fairly well. It will be asympotpically slower (
O(n^3)instead ofO(n^2), however the binning should dramatically reduce the number of comparisons necessary, enough in practice to make it faster than your quadratic time solution, unless the points are extremely dense. If that is the case, you might be able to modify the algorithm to use an alternative binning technique, with smaller bins of widthD/2, so that every point inside the bin is guaranteed to intersect with every other point in the bin, and then save yourself a lot of time / computations that way.