Guest User

Untitled

a guest
Jan 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. int w = 800;
  2. int h = 600;
  3. int z = 600;
  4. color white = color(255, 255, 255);
  5. color black = color(0, 0, 0);
  6. color red = color(255, 0, 0);
  7. color green = color(0, 255, 0);
  8. color blue = color(0, 0, 255);
  9. color yellow = color(255,255,0);
  10.  
  11. ArrayList<ArrayList<PVector>> container;
  12.  
  13. void setup() {
  14.   size(w, h, P2D);
  15.   background(0);
  16.   smooth();
  17.   frameRate(30);
  18.   resetTriangles();
  19.  
  20. }
  21.  
  22. void resetTriangles() {
  23.   container = new ArrayList<ArrayList<PVector>>();
  24.   container.add(randomTriangle());
  25.   container.add(randomTriangle());
  26.   print("------------------------\n");
  27.   testIntersect(container.get(0), container.get(1));
  28.   print("-------------------------\n");
  29.  
  30. }
  31.  
  32. void draw() {
  33.   background(0);
  34.   noFill();
  35.  
  36.   stroke(green);
  37.   triangle(int(container.get(0).get(0).get().x),int(container.get(0).get(0).get().y),
  38.            int(container.get(0).get(1).get().x),int(container.get(0).get(1).get().y),
  39.            int(container.get(0).get(2).get().x),int(container.get(0).get(2).get().y));
  40.  
  41.   stroke(color(255,0,255));
  42.   triangle(int(container.get(1).get(0).get().x),int(container.get(1).get(0).get().y),
  43.            int(container.get(1).get(1).get().x),int(container.get(1).get(1).get().y),
  44.            int(container.get(1).get(2).get().x),int(container.get(1).get(2).get().y));
  45.  
  46. }
  47.  
  48.  
  49.  
  50. void mousePressed() {
  51.   resetTriangles();
  52. }
  53.  
  54. ArrayList randomTriangle() {
  55.   ArrayList<PVector> rt = new ArrayList<PVector>();
  56.   for (int i = 0; i < 3; i++) {
  57.     rt.add(new PVector(int(random(w)), int(random(h))));
  58.   }
  59.   return rt;
  60. }
  61.  
  62. boolean testIntersect(ArrayList<PVector> a, ArrayList<PVector> b) {
  63.   PVector[] n = new PVector[6];
  64.   n[0] = PVector.sub(a.get(1), a.get(0));
  65.   n[1] = PVector.sub(a.get(2), a.get(1));
  66.   n[2] = PVector.sub(a.get(0), a.get(2));
  67.  
  68.   n[3] = PVector.sub(b.get(1), b.get(0));
  69.   n[4] = PVector.sub(b.get(2), b.get(1));
  70.   n[5] = PVector.sub(b.get(0), b.get(2));
  71.  
  72.   float[][] a_dots = new float[6][3];
  73.   float[][] b_dots = new float[6][3];
  74.  
  75.   for (int i = 0; i < 6; i++) {
  76.     for (int j = 0; j < 3; j++) {
  77.       a_dots[i][j] = n[i].dot(a.get(j));
  78.       b_dots[i][j] = n[i].dot(b.get(j));
  79.     }
  80.   }
  81.  
  82.   for (int i = 0; i < 6; i++) {
  83.     float as = a_dots[i][0];
  84.     float al = a_dots[i][0];
  85.     float bs = b_dots[i][0];
  86.     float bl = b_dots[i][0];
  87.    
  88.     for (int j = 1; j < 3; j++) {
  89.       if (a_dots[i][j] < as) { as = a_dots[i][j]; }
  90.       if (a_dots[i][j] > al) { al = a_dots[i][j]; }
  91.       if (b_dots[i][j] < bs) { bs = b_dots[i][j]; }
  92.       if (b_dots[i][j] > bl) { bl = b_dots[i][j]; }
  93.     }
  94.     print ("as: "+as+" al: "+al+" bs: "+bs+" bl: "+bl+" -- ");
  95.     if ((as < bs && al > bs) || (bs < as && bl > as)) {
  96.       print(1);
  97.     } else {
  98.       print(0);
  99.     }
  100.     print ("\n");
  101.   /*
  102.     AS BS AL BL
  103.     BS AS AL BL
  104.   */
  105.   }
  106.   print("\n");
  107.   return true;
  108. }
Add Comment
Please, Sign In to add comment