Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static final int SZ=600;
- static final int PAD=200;
- int rad=SZ/20;
- ArrayList<Rect> rect=new ArrayList<Rect>();
- ArrayList<Seg> seg=new ArrayList<Seg>();
- Point st;
- boolean in(Point x){
- for(Rect y:rect){
- if(x.x>=y.l && x.x<=y.r && x.y>=y.u && x.y<=y.d){
- return true;
- }
- }
- return false;
- }
- void setup(){
- size(600,600);
- background(255);
- int sepline=(int)random(SZ);
- while(sepline<PAD || sepline>SZ-PAD){
- sepline=(int)random(SZ);
- }
- int u=(int)random(SZ), d=(int)random(SZ);
- int u2=(int)random(SZ), d2=(int)random(SZ);
- rect.add(new Rect((int)random(sepline),(int)random(sepline),u,d));
- rect.add(new Rect((int)random(SZ-sepline)+sepline,(int)random(SZ-sepline)+sepline,u2,d2));
- while(true){//not perfect
- st=new Point((int)random(SZ), (int)random(SZ));
- if(!in(st))break;
- }
- for(Rect x:rect){
- seg.add(new Seg(new Point(x.l,x.u),new Point(x.r,x.u)));
- seg.add(new Seg(new Point(x.l,x.d),new Point(x.r,x.d)));
- seg.add(new Seg(new Point(x.l,x.u),new Point(x.l,x.d)));
- seg.add(new Seg(new Point(x.r,x.u),new Point(x.r,x.d)));
- }
- }
- float cross(PVector a, PVector b){
- return a.x*b.y-a.y*b.x;
- }
- boolean intersect(Point a, Point b, Point c, Point d){
- PVector B=new PVector(b.x-a.x,b.y-a.y);
- PVector C=new PVector(c.x-a.x,c.y-a.y);
- PVector D=new PVector(d.x-a.x,d.y-a.y);
- float c1=cross(B,C);
- float c2=cross(B,D);
- int cc1=(int)(c1/abs(c1));
- int cc2=(int)(c2/abs(c2));
- println(cc1+" "+cc2+" "+c1+" "+c2);
- if(cc1+cc2!=0){
- return false;
- }
- println("HI AF");
- PVector A=new PVector(a.x-c.x,a.y-c.y);
- B=new PVector(b.x-c.x,b.y-c.y);
- D=new PVector(d.x-c.x,d.y-c.y);
- c1=cross(D,A);
- c2=cross(D,B);
- cc1=(int)(c1/abs(c1));
- cc2=(int)(c2/abs(c2));
- if(cc1+cc2!=0){
- return false;
- }
- return true;
- }
- void draw(){
- clear();
- background(255);
- circle(st.x,st.y,rad);
- for(Rect x:rect){
- rect(x.l,x.u,x.r-x.l,x.d-x.u);
- }
- //line(st.x,st.y,mouseX,mouseY);
- PVector dir=new PVector(mouseX-st.x,mouseY-st.y);
- PVector mag=new PVector(mouseX-st.x,mouseY-st.y);
- println(dir.heading());
- dir.normalize();
- mag.normalize();
- mag.mult(2*SZ);
- for(Seg x:seg){
- println(x.x.x+" "+x.x.y);
- if(intersect(st,new Point(st.x+mag.x,st.y+mag.y),x.x,x.y)){
- println("HI INTER"+frameCount);
- float L=0,R=mag.mag();
- for(int i=0;i<30;i++){
- float M=(L+R)/2;
- PVector test=PVector.mult(dir,M);
- if(intersect(st,new Point(st.x+test.x,st.y+test.y),x.x,x.y)){
- R=M;
- } else L=M;
- }
- mag=PVector.mult(dir,L);
- }
- }
- line(st.x,st.y,st.x+mag.x,st.y+mag.y);
- }
- final static int move=10;
- void keyPressed(){//strafe and vel
- if(key==CODED){
- if(keyCode==LEFT){
- Point sy=new Point(st.x-move,st.y);
- if(!in(sy))st=sy;
- }
- if(keyCode==UP){
- Point sy=new Point(st.x,st.y-move);
- if(!in(sy))st=sy;
- }
- if(keyCode==RIGHT){
- Point sy=new Point(st.x+move,st.y);
- if(!in(sy))st=sy;
- }
- if(keyCode==DOWN){
- Point sy=new Point(st.x,st.y+move);
- if(!in(sy))st=sy;
- }
- }
- }
- class Point{
- float x,y;
- Point(float a, float b){
- x=a;y=b;
- }
- }
- class Seg{
- Point x, y;
- Seg(Point a, Point b){
- x=a;y=b;
- }
- }
- class Rect{
- int l,r,u,d;
- Rect(int a, int b, int c, int e){
- if(c<=e){
- u=c;
- d=e;
- } else {
- u=e;
- d=c;
- }
- if(a<=b){
- l=a;
- r=b;
- } else {
- l=b;
- r=a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement