/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Capitulo_3.NumerosComplejos;

/**
 *
 * @author felix
 */
public class Complejo {
    private double real;
    private double imaginaria;
    
    Complejo(double r, double i) {
        real = r;
        imaginaria = i;
    }
    
    static public Complejo Conjugado(Complejo a) {
        return new Complejo(a.real, -a.imaginaria);
    }
    
    public double Angulo() {
        return Math.atan2(imaginaria, real);
    }
    
    static public Complejo Division(Complejo a, Complejo b) {
        double r, i, mag;
        
        mag = b.real*b.real + b.imaginaria*b.imaginaria;
        
        r = a.real*b.real + a.imaginaria*b.imaginaria;
        i = -a.real*b.imaginaria + a.imaginaria*b.real;
        
        return new Complejo(r/mag, i/mag);
    }

    static public Complejo Suma(Complejo a, Complejo b) {
        double r, i;
        
        r = a.real + b.real;
        i = a.imaginaria + b.imaginaria;
        
        return new Complejo(r, i);
    }

    static public Complejo Resta(Complejo a, Complejo b) {
        double r, i;
        
        r = a.real - b.real;
        i = a.imaginaria - b.imaginaria;
        
        return new Complejo(r, i);
    }
    
    public double Real() {
        return real;
    }
    
    public double Imaginaria() {
        return imaginaria;
    }

    static public Complejo Multiplica(Complejo a, Complejo b) {
        double r, i;
        
        r = a.real*b.real - a.imaginaria*b.imaginaria;
        i = a.real*b.imaginaria + a.imaginaria*b.real;
        
        return new Complejo(r, i);
    }
    
    public double Magnitud() {
        return Math.sqrt(real*real + imaginaria*imaginaria);
    }
    
    @Override
    public String toString() {
        String aux = "";
        aux += real;
        if (imaginaria < 0) 
            aux += " - j "+(-imaginaria);
        else if(imaginaria > 0)
            aux += " + j " + imaginaria;
        return aux;
    }
}
