// RPApp: Transformar y operar coordenadas Rectangular <--> Polar");
import java.util.Scanner;
public class RPApp{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double x, y, r, g;
System.out.println("Transformar y operar coordenadas Rectangular <--> Polar");
System.out.print("Ingrese un punto rectangular (x, y): ");
x = sc.nextDouble();
y = sc.nextDouble();
RP rec1 = new RP(x, y);
RP pol1 = RP.recToPol(rec1);
System.out.printf("(%.2f, %.2f) ≡ (%.2f, %.2f°)\n", rec1.x, rec1.y, pol1.x, pol1.y);
System.out.print("\nIngrese un punto polar (r, g): ");
r = sc.nextDouble();
g = sc.nextDouble();
RP pol2 = new RP(r, g);
RP rec2 = RP.polToRec(pol2);
System.out.printf("(%.2f, %.2f°) ≡ (%.2f, %.2f)\n", pol2.x, pol2.y, rec2.x, rec2.y);
RP rec3 = RP.sumaRec(rec1, rec2);
System.out.printf("\nSuma de rectangulares: (%.2f, %.2f) + (%.2f, %.2f) ≡ (%.2f, %.2f)\n",
rec1.x, rec1.y, rec2.x, rec2.y, rec3.x, rec3.y);
RP pol3 = RP.sumaPol(pol1, pol2);
System.out.printf("Suma de polares : (%.2f, %.2f°) + (%.2f, %.2f°) ≡ (%.2f, %.2f°)\n",
pol1.x, pol1.y, pol2.x, pol2.y, pol3.x, pol3.y);
}
}
class RP{ // Coordenadas rectángulares y polares
double x, y; // rec: (x, y) o polar: (x=radio, y = ángulo en grados )
RP(double x, double y){ // constructor
this.x = x;
this.y = y;
}
static RP polToRec(RP pol){
double radian = pol.y*Math.PI/180.0;
return(new RP(pol.x*Math.cos(radian), pol.x*Math.sin(radian)));
}
static RP recToPol(RP rec){
return(new RP(Math.sqrt(rec.x*rec.x+rec.y*rec.y), Math.atan(rec.y/rec.x)*180.0/Math.PI));
}
static RP sumaRec(RP r1, RP r2){
return(new RP(r1.x+r2.x, r1.y+r2.y));
}
static RP sumaPol(RP p1, RP p2){
return recToPol(sumaRec(polToRec(p1), polToRec(p2)));
}
}