package io.measures.passage.geometry;

import static io.measures.passage.Sketch.*;

public class SphericalPoint implements Projectable3D {

    private float r, phi, theta;

    public SphericalPoint(float r, float phi, float theta) {
        this.r = r;
        this.phi = phi;
        this.theta = theta;
    }

    public float r() {
        return r;
    }

    public float phi() {
        return phi;
    }

    public float theta() {
        return theta;
    }

    // todo - make latlng its own Projectable3D impl
    public SphericalPoint latlng(float r, float lat, float lng) {
        this.r = r;
        this.phi = radians(lng);
        this.theta = map(radians(lat), -PI/2, PI/2, 0, PI);
        return this;
    }

    public SphericalPoint extend(float dr) {
        return new SphericalPoint(r + dr, phi, theta);
    }

    public float x() { return r * sin(theta) * cos(phi); }
    public float y() { return r * sin(theta) * sin(phi); }
    public float z() { return r * cos(theta); }
}


@@ -995,12 +995,381 @@
 heta); %7D
+%0A%0A    public static SphericalPoint fromXYZ(float x, float y, float z) %7B%0A        float r = dist(0, 0, 0, x, y, z);%0A        float phi = atan(y/x);%0A        if(phi %3E TWO_PI) %7B%0A            phi = phi %25 TWO_PI;%0A        %7D%0A        while(phi %3C 0) %7B%0A            phi = TWO_PI + phi;%0A        %7D%0A        float theta = acos(z/r);%0A        return new SphericalPoint(r, phi, theta);%0A    %7D
 %0A%7D%0A%0A

build SphericalPoint from xyz