We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › dynamically checking to see if two lines intersect
Page Index Toggle Pages: 1
dynamically checking to see if two lines intersect (Read 899 times)
dynamically checking to see if two lines intersect
Nov 2nd, 2009, 11:44am
 
i'm looking to see if two lines intersect, each end point on each line is able to be moved with the mouse. i know that i need to use the equation of two lines using the slope intercept form but am having trouble getting the code down. the point is to make it so no lines are intersecting. any help or direction to how i should go about writing the code would be amazing. below is the code i have so far

Code:

int collisionDist = 50; //distance that orbs collide at
int listener; // tells which orb is selected
int orbsize = 50; //width and height of each orb
int orbcount = 10; //number of orbs on screen
Orb[] orbs = new Orb[orbcount]; //orbs array
float[] orbsx = new float[orbcount];
float[] orbsy = new float[orbcount];
boolean once = true;

int[] xtrail = new int[orbsize]; //array for x values of the trail effect
int[] ytrail = new int[orbsize]; //array for y values of the trail effect

void setup(){
 size(960,600);
 smooth();
 frameRate(1000);
 //here i'm assigning orbsx and orbsy values for the initial start values for each orb
 for(int i = 0; i < orbs.length; i++){
   orbsx[i] = round(random(25,935));
   orbsy[i] = round(random(25,575));
   orbs[i] = new Orb(orbsx[i],orbsy[i],orbsize);
 }

 //here i'm initializing the x and y trail array
 for (int i = 0; i < xtrail.length; i ++ ) {
   xtrail[i] = 0;
   ytrail[i] = 0;
 }



}
void draw(){
 background(0);
 
 drawLines();
 
 //displaying the orbs
 for(int i = 0; i < orbs.length; i++){
   orbs[i].display();
 }
 
 
 
 //spreading out the orbs so they don't overlap
 for(int i=0; i<orbs.length; i++){
   fixOverlap(i);
 }

 for(int i = 0; i < orbs.length; i++){

   if(dist(mouseX, mouseY, orbs[i].orbx, orbs[i].orby) < 25 && mousePressed){
     findListener();
     orbs[i].orbx = mouseX;
     orbs[i].orby = mouseY;
     detectCollision(i);
     for(int x=0; x<orbs.length; x++){
       detectCollisionByOthers(x);
     }
     stroke(255);
     fill(0);
     for (int x = 45; x < xtrail.length-1; x ++ ) {
       xtrail[x] = xtrail[x+1];
       ytrail[x] = ytrail[x+1];
     }
     xtrail[xtrail.length-1] = mouseX;
     ytrail[ytrail.length-1] = mouseY;

     for (int t = 45 ; t < xtrail.length; t ++ ) {
       ellipse(xtrail[t],ytrail[t],t,t);
     }
     orbs[i].activated = true;
     orbs[i].listening = true;
   }
   else if(!mousePressed){
     orbs[i].activated = false;
   }
 }
}

//fixOverlap is a function to spread out the orbs so they dont overap eachother
void fixOverlap(int temp){
 for(int i=0; i<orbs.length; i++){
   if (i != temp){
     float tdist = dist(orbs[temp].orbx, orbs[temp].orby, orbs[i].orbx, orbs[i].orby);
     if(tdist < collisionDist){
       float originx = orbs[temp].orbx - orbs[i].orbx;
       float originy = orbs[temp].orby - orbs[i].orby;
       float theta = atan2(originy, originx);
       float newx = collisionDist * -cos(theta) + orbs[temp].orbx;
       float newy = collisionDist * -sin(theta) + orbs[temp].orby;
       orbs[i].orbx = newx;
       orbs[i].orby = newy;
       //orbs[i].activated = true;
     }
   }
 }
}

//this function detects collisions between the selected orb and any other orb
void detectCollision(int temp){
 for(int i=0; i<orbs.length; i++){
   if (i != listener){
     float tdist = dist(orbs[listener].orbx, orbs[listener].orby, orbs[i].orbx, orbs[i].orby);
     if(tdist < collisionDist-1){
       float originx = orbs[listener].orbx - orbs[i].orbx;
       float originy = orbs[listener].orby - orbs[i].orby;
       float theta = atan2(originy, originx);
       float newx = collisionDist * -cos(theta) + orbs[listener].orbx;
       //newx = constrain(newx, 0+orbsize/2, width-orbsize/2);
       float newy = collisionDist * -sin(theta) + orbs[listener].orby;
       //newy = constrain(newy, 0+orbsize/2, height-orbsize/2);
       orbs[i].orbx = newx;
       orbs[i].orby = newy;
       orbs[i].activated = true;
       }
     }
   }
 }

//this function detects collisions between any of the orbs other than the selected orb
void detectCollisionByOthers(int temp){
 for(int i=0; i<orbs.length; i++){
   if (i != temp){
     float tdist = dist(orbs[temp].orbx, orbs[temp].orby, orbs[i].orbx, orbs[i].orby);
     if(tdist < collisionDist-1){
       float originx = orbs[temp].orbx - orbs[i].orbx;
       float originy = orbs[temp].orby - orbs[i].orby;
       float theta = atan2(originy, originx);
       float newx = collisionDist * -cos(theta) + orbs[temp].orbx;
       //newx = constrain(newx, 0+orbsize/2, width-orbsize/2);
       float newy = collisionDist * -sin(theta) + orbs[temp].orby;
       //newy = constrain(newy, 0+orbsize/2, height-orbsize/2);
       orbs[i].orbx = newx;
       orbs[i].orby = newy;
       orbs[i].activated = true;
     }
   }
 }
}

//reloads once var whenthe mouse is released
void mouseReleased(){
 once = true;
}

// finds which orb is selected
void findListener(){
 for(int i=0; i<orbs.length; i++){
   if(orbs[i].listening){
     listener = i;
   }
 }
}

void drawLines(){
 strokeWeight(10);
 stroke(100,0,200);
 for(int i = 0; i < orbs.length-1; i++){
   line(orbs[i].orbx, orbs[i].orby,orbs[i+1].orbx, orbs[i+1].orby);
 }
}

float findSlope(float px1, float py1, float px2, float py2){
 float slope = (py2-py1)/(px2-px1);
 return slope;
}

void lineEquation(float slope, float px, float py){
}
 

void checkLineItersection(){
}

class Orb{
 boolean listening;
 float orbx;
 float orby;
 float orbdia;
 boolean activated;
 float[] xvals = new float[360];
 float[] yvals = new float[360];
 
 Orb(float x, float y, float dia){
   orbx = x;
   orby = y;
   orbdia = dia;
   activated = false;
 }
 void display(){
   if(activated == false){
     strokeWeight(1);
     stroke(100);
   }
   else if(activated == true){
     strokeWeight(2);
     stroke(255);
   }
   
   fill(0);
   ellipseMode(CENTER);
   ellipse(orbx, orby, 50,50);
   
 }
}
Re: dynamically checking to see if two lines intersect
Reply #1 - Nov 2nd, 2009, 11:51am
 
Sorry, i didnt read your code. But maybe this posting and example of unlekker intersection library is what you are looking for.

http://processing.org/discourse/yabb2/num_1252607024.html

Page Index Toggle Pages: 1