Joe Rice
YaBB Newbies
Offline
Posts: 28
Re: need help asap...project due tomorrow :(
Reply #1 - Nov 4th , 2009, 5:18pm
Code: else if(testCompletion() == true){ drawLines(); //displaying the orbs for(int i = 0; i < orbs.length; i++){ orbs[i].display(); } rectMode(CENTER); strokeWeight(5); stroke(0); fill(50); rect(width/2, height/2, 360,200); fill(255); textFont(font); textSize(40); textMode(SCREEN); text("You Win!", width/2-75, height/2); delay(2000); lvl++; } } //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 variable 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); } line(orbs[0].orbx, orbs[0].orby,orbs[orbs.length-1].orbx, orbs[orbs.length-1].orby); } boolean testCompletion(){ int temp = 0; boolean temp3 = false; for(int j=0; j< orbcount; j++) { int temp1 = j+1; if(temp1 == orbcount){ temp1 = 0; } line(p[j].x,p[j].y, p[temp1].x,p[temp1].y); for(int i=0; i< orbcount; i++) if(i!=j) { int temp2 = i+1; if(temp2 == orbcount){ temp2 = 0; } Point pt=findIntersection(p[i],p[temp2], p[j],p[temp1]); if(pt != null && (pt != p[i] || pt != p[j] || pt != p[temp1] || pt != p[temp2])){ temp++; println(temp); } } } if(temp == orbcount*2){ temp3 = true; } return temp3; } //this function borrowed from this example: // http://processing.org/discourse/yabb2/YaBB.pl?num=1252607024/1 Point findIntersection(Point p1,Point p2, Point p3,Point p4) { float xD1,yD1,xD2,yD2,xD3,yD3; float dot,deg,len1,len2; float segmentLen1,segmentLen2; float ua,ub,div; // calculate differences xD1=p2.x-p1.x; xD2=p4.x-p3.x; yD1=p2.y-p1.y; yD2=p4.y-p3.y; xD3=p1.x-p3.x; yD3=p1.y-p3.y; // calculate the lengths of the two lines len1=sqrt(xD1*xD1+yD1*yD1); len2=sqrt(xD2*xD2+yD2*yD2); // calculate angle between the two lines. dot=(xD1*xD2+yD1*yD2); // dot product deg=dot/(len1*len2); // if abs(angle)==1 then the lines are parallell, // so no intersection is possible if(abs(deg)==1) return null; // find intersection Pt between two lines Point pt=new Point(0,0); div=yD2*xD1-xD2*yD1; ua=(xD2*yD3-yD2*xD3)/div; ub=(xD1*yD3-yD1*xD3)/div; pt.x=p1.x+ua*xD1; pt.y=p1.y+ua*yD1; // calculate the combined length of the two segments // between Pt-p1 and Pt-p2 xD1=pt.x-p1.x; xD2=pt.x-p2.x; yD1=pt.y-p1.y; yD2=pt.y-p2.y; segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2); // calculate the combined length of the two segments // between Pt-p3 and Pt-p4 xD1=pt.x-p3.x; xD2=pt.x-p4.x; yD1=pt.y-p3.y; yD2=pt.y-p4.y; segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2); // if the lengths of both sets of segments are the same as // the lenghts of the two lines the point is actually // on the line segment. // if the point isnÔøΩt on the line, return null if(abs(len1-segmentLen1)>0.01 || abs(len2-segmentLen2)>0.01) return null; // return the valid intersection return pt; } class Point{ float x,y; Point(float x, float y){ this.x = x; this.y = y; } void set(float x, float y){ this.x = x; this.y = y; } }