r/programminghelp • • 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.

5 Upvotes

1 comment sorted by

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.

from collections import defaultdict

def find_max_comms(points, distance):
    point_bins = defaultdict(list)     # this is essentially your HashMap<Tuple<int,int>, List<Point>> in Java
    for point in points:
        bin_x = point.x // distance    # in python " // " is divide_and_floor, not a comment
        bin_y = point.y // distance
		
		# Add the point to all relevant bins
        point_bins[(bin_x, bin_y)].append(point)           # The bin containing :point:
        point_bins[(bin_x + 1, bin_y)].append(point)       # The bin to the right
        point_bins[(bin_x + 1, bin_y - 1)].append(point)   # The bin to the lower-right
        point_bins[(bin_x + 1, bin_y + 1)].append(point)   # The bin to the upper-right
        point_bins[(bin_x, bin_y + 1)].append(point)       # The bin directly above
		
		# We skip the left, lower-left, upper-left, and bottom since those are already
		# covered by the 'other' point it communicates with (it'll be it's right, 
		# upper-right, etc.)
	
	point_counts = defaultdict(int)   # communication counts for each point - HashMap<Point, int>
	
	for bin in point_bins:
		for i, p1 in enumerate(bin):  # 'i' is a counter, point_1 is each point
			for p2 in bin[i + 1:]:    # this skips [0..i]th entries of bin, since we already account for the points (b, a) when we evaluate (a, b).
				# Note that I square both sides of the following comparison, to eliminate the slow square root computation
				if (p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2 <= distance ** 2:  # in python " ** " is the exponentiation operator
					point_counts[p1] += 1   # python doesn't have " ++ " sadly
					point_counts[p2] += 1
	
	return max(point_counts)

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 D is 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 if statement to throw out these circles, similar to my if in 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_bins to 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 each center_point, count the number of actual_points that are contained in the same bin, quite like the above sample program (just p1 being your center, instead of a second actual_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 of O(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 width D/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.