Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int w = 800;
- int h = 600;
- int z = 600;
- color white = color(255, 255, 255);
- color black = color(0, 0, 0);
- color red = color(255, 0, 0);
- color green = color(0, 255, 0);
- color blue = color(0, 0, 255);
- color yellow = color(255,255,0);
- ArrayList<ArrayList<PVector>> container;
- void setup() {
- size(w, h, P2D);
- background(0);
- smooth();
- frameRate(30);
- resetTriangles();
- }
- void resetTriangles() {
- container = new ArrayList<ArrayList<PVector>>();
- container.add(randomTriangle());
- container.add(randomTriangle());
- print("------------------------\n");
- testIntersect(container.get(0), container.get(1));
- print("-------------------------\n");
- }
- void draw() {
- background(0);
- noFill();
- stroke(green);
- triangle(int(container.get(0).get(0).get().x),int(container.get(0).get(0).get().y),
- int(container.get(0).get(1).get().x),int(container.get(0).get(1).get().y),
- int(container.get(0).get(2).get().x),int(container.get(0).get(2).get().y));
- stroke(color(255,0,255));
- triangle(int(container.get(1).get(0).get().x),int(container.get(1).get(0).get().y),
- int(container.get(1).get(1).get().x),int(container.get(1).get(1).get().y),
- int(container.get(1).get(2).get().x),int(container.get(1).get(2).get().y));
- }
- void mousePressed() {
- resetTriangles();
- }
- ArrayList randomTriangle() {
- ArrayList<PVector> rt = new ArrayList<PVector>();
- for (int i = 0; i < 3; i++) {
- rt.add(new PVector(int(random(w)), int(random(h))));
- }
- return rt;
- }
- boolean testIntersect(ArrayList<PVector> a, ArrayList<PVector> b) {
- PVector[] n = new PVector[6];
- n[0] = PVector.sub(a.get(1), a.get(0));
- n[1] = PVector.sub(a.get(2), a.get(1));
- n[2] = PVector.sub(a.get(0), a.get(2));
- n[3] = PVector.sub(b.get(1), b.get(0));
- n[4] = PVector.sub(b.get(2), b.get(1));
- n[5] = PVector.sub(b.get(0), b.get(2));
- float[][] a_dots = new float[6][3];
- float[][] b_dots = new float[6][3];
- for (int i = 0; i < 6; i++) {
- for (int j = 0; j < 3; j++) {
- a_dots[i][j] = n[i].dot(a.get(j));
- b_dots[i][j] = n[i].dot(b.get(j));
- }
- }
- for (int i = 0; i < 6; i++) {
- float as = a_dots[i][0];
- float al = a_dots[i][0];
- float bs = b_dots[i][0];
- float bl = b_dots[i][0];
- for (int j = 1; j < 3; j++) {
- if (a_dots[i][j] < as) { as = a_dots[i][j]; }
- if (a_dots[i][j] > al) { al = a_dots[i][j]; }
- if (b_dots[i][j] < bs) { bs = b_dots[i][j]; }
- if (b_dots[i][j] > bl) { bl = b_dots[i][j]; }
- }
- print ("as: "+as+" al: "+al+" bs: "+bs+" bl: "+bl+" -- ");
- if ((as < bs && al > bs) || (bs < as && bl > as)) {
- print(1);
- } else {
- print(0);
- }
- print ("\n");
- /*
- AS BS AL BL
- BS AS AL BL
- */
- }
- print("\n");
- return true;
- }
Add Comment
Please, Sign In to add comment