Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class FastM {
- public static void main(String[] args) throws IOException{
- // input
- FileWriter fw = new FileWriter("visualPoints.txt", false);
- BufferedWriter write = new BufferedWriter(fw);
- int n = StdIn.readInt();
- Point[] points = new Point[n];
- for(int i = 0; i < n; i++) { //Add points
- points[i] = new Point(StdIn.readInt(), StdIn.readInt());
- }
- // main list
- ArrayList<ArrayList<Point>> pl = new ArrayList<ArrayList<Point>>();
- // check everything
- for(int a = 0; a < n; a++) {
- Point[] temp = Arrays.copyOf(points, points.length);
- Arrays.sort(temp, temp[a].BY_SLOPE_ORDER);
- double curSlope = temp[0].getSlope(temp[1]);
- ArrayList<Point> seg = new ArrayList<>();
- for(int b = 1; b < n; b++) {
- // first iteration only
- if(seg.isEmpty()) {
- seg.add(temp[b]);
- curSlope = temp[0].getSlope(temp[b]);
- }
- // matching slope
- else if(curSlope == temp[0].getSlope(temp[b])) {
- seg.add(temp[b]);
- if(b == n - 1 && seg.size() >= 3) {
- seg.add(temp[0]);
- pl.add(seg);
- seg = new ArrayList<>();
- }
- }
- // nonmatching slope
- else{
- if(seg.size() >= 3) {
- seg.add(temp[0]);
- pl.add(seg);
- seg = new ArrayList<>();
- }
- seg.clear();
- seg.add(temp[b]);
- curSlope = temp[0].getSlope(temp[b]);
- }
- }
- }
- // sort
- for(ArrayList<Point> col : pl) Collections.sort(col);
- // remove dupes
- Set<ArrayList<Point>> ff = new HashSet<>();
- ff.addAll(pl);
- pl.clear();
- pl.addAll(ff);
- // print
- for (ArrayList<Point> elem : pl) {
- StdOut.print(elem.size() + ":");
- for (int i = 0; i < elem.size() - 1; i++) {
- StdOut.print(elem.get(i));
- StdOut.print(" -> ");
- }
- StdOut.println(elem.get(elem.size() - 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement