We are supposed to find with every point (distance between two points must be <= 2) forming a circle, what is the maximum number of points that can be contained by a circle formed by these points. These points are stored in list. It just means among the list of points, we are supposed to pair two points together and see whether a circle forms by this 2 points can contain the most number of points. We are finding the most number of points.
Here is the code.
class Point {
private final double x;
private final double y;
Point (double x, double y) {
this.x = x;
this.y = y;
}
Point midPoint(Point q) {
Point midPoint = new Point ((this.x + q.x)/2, (this.y + q.y)/2);
return midPoint;
}
double distanceTo (Point q) {
return Math.sqrt(Math.pow((this.x - q.x), 2) + Math.pow((this.y - q.y),2));
}
double angleTo(Point q) {
Point middlePoint = midPoint(q);
return Math.atan2(middlePoint.x, middlePoint.y);
}
Point moveTo(double theta, double d) {
Point newPoint = new Point ((this.x + d * Math.cos(theta)), this.y + d * Math.sin(theta));
return newPoint;
}
@Override
public String toString () {
return "point (" + String.format("%.3f",this.x) + ", " + String.format("%.3f", this.y) + ")";
}
}
import java.util.List;
class Circle {
private final Point centre;
private final double radius;
Circle (Point centre, double radius) {
this.centre = centre;
this.radius = radius;
}
public int containsPoints (List<Point> points) {
int numOfPoints = 0;
for (int i = 0; i< points.size(); i++) {
if (centre.distanceTo(points.get(i)) <= 2) {
numOfPoints++;
}
}
return numOfPoints;
}
@Override
public String toString() {
return "circle of radius " + this.radius + " centered at " + this.centre;
}
}
import java.util.List;
Circle createUnitCircle(Point p, Point q) {
double anglePQ = p.angleTo(q);
Point midPoint = p.midPoint (q);
double pqDistance = p.distanceTo(q);
double mqDistance = midPoint.distanceTo(q);
double dSquare = 1 - Math.pow(mqDistance, 2);
double d = Math.sqrt (dSquare);
Point centre = midPoint.moveTo(anglePQ, d);
return new Circle (centre, 1);
}
int findMaxDiscCoverage (List<Point> points) {
int maxDiscCoverage = 0;
int numOfPoints = points.size();
int numOfPointsContained = 0;
for (int i = 0; i < numOfPoints - 1; i++) {
for (int j = i + 1; j < numOfPoints; j++) {
Point point1 = points.get(i);
Point point2 = points.get(j);
if (point1.distanceTo(point2) > 2) {
continue;
}
Circle c1 = createUnitCircle(point1, point2);
numOfPointsContained = c1.containsPoints(points);
if (numOfPointsContained > maxDiscCoverage) {
maxDiscCoverage = numOfPointsContained;
}
return maxDiscCoverage;
}
I keep getting error like this
| Error:
| cannot find symbol
| symbol: method findMaxDiscCoverage(java.util.List<Point>)
| findMaxDiscCoverage(points)
| ^-----------------^
Please help me! Thank you!