Advertisement
ec1117

Untitled

Apr 7th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1.  
  2. static final int SZ=600;
  3. static final int PAD=200;
  4. int rad=SZ/20;
  5. ArrayList<Rect> rect=new ArrayList<Rect>();
  6. ArrayList<Seg> seg=new ArrayList<Seg>();
  7. Point st;
  8.  
  9. boolean in(Point x){
  10. for(Rect y:rect){
  11. if(x.x>=y.l && x.x<=y.r && x.y>=y.u && x.y<=y.d){
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17.  
  18. void setup(){
  19. size(600,600);
  20. background(255);
  21. int sepline=(int)random(SZ);
  22. while(sepline<PAD || sepline>SZ-PAD){
  23. sepline=(int)random(SZ);
  24. }
  25. int u=(int)random(SZ), d=(int)random(SZ);
  26. int u2=(int)random(SZ), d2=(int)random(SZ);
  27. rect.add(new Rect((int)random(sepline),(int)random(sepline),u,d));
  28. rect.add(new Rect((int)random(SZ-sepline)+sepline,(int)random(SZ-sepline)+sepline,u2,d2));
  29. while(true){//not perfect
  30. st=new Point((int)random(SZ), (int)random(SZ));
  31. if(!in(st))break;
  32. }
  33. for(Rect x:rect){
  34. seg.add(new Seg(new Point(x.l,x.u),new Point(x.r,x.u)));
  35. seg.add(new Seg(new Point(x.l,x.d),new Point(x.r,x.d)));
  36. seg.add(new Seg(new Point(x.l,x.u),new Point(x.l,x.d)));
  37. seg.add(new Seg(new Point(x.r,x.u),new Point(x.r,x.d)));
  38. }
  39. }
  40.  
  41. float cross(PVector a, PVector b){
  42. return a.x*b.y-a.y*b.x;
  43. }
  44.  
  45. boolean intersect(Point a, Point b, Point c, Point d){
  46. PVector B=new PVector(b.x-a.x,b.y-a.y);
  47. PVector C=new PVector(c.x-a.x,c.y-a.y);
  48. PVector D=new PVector(d.x-a.x,d.y-a.y);
  49. float c1=cross(B,C);
  50. float c2=cross(B,D);
  51. int cc1=(int)(c1/abs(c1));
  52. int cc2=(int)(c2/abs(c2));
  53. println(cc1+" "+cc2+" "+c1+" "+c2);
  54. if(cc1+cc2!=0){
  55. return false;
  56. }
  57. println("HI AF");
  58. PVector A=new PVector(a.x-c.x,a.y-c.y);
  59. B=new PVector(b.x-c.x,b.y-c.y);
  60. D=new PVector(d.x-c.x,d.y-c.y);
  61. c1=cross(D,A);
  62. c2=cross(D,B);
  63. cc1=(int)(c1/abs(c1));
  64. cc2=(int)(c2/abs(c2));
  65. if(cc1+cc2!=0){
  66. return false;
  67. }
  68. return true;
  69. }
  70.  
  71. void draw(){
  72. clear();
  73. background(255);
  74. circle(st.x,st.y,rad);
  75. for(Rect x:rect){
  76. rect(x.l,x.u,x.r-x.l,x.d-x.u);
  77. }
  78. //line(st.x,st.y,mouseX,mouseY);
  79. PVector dir=new PVector(mouseX-st.x,mouseY-st.y);
  80. PVector mag=new PVector(mouseX-st.x,mouseY-st.y);
  81. println(dir.heading());
  82. dir.normalize();
  83. mag.normalize();
  84. mag.mult(2*SZ);
  85. for(Seg x:seg){
  86. println(x.x.x+" "+x.x.y);
  87. if(intersect(st,new Point(st.x+mag.x,st.y+mag.y),x.x,x.y)){
  88. println("HI INTER"+frameCount);
  89. float L=0,R=mag.mag();
  90. for(int i=0;i<30;i++){
  91. float M=(L+R)/2;
  92. PVector test=PVector.mult(dir,M);
  93. if(intersect(st,new Point(st.x+test.x,st.y+test.y),x.x,x.y)){
  94. R=M;
  95. } else L=M;
  96. }
  97. mag=PVector.mult(dir,L);
  98. }
  99. }
  100. line(st.x,st.y,st.x+mag.x,st.y+mag.y);
  101. }
  102.  
  103. final static int move=10;
  104.  
  105. void keyPressed(){//strafe and vel
  106. if(key==CODED){
  107. if(keyCode==LEFT){
  108. Point sy=new Point(st.x-move,st.y);
  109. if(!in(sy))st=sy;
  110. }
  111. if(keyCode==UP){
  112. Point sy=new Point(st.x,st.y-move);
  113. if(!in(sy))st=sy;
  114. }
  115. if(keyCode==RIGHT){
  116. Point sy=new Point(st.x+move,st.y);
  117. if(!in(sy))st=sy;
  118. }
  119. if(keyCode==DOWN){
  120. Point sy=new Point(st.x,st.y+move);
  121. if(!in(sy))st=sy;
  122. }
  123. }
  124. }
  125.  
  126. class Point{
  127. float x,y;
  128. Point(float a, float b){
  129. x=a;y=b;
  130. }
  131. }
  132.  
  133. class Seg{
  134. Point x, y;
  135. Seg(Point a, Point b){
  136. x=a;y=b;
  137. }
  138. }
  139.  
  140. class Rect{
  141. int l,r,u,d;
  142. Rect(int a, int b, int c, int e){
  143. if(c<=e){
  144. u=c;
  145. d=e;
  146. } else {
  147. u=e;
  148. d=c;
  149. }
  150. if(a<=b){
  151. l=a;
  152. r=b;
  153. } else {
  154. l=b;
  155. r=a;
  156. }
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement