All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.visibility.PolarPoint Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.visibility;

/**
 Ported by David Turner from Visilibity, by Karl J. Obermeyer

 This port undoubtedly introduced a number of bugs (and removed some features).

 Bug reports should be directed to the OpenTripPlanner project, unless they
 can be reproduced in the original VisiLibity
 */
class PolarPoint extends VLPoint implements Comparable {

    VLPoint polar_origin;

    // Polar coordinates where radius always positive, and angle
    // measured ccw from the world coordinate system's x-axis.
    double range = Double.NaN;

    Angle bearing = new Angle(Double.NaN);

    public PolarPoint(VLPoint polar_origin_temp, VLPoint point_temp) {
        this(polar_origin_temp, point_temp, 0);
    }

    public PolarPoint(VLPoint polar_origin_temp, VLPoint point_temp, double epsilon) {
        super(point_temp);
        polar_origin = polar_origin_temp.clone();
        if (polar_origin.distance(point_temp) <= epsilon) {
            bearing = new Angle(0.0);
            range = 0.0;
        } else {
            bearing = new Angle(point_temp.y - polar_origin_temp.y, point_temp.x
                    - polar_origin_temp.x);
            range = polar_origin_temp.distance(point_temp);
        }
    }

    public void set_polar_origin(VLPoint polar_origin_temp) {
        PolarPoint newPoint = new PolarPoint(polar_origin_temp, new VLPoint(x, y));
        setFromPolarPoint(newPoint);
    }

    void setFromPolarPoint(PolarPoint newPoint) {
        this.polar_origin = newPoint.polar_origin.clone();
        this.range = newPoint.range;
        this.bearing = newPoint.bearing.clone();
        this.x = newPoint.x;
        this.y = newPoint.y;
    }

    public PolarPoint clone() {
        PolarPoint clone = new PolarPoint();
        clone.setFromPolarPoint(this);
        return clone;
    }

    public void set_x(double x_temp) {
        PolarPoint newPoint = new PolarPoint(polar_origin, new VLPoint(x_temp, y));
        setFromPolarPoint(newPoint);
    }

    public void set_y(double y_temp) {
        PolarPoint newPoint = new PolarPoint(polar_origin, new VLPoint(x, y_temp));
        setFromPolarPoint(newPoint);
    }

    public void set_range(double range_temp) {
        range = range_temp;
        x = polar_origin.x + range * Math.cos(bearing.get());
        y = polar_origin.y + range * Math.sin(bearing.get());
    }

    public void set_bearing(Angle bearing_temp) {
        bearing = bearing_temp.clone();
        x = polar_origin.x + range * Math.cos(bearing.get());
        y = polar_origin.y + range * Math.sin(bearing.get());
    }

    public boolean equals(Object o) {
        if (!(o instanceof PolarPoint)) {
            return false;
        }
        PolarPoint polar_point2 = (PolarPoint) o;
        return polar_origin.equals(polar_point2.polar_origin) && range == polar_point2.range
                && bearing.equals(polar_point2.bearing);
    }

    public int compareTo(VLPoint point2) {
        if (!(point2 instanceof PolarPoint)) {
            return super.compareTo(point2);
        }
        PolarPoint polar_point2 = (PolarPoint) point2;
        int bearingComp = bearing.compareTo(polar_point2.bearing);
        if (bearingComp == 0) {
            return (int) Math.signum(range - polar_point2.range);
        }
        return bearingComp;

    }

    void set_bearing_to_2pi() {
        bearing.set_to_2pi();
    }

    public PolarPoint() {
        super();
        range = Double.NaN;
        bearing = new Angle(Double.NaN);
    }

    public String toString() {
        return "PolarPoint(" + bearing + "  " + range + ") of " + super.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy