Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class FastM {
  5. public static void main(String[] args) throws IOException{
  6. // input
  7. FileWriter fw = new FileWriter("visualPoints.txt", false);
  8. BufferedWriter write = new BufferedWriter(fw);
  9. int n = StdIn.readInt();
  10. Point[] points = new Point[n];
  11. for(int i = 0; i < n; i++) { //Add points
  12. points[i] = new Point(StdIn.readInt(), StdIn.readInt());
  13. }
  14.  
  15. // main list
  16. ArrayList<ArrayList<Point>> pl = new ArrayList<ArrayList<Point>>();
  17.  
  18. // check everything
  19. for(int a = 0; a < n; a++) {
  20. Point[] temp = Arrays.copyOf(points, points.length);
  21.  
  22. Arrays.sort(temp, temp[a].BY_SLOPE_ORDER);
  23. double curSlope = temp[0].getSlope(temp[1]);
  24.  
  25. ArrayList<Point> seg = new ArrayList<>();
  26. for(int b = 1; b < n; b++) {
  27. // first iteration only
  28. if(seg.isEmpty()) {
  29. seg.add(temp[b]);
  30. curSlope = temp[0].getSlope(temp[b]);
  31. }
  32. // matching slope
  33. else if(curSlope == temp[0].getSlope(temp[b])) {
  34. seg.add(temp[b]);
  35. if(b == n - 1 && seg.size() >= 3) {
  36. seg.add(temp[0]);
  37. pl.add(seg);
  38. seg = new ArrayList<>();
  39. }
  40. }
  41. // nonmatching slope
  42. else{
  43. if(seg.size() >= 3) {
  44. seg.add(temp[0]);
  45. pl.add(seg);
  46. seg = new ArrayList<>();
  47. }
  48. seg.clear();
  49. seg.add(temp[b]);
  50. curSlope = temp[0].getSlope(temp[b]);
  51. }
  52. }
  53. }
  54. // sort
  55. for(ArrayList<Point> col : pl) Collections.sort(col);
  56. // remove dupes
  57. Set<ArrayList<Point>> ff = new HashSet<>();
  58. ff.addAll(pl);
  59. pl.clear();
  60. pl.addAll(ff);
  61. // print
  62. for (ArrayList<Point> elem : pl) {
  63. StdOut.print(elem.size() + ":");
  64. for (int i = 0; i < elem.size() - 1; i++) {
  65. StdOut.print(elem.get(i));
  66. StdOut.print(" -> ");
  67. }
  68. StdOut.println(elem.get(elem.size() - 1));
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement