Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2017
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Collections;
  4. import java.util.Arrays;
  5. int width = 500;
  6. int height = 500;
  7. int[][] input2={{0,0},{0,32},{32,0},{32,32},{13,13},{13,19},{19,19},{19,13},{16,5},{16,27},{5,16},{27,16}};
  8. int offset=50;
  9. int pointsize=10;
  10. int mult=(width-2*offset)/32;//current run
  11. float max_distance;
  12. int[][] input1 ={{0,0
  13. },{
  14. 0,4
  15. },{
  16. 4,4
  17. },{
  18. 4,0
  19. },{
  20. 2,2}};
  21. int crossSign(int[] origin,int[]p1,int p2[]){
  22.   float error=0.0000;
  23.   float[] p11 ={p1[0]-origin[0],p1[1]-origin[1]};
  24.   float[] p21 ={p2[0]-origin[0],p2[1]-origin[1]};
  25.    float cross= p11[0]*p21[1]-p11[1]*p21[0];
  26.    return cross<error?-1:1;
  27. }
  28. boolean noOverlap(ArrayList<int[][]> a,int[] p1,int[] p2){
  29.   for(int[][] line:a)
  30.   {
  31.       if(line[0][0]==p1[0]&&line[0][1]==p1[1]||
  32.           line[1][0]==p1[0]&&line[1][1]==p1[1]||
  33.           line[0][0]==p2[0]&&line[0][1]==p2[1]||
  34.           line[1][0]==p2[0]&&line[1][1]==p2[1]
  35.           ) {
  36.       continue;
  37.       }
  38.     if(crossSign(line[0],line[1],p1)!=crossSign(line[0],line[1],p2)&&
  39.     crossSign(p1,p2,line[0])!=crossSign(p1,p2,line[1]))
  40.       {
  41.         return false;
  42.       }
  43.   }
  44.   return true;
  45. }
  46. int[][][] solveNaive(int[][]input){
  47.   ArrayList<int[][]> c = new ArrayList<int[][]>();
  48.   for(int i =0;i<input.length;i++){
  49.     for(int j=i+1;j<input.length;j++){
  50.       if(noOverlap(c,input[i],input[j]))
  51.          {
  52.            int[][] ct={input[i],input[j]};
  53.           c.add(ct);
  54.          }}}
  55.    int[][][] rv = new int[c.size()][][];
  56.    int i=0;
  57.    for(int[][] line:c){
  58.      rv[i++]=line;
  59.    }
  60.    return rv;
  61. }
  62. int[][][] sollution;
  63. void setup() {
  64.   size(500, 500);
  65.   noStroke();
  66.   List<int[]> l = Arrays.asList(input2);
  67.       Collections.shuffle(l);
  68.       input2=l.toArray(input2);
  69.   sollution=solveNaive(input2);
  70. }
  71. void point(int x,int y){
  72.   x=offset+x*mult;
  73.   y=offset+y*mult;
  74.   fill(color(255,0,0));
  75.   ellipse(x,y,pointsize,pointsize);
  76.  
  77. }
  78. void line2(int[] p1,int[]p2){
  79.   stroke(color(0));
  80.   int x1=offset+mult*p1[0];
  81.   int y1=offset+mult*p1[1];
  82.   int x2=offset+mult*p2[0];
  83.   int y2=offset+mult*p2[1];
  84.   line(x1,y1,x2,y2);
  85.   noStroke();
  86. }
  87. void draw() {
  88.   background(255,255,255);
  89.    for(int[] a:input2){
  90.      point(a[0],a[1]);
  91.    }
  92.   for(int[][] line:sollution){
  93.   line2(line[0],line[1]);
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement