Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. float planeX, planeY;
  2. float vektorx, vektory;
  3. int mouseX2, mouseY2;
  4. float radius;
  5. int i;
  6.  
  7. void setup() {
  8.   size(640, 400);
  9.  
  10.   planeX = 320;
  11.   planeY = 200;
  12.  
  13.   radius = 50;
  14.  
  15.   i = 0;
  16. }
  17.  
  18. void draw() {
  19.   background(150, 150, 220);
  20.  
  21.   noFill();
  22.  
  23.   rectMode(CENTER);
  24.  
  25.   //Kreis um mouse und plane berechnen
  26.   int kreisRadius = getDistance(mouseX, mouseY, planeX, planeY) / 2;
  27.   int kreisMitteX = (mouseX + planeX) / 2;
  28.   int kreisMitteY = (mouseY + planeY) / 2;  
  29.  
  30.   ellipse(mouseX, mouseY, radius * 2, radius * 2);
  31.  
  32.   //Punkte z1 und z2 berechnen
  33.   //http://stackoverflow.com/questions/3349125/circle-circle-intersection-points
  34.   int d = getDistance(kreisMitteX, kreisMitteY, mouseX, mouseY);
  35.   int a = (pow(radius, 2) - pow(kreisRadius, 2) + pow(d, 2)) / (2 * d);
  36.  
  37.   int p2x = mouseX + a * (kreisMitteX - mouseX) / d;
  38.   int p2y = mouseY + a * (kreisMitteY - mouseY) / d;
  39.  
  40.   int h = sqrt(pow(radius, 2) - pow(a, 2));
  41.  
  42.   int z1x = p2x + h * (mouseY - kreisMitteY) / d;
  43.   int z1y = p2y - h * (mouseX - kreisMitteX) / d;
  44.  
  45.   int z2x = p2x - h * (mouseY - kreisMitteY) / d;
  46.   int z2y = p2y + h * (mouseX - kreisMitteX) / d;
  47.  
  48.   fill(255, 255, 255);
  49.  
  50.   ellipse(mouseX, mouseY, 20, 20);
  51.  
  52.   rect(planeX,  planeY,  20,  20);
  53.  
  54.   //Das Flugzeug bewegen
  55.   if(getDistance(planeX, planeY, mouseX, mouseY) > radius + 10) {
  56.     if(abs(mouseX - mouseX2) > 10 || abs(mouseY - mouseY2) > 10) {
  57.       vektorx = planeX - z1x;
  58.       vektory = planeY - z1y;
  59.    
  60.       float size = sqrt(pow(vektorx, 2) + pow(vektory, 2));
  61.    
  62.       if (size > 0) {
  63.         vektorx = vektorx / size;
  64.         vektory = vektory / size;
  65.       }
  66.      
  67.       i = 0;
  68.      
  69.       mouseX2 = mouseX;
  70.       mouseY2 = mouseY;
  71.     }
  72.     planeX -= vektorx;
  73.     planeY -= vektory;
  74.   }
  75.   else {
  76.     planeX = radius * cos(3.1415926f / 180.0f * i) + mouseX;
  77.     planeY = radius * sin(3.1415926f / 180.0f * i) + mouseY;
  78.    
  79.     i++;
  80.   }
  81. }
  82.  
  83. int getDistance(int x1, int y1, int x2, int y2) {
  84.   return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement