Deberías plantear el problema en coordenas polares, te vas a ahorrar bastante trabajo:
// ==============================================// Title: Pruebas coordenadas y rotación// Autor: fre3men// Copyright: CC 3.0 By-Nc-Sa// ==============================================// NOTA: Hay que tener en cuenta que las coordenadas Y (ordenadas) están invertidas// a causa de que a la hora de dibujar, tenemos en cuenta que siempre es el centro 0,0// así que para desplazar hacia arriba en el eje Y este es negativo en pantalla// https://es.wikipedia.org/wiki/Circunferencia// https://processing.org/tutorials/trig/// GLOBALSint posX = 0, posY = 0; // Para posicionar el objeto Aint posX_c = 0, posY_c = 0; // Punto de rotaciónfloat distance = 0; // Distancia entre el punto de rotación y el objetofloat angle = 0;float theta = 0; //2*PI;int rotation = 0;// SETUPvoid setup() { size(500, 500); frameRate(30); smooth(); // Aplicamos el filtro anti-aliasing rectMode(CENTER); ellipseMode(CENTER);}void draw() { background(0); // CLEAR SCREEN noFill(); // sin relleno stroke(255); // white //d = sqrt((x - h)^2 + (y - k)^2); //x = h + r * cos t; //y = k + r * sin t; //t = arc cos ((x - h) / r) //t = arc sin ((y - k) / r) //a = t * 180/PI -> degrees //t = a * PI/180 -> radians distance = sqrt((posX - posX_c) * (posX - posX_c) + (posY - posY_c) * (posY - posY_c)); if (distance != 0) { // Evitar error de división por cero theta = asin(posY / distance); // La distancia es como la hipotenusa o radio si tenemos en cuenta que el vector respecto la rotación forma parte de una circunferencia //theta = acos(posX / distance); angle = degrees(theta); // Pasamos a grados //theta = radians(angle); } else { theta = angle = 0; } pushMatrix(); translate(width/2, height/2); // Desplazamos el punto de dibujo al medio de la pantalla rotate(radians(rotation)); // Dibujamos los ejes cartesianos stroke(255); line(0, -200, 0, 200); line(-200, 0, 200, 0); ellipse(posX_c, posY_c, 150, 150); ellipse(posX, posY, 50, 50); // Objeto A stroke(75); line(0, 0, posX, posY); // Línea que nos remarca la distancia entre el punto A y el punto B fill(255); textSize(20); text("Eje X", -180, -10); text("Eje Y", +10, -180); popMatrix(); textSize(15); text("PosX: " + posX, width/2 + 50, height/2 + 100); text("PosY: " + posY, width/2 + 50, height/2 + 120); text("Distancia: " + distance, width/2 + 50, height/2 + 140); text("Ángulo: " + angle + "º", width/2 + 50, height/2 + 160); text("Rotación: " + rotation + "º", width/2 + 50, height/2 + 180); if (rotation != 0) { float x = posX_c + distance * cos(theta + radians(rotation)); float y = posY_c + distance * sin(theta + radians(rotation)); text("Ángulo+Rot: " + (angle + (float)rotation) + "º", width/2 -210, height/2 + 140); text("PosX real: " + x, width/2 - 210, height/2 + 160); text("PosY real: " + y, width/2 - 210, height/2 + 180); } }void mousePressed() { posX = mouseX - width/2; posY = mouseY - width/2;}void keyPressed() { switch(keyCode) { case RIGHT: posX++; break; case LEFT: posX--; break; case UP: posY--; break; case DOWN: posY++; break; case 82: // R rotation++; break; case 69: // E rotation--; break; }}
if (posX == 0 && posY >= 0) { theta = PI/2; } else if (posX == 0 && posY < 0) { theta = 3*PI/2; } else if (posX > 0 && posY >= 0) { theta = atan(posY / posX); } else if (posX > 0 && posY <0) { theta = atan(posY / posX) + 2*PI; } else if (posX < 0) { theta = atan(posY / posX) + PI; } angle = degrees(theta); // Pasamos a grados
distance = sqrt(posX * posX + posY * posY); x = distance * cos(theta + radians(rotation)); y = distance * sin(theta + radians(rotation));
Fre3men yo no tengo ni idea de mates... Pero me tienes realmente intrigado