0

I'm trying to modify the code, but got the error listed in the title.Anyone please help.

Error: The method segments() in the type BruteCollinearPoints is not applicable for the arguments (Point, Point)

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import edu.princeton.cs.algs4.*;

public class BruteCollinearPoints 
{

   public BruteCollinearPoints(Point[] points)    // finds all line segments containing 4 points
   {
       int N = points.length;
       int i = 0;

       for (int p = 0; p < N; p++) {
            for (int q = p + 1; q < N; q++) {
                double slopeToQ = points[p].slopeTo(points[q]);
                for (int r = q + 1; r < N; r++) {
                    double slopeToR = points[p].slopeTo(points[r]);
                    if (slopeToQ == slopeToR) {
                        for (int s = r + 1; s < N; s++) {
                            double slopeToS = points[p].slopeTo(points[s]);
                            if (slopeToQ == slopeToS) {
                                // Create the list of collinear points and sort them.
                                List<Point> collinearPoints = new ArrayList<Point>(4);
                                collinearPoints.add(points[p]);
                                collinearPoints.add(points[q]);
                                collinearPoints.add(points[r]);
                                collinearPoints.add(points[s]);
                                Collections.sort(collinearPoints);
                                segments(Collections.min(collinearPoints), Collections.max(collinearPoints)); //this is where the error is

                            }
                        }
                    }
                }
            }
       }
   }
   //public           int numberOfSegments();        // the number of line segments
   public LineSegment[] segments()                // the line segments
   {  
        return segments();
   }
}
Bruce
  • 1

2 Answers2

0

The segments method is defined like this:

public LineSegment[] segments()                // the line segments
{  
    return segments();
}

This means that the method does not take any arguments, yet you are calling it with two arguments:

segments(Collections.min(collinearPoints), Collections.max(collinearPoints));

To fix this you need to call segments method without arguments like this:

segments();

or redefine the method to work with arguments. For example:

public LineSegment[] segments(Point a, Point b)
{  
    // calculate line segments from a to b and return them
}

On a side note, the segments method, as it is defined now, will most certainly not work because it is calling itself recursively without termination condition.

stjepano
  • 1,052
  • 7
  • 15
0

From your import statements, it seems you're working on a programming assignment from the Algorithms, 4th Edition course called Pattern Recognition. In that course, the method segments() returns an array of type LineSegment[] from another class. You can see it used in the "sample client program" that is provided. Here is a part of that:

for (LineSegment segment : collinear.segments()) {
    StdOut.println(segment);
    segment.draw();
}
StdDraw.show();

In order to actually create a new LineSegment, use something like this:

LineSegment seg = new LineSegment(point1, point2)

For reference, here is the creator that is called in LineSegment.java:

public class LineSegment {
private final Point p;   // one endpoint of this line segment
private final Point q;   // the other endpoint of this line segment

/**
 * Initializes a new line segment.
 *
 * @param  p one endpoint
 * @param  q the other endpoint
 * @throws NullPointerException if either p or q is null
 */
    public LineSegment(Point p, Point q) {
        if (p == null || q == null) {
            throw new NullPointerException("argument is null");
        }
        this.p = p;
        this.q = q;
    }
.
.
.
}
TT--
  • 2,956
  • 1
  • 27
  • 46