Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Collections;
- import java.util.Arrays;
- int width = 500;
- int height = 500;
- 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}};
- int offset=50;
- int pointsize=10;
- int mult=(width-2*offset)/32;//current run
- float max_distance;
- int[][] input1 ={{0,0
- },{
- 0,4
- },{
- 4,4
- },{
- 4,0
- },{
- 2,2}};
- int crossSign(int[] origin,int[]p1,int p2[]){
- float error=0.0000;
- float[] p11 ={p1[0]-origin[0],p1[1]-origin[1]};
- float[] p21 ={p2[0]-origin[0],p2[1]-origin[1]};
- float cross= p11[0]*p21[1]-p11[1]*p21[0];
- return cross<error?-1:1;
- }
- boolean noOverlap(ArrayList<int[][]> a,int[] p1,int[] p2){
- for(int[][] line:a)
- {
- if(line[0][0]==p1[0]&&line[0][1]==p1[1]||
- line[1][0]==p1[0]&&line[1][1]==p1[1]||
- line[0][0]==p2[0]&&line[0][1]==p2[1]||
- line[1][0]==p2[0]&&line[1][1]==p2[1]
- ) {
- continue;
- }
- if(crossSign(line[0],line[1],p1)!=crossSign(line[0],line[1],p2)&&
- crossSign(p1,p2,line[0])!=crossSign(p1,p2,line[1]))
- {
- return false;
- }
- }
- return true;
- }
- int[][][] solveNaive(int[][]input){
- ArrayList<int[][]> c = new ArrayList<int[][]>();
- for(int i =0;i<input.length;i++){
- for(int j=i+1;j<input.length;j++){
- if(noOverlap(c,input[i],input[j]))
- {
- int[][] ct={input[i],input[j]};
- c.add(ct);
- }}}
- int[][][] rv = new int[c.size()][][];
- int i=0;
- for(int[][] line:c){
- rv[i++]=line;
- }
- return rv;
- }
- int[][][] sollution;
- void setup() {
- size(500, 500);
- noStroke();
- List<int[]> l = Arrays.asList(input2);
- Collections.shuffle(l);
- input2=l.toArray(input2);
- sollution=solveNaive(input2);
- }
- void point(int x,int y){
- x=offset+x*mult;
- y=offset+y*mult;
- fill(color(255,0,0));
- ellipse(x,y,pointsize,pointsize);
- }
- void line2(int[] p1,int[]p2){
- stroke(color(0));
- int x1=offset+mult*p1[0];
- int y1=offset+mult*p1[1];
- int x2=offset+mult*p2[0];
- int y2=offset+mult*p2[1];
- line(x1,y1,x2,y2);
- noStroke();
- }
- void draw() {
- background(255,255,255);
- for(int[] a:input2){
- point(a[0],a[1]);
- }
- for(int[][] line:sollution){
- line2(line[0],line[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement