Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float planeX, planeY;
- float vektorx, vektory;
- int mouseX2, mouseY2;
- float radius;
- int i;
- void setup() {
- size(640, 400);
- planeX = 320;
- planeY = 200;
- radius = 50;
- i = 0;
- }
- void draw() {
- background(150, 150, 220);
- noFill();
- rectMode(CENTER);
- //Kreis um mouse und plane berechnen
- int kreisRadius = getDistance(mouseX, mouseY, planeX, planeY) / 2;
- int kreisMitteX = (mouseX + planeX) / 2;
- int kreisMitteY = (mouseY + planeY) / 2;
- ellipse(mouseX, mouseY, radius * 2, radius * 2);
- //Punkte z1 und z2 berechnen
- //http://stackoverflow.com/questions/3349125/circle-circle-intersection-points
- int d = getDistance(kreisMitteX, kreisMitteY, mouseX, mouseY);
- int a = (pow(radius, 2) - pow(kreisRadius, 2) + pow(d, 2)) / (2 * d);
- int p2x = mouseX + a * (kreisMitteX - mouseX) / d;
- int p2y = mouseY + a * (kreisMitteY - mouseY) / d;
- int h = sqrt(pow(radius, 2) - pow(a, 2));
- int z1x = p2x + h * (mouseY - kreisMitteY) / d;
- int z1y = p2y - h * (mouseX - kreisMitteX) / d;
- int z2x = p2x - h * (mouseY - kreisMitteY) / d;
- int z2y = p2y + h * (mouseX - kreisMitteX) / d;
- fill(255, 255, 255);
- ellipse(mouseX, mouseY, 20, 20);
- rect(planeX, planeY, 20, 20);
- //Das Flugzeug bewegen
- if(getDistance(planeX, planeY, mouseX, mouseY) > radius + 10) {
- if(abs(mouseX - mouseX2) > 10 || abs(mouseY - mouseY2) > 10) {
- vektorx = planeX - z1x;
- vektory = planeY - z1y;
- float size = sqrt(pow(vektorx, 2) + pow(vektory, 2));
- if (size > 0) {
- vektorx = vektorx / size;
- vektory = vektory / size;
- }
- i = 0;
- mouseX2 = mouseX;
- mouseY2 = mouseY;
- }
- planeX -= vektorx;
- planeY -= vektory;
- }
- else {
- planeX = radius * cos(3.1415926f / 180.0f * i) + mouseX;
- planeY = radius * sin(3.1415926f / 180.0f * i) + mouseY;
- i++;
- }
- }
- int getDistance(int x1, int y1, int x2, int y2) {
- return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement