1 /*
2  * Copyright 2005 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.common.geometry;
17 
18 
19 public final strictfp class S1Angle implements Comparable<S1Angle> {
20 
21   private final double radians;
22 
radians()23   public double radians() {
24     return radians;
25   }
26 
degrees()27   public double degrees() {
28     return radians * (180 / Math.PI);
29   }
30 
e5()31   public long e5() {
32     return Math.round(degrees() * 1e5);
33   }
34 
e6()35   public long e6() {
36     return Math.round(degrees() * 1e6);
37   }
38 
e7()39   public long e7() {
40     return Math.round(degrees() * 1e7);
41   }
42 
43   /**
44    * The default constructor yields a zero angle.
45    */
S1Angle()46   public S1Angle() {
47     this.radians = 0;
48   }
49 
S1Angle(double radians)50   private S1Angle(double radians) {
51     this.radians = radians;
52   }
53 
54   /**
55    * Return the angle between two points, which is also equal to the distance
56    * between these points on the unit sphere. The points do not need to be
57    * normalized.
58    */
S1Angle(S2Point x, S2Point y)59   public S1Angle(S2Point x, S2Point y) {
60     this.radians = x.angle(y);
61   }
62 
63   @Override
equals(Object that)64   public boolean equals(Object that) {
65     if (that instanceof S1Angle) {
66       return this.radians() == ((S1Angle) that).radians();
67     }
68     return false;
69   }
70 
71   @Override
hashCode()72   public int hashCode() {
73     long value = Double.doubleToLongBits(radians);
74     return (int) (value ^ (value >>> 32));
75   }
76 
lessThan(S1Angle that)77   public boolean lessThan(S1Angle that) {
78     return this.radians() < that.radians();
79   }
80 
greaterThan(S1Angle that)81   public boolean greaterThan(S1Angle that) {
82     return this.radians() > that.radians();
83   }
84 
lessOrEquals(S1Angle that)85   public boolean lessOrEquals(S1Angle that) {
86     return this.radians() <= that.radians();
87   }
88 
greaterOrEquals(S1Angle that)89   public boolean greaterOrEquals(S1Angle that) {
90     return this.radians() >= that.radians();
91   }
92 
max(S1Angle left, S1Angle right)93   public static S1Angle max(S1Angle left, S1Angle right) {
94     return right.greaterThan(left) ? right : left;
95   }
96 
min(S1Angle left, S1Angle right)97   public static S1Angle min(S1Angle left, S1Angle right) {
98     return right.greaterThan(left) ? left : right;
99   }
100 
radians(double radians)101   public static S1Angle radians(double radians) {
102     return new S1Angle(radians);
103   }
104 
degrees(double degrees)105   public static S1Angle degrees(double degrees) {
106     return new S1Angle(degrees * (Math.PI / 180));
107   }
108 
e5(long e5)109   public static S1Angle e5(long e5) {
110     return degrees(e5 * 1e-5);
111   }
112 
e6(long e6)113   public static S1Angle e6(long e6) {
114     // Multiplying by 1e-6 isn't quite as accurate as dividing by 1e6,
115     // but it's about 10 times faster and more than accurate enough.
116     return degrees(e6 * 1e-6);
117   }
118 
e7(long e7)119   public static S1Angle e7(long e7) {
120     return degrees(e7 * 1e-7);
121   }
122 
123   /**
124    * Writes the angle in degrees with a "d" suffix, e.g. "17.3745d". By default
125    * 6 digits are printed; this can be changed using setprecision(). Up to 17
126    * digits are required to distinguish one angle from another.
127    */
128   @Override
toString()129   public String toString() {
130     return degrees() + "d";
131   }
132 
133   @Override
compareTo(S1Angle that)134   public int compareTo(S1Angle that) {
135     return this.radians < that.radians ? -1 : this.radians > that.radians ? 1 : 0;
136   }
137 }
138