package org.jzy3d.maths;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A {@link Coord3d} stores a 3 dimensional coordinate for cartesian or polar
 * mode, and provide few operators.
 * 
 * Operators allow adding, substracting, multiplying and divising coordinate
 * values, as well as computing the distance between two points, and converting
 * polar and cartesian coordinates.
 * 
 * @author Martin Pernollet
 * 
 */
public class Coord3d implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -1636927109633279805L;

    public static List list(int size){
        return new ArrayList(size);
    }
    
    public static Range getZRange(List coords) {
        float min = Float.POSITIVE_INFINITY;
        float max = Float.NEGATIVE_INFINITY;
        
        for(Coord3d c: coords){
            if(c.z>max)
                max = c.z;
            if(c.z 0)
            return new Coord3d(0, Math.PI / 2, r);
        else if (d == 0 && z <= 0)
            return new Coord3d(0, -Math.PI / 2, r);
        // other cases
        else {
            // classical case for azimuth
            if (Math.abs(x / d) < 1)
                a = Math.acos(x / d) * (y > 0 ? 1 : -1);
            // special on each pole for azimuth
            else if (y == 0 && x > 0) // y==0
                a = 0;
            else if (y == 0 && x < 0)
                a = Math.PI;
            else
                a = 0;

            e = Math.atan(z / d);

            return new Coord3d(a, e, r);
        }

        /*
         * return new Coord3d( Math.atan(y/x), // azimuth Math.acos(z/r), //
         * elevation r); // range
         */
    }

    /** Compute the distance between two coordinates. */
    public double distance(Coord3d c) {
        return Math.sqrt(distanceSq(c));
    }

    public double distanceSq(Coord3d c) {
        return Math.pow(x - c.x, 2) + Math.pow(y - c.y, 2) + Math.pow(z - c.z, 2);
    }

    /**************************************************************/

    public float magSquared() {
        return x * x + y * y + z * z;
    }

    public Coord3d getNormalizedTo(float len) {
        return clone().normalizeTo(len);
    }

    public Coord3d normalizeTo(float len) {
        float mag = (float) Math.sqrt(x * x + y * y + z * z);
        if (mag > 0) {
            mag = len / mag;
            x *= mag;
            y *= mag;
            z *= mag;
        }
        return this;
    }

    public final float dot(Coord3d v) {
        return x * v.x + y * v.y + z * v.z;
    }

    public final Coord3d cross(Coord3d v) {
        return new Coord3d(
                y * v.z - z * v.y,
                z * v.x - x * v.z,
                x * v.y - y * v.x
        );
    }

    /**
     * Applies a rotation represented by the AxisAngle
     * notation using the Rodrigues' rotation formula.
     * 

* math implemented using * http://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula * * @param angleDeg angle of rotation about the given axis [deg] * @param axis unit vector describing an axis of rotation * @return rotated copy of the original vector */ public final Coord3d rotate(float angleDeg, Coord3d axis) { float angleRad = (float) Math.toRadians(angleDeg); float s = (float) Math.sin(angleRad); float c = (float) Math.cos(angleRad); Coord3d v = this; Coord3d k = axis.normalizeTo(1f); float kdotv = k.dot(v); Coord3d kXv = k.cross(v); return new Coord3d( v.x * c + kXv.x * s + k.x * kdotv * (1 - c), v.y * c + kXv.y * s + k.y * kdotv * (1 - c), v.z * c + kXv.z * s + k.z * kdotv * (1 - c)); } public final Coord3d interpolateTo(Coord3d v, float f) { return new Coord3d(x + (v.x - x) * f, y + (v.y - y) * f, z + (v.z - z) * f); } /**************************************************************/ /** Return a string representation of this coordinate. */ @Override public String toString() { return ("x=" + x + " y=" + y + " z=" + z); } /** Return an array representation of this coordinate. */ public float[] toArray() { float[] array = { x, y, z }; return array; } /**************************************************************/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Float.floatToIntBits(x); result = prime * result + Float.floatToIntBits(y); result = prime * result + Float.floatToIntBits(z); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Coord3d)) return false; Coord3d other = (Coord3d) obj; if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x)) return false; if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y)) return false; if (Float.floatToIntBits(z) != Float.floatToIntBits(other.z)) return false; return true; } /**************************************************************/ public float x; public float y; public float z; }

Ads help maintain this website.