source: josm/trunk/src/org/openstreetmap/josm/data/coor/PolarCoor.java@ 13808

Last change on this file since 13808 was 13107, checked in by Don-vip, 6 years ago

extract PolarCoor from AlignInCircleAction

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import java.io.Serializable;
5import java.util.Objects;
6
7/**
8 * Polar coordinate.
9 * @since 13107 (extracted from {@code AlignInCircleAction})
10 */
11public class PolarCoor implements Serializable {
12
13 private static final long serialVersionUID = 1L;
14
15 /**
16 * Radial coordinate (distance from the pole).
17 */
18 public final double radius;
19
20 /**
21 * Angular coordinate in radians.
22 */
23 public final double angle;
24
25 /**
26 * Reference point (analogous to the origin of a Cartesian coordinate system).
27 */
28 public final EastNorth pole;
29
30 /**
31 * Constructs a new {@code PolarCoor}, using {@code (0,0)} as pole.
32 * @param radius radial coordinate (distance from the pole)
33 * @param angle angular coordinate in radians
34 */
35 public PolarCoor(double radius, double angle) {
36 this(radius, angle, new EastNorth(0, 0));
37 }
38
39 /**
40 * Constructs a new {@code PolarCoor}.
41 * @param radius radial coordinate (distance from the pole)
42 * @param angle angular coordinate in radians
43 * @param pole reference point (analogous to the origin of a Cartesian coordinate system)
44 */
45 public PolarCoor(double radius, double angle, EastNorth pole) {
46 this.radius = radius;
47 this.angle = angle;
48 this.pole = pole;
49 }
50
51 /**
52 * Constructs a new {@code PolarCoor} from an {@link EastNorth}, using {@code (0,0)} as pole.
53 * @param en east/north coordinates
54 */
55 public PolarCoor(EastNorth en) {
56 this(en, new EastNorth(0, 0));
57 }
58
59 /**
60 * Constructs a new {@code PolarCoor}.
61 * @param en east/north coordinates
62 * @param pole reference point (analogous to the origin of a Cartesian coordinate system)
63 */
64 public PolarCoor(EastNorth en, EastNorth pole) {
65 this(en.distance(pole), computeAngle(en, pole), pole);
66 }
67
68 /**
69 * Compute polar angle between an east/north and the pole.
70 * @param en east/north coordinates
71 * @param pole reference point (analogous to the origin of a Cartesian coordinate system)
72 * @return polar angle in radians
73 */
74 public static double computeAngle(EastNorth en, EastNorth pole) {
75 return Math.atan2(en.north() - pole.north(), en.east() - pole.east());
76 }
77
78 /**
79 * Converts this {@code PolarCoor} to an {@link EastNorth} instance.
80 * @return a new {@code EastNorth} instance
81 */
82 public EastNorth toEastNorth() {
83 return new EastNorth(
84 radius * Math.cos(angle) + pole.east(),
85 radius * Math.sin(angle) + pole.north());
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(radius, angle, pole);
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj) return true;
96 if (obj == null || getClass() != obj.getClass()) return false;
97 PolarCoor that = (PolarCoor) obj;
98 return Double.compare(that.radius, radius) == 0 &&
99 Double.compare(that.angle, angle) == 0 &&
100 Objects.equals(that.pole, pole);
101 }
102
103 @Override
104 public String toString() {
105 return "PolarCoor [radius=" + radius + ", angle=" + angle + ", pole=" + pole + ']';
106 }
107}
Note: See TracBrowser for help on using the repository browser.