]>
Softwares of Agnibho - solarcompass.git/blob - solarcompass/CompassLogic.java
1 /**********************************************************************
3 * Description: Android app for finding directions using the sun
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com/solarcompass
6 **********************************************************************
7 Copyright (c) 2016 Agnibho Mondal
9 **********************************************************************
10 This file is part of Solar Compass.
12 Solar Compass is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Solar Compass is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Solar Compass. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
26 package com
.agnibho
.android
.solarcompass
;
28 import java
.util
.Calendar
;
29 import java
.util
.TimeZone
;
31 public class CompassLogic
{
37 public static double getAngle(){
38 return Math
.toDegrees(getAzimuthAngle());
45 private static double getDeclinationAngle(){
46 int dayNum
= Calendar
.getInstance().get(Calendar
.DAY_OF_YEAR
);
47 return 23.45*Math
.sin(Math
.toRadians(360.0/365.0*(284.0+dayNum
)));
54 private static double getHourAngle(){
55 Calendar cal
=Calendar
.getInstance();
56 cal
.setTimeZone(TimeZone
.getTimeZone("UTC"));
57 double utc
=(double)cal
.get(Calendar
.HOUR_OF_DAY
)+(double)cal
.get(Calendar
.MINUTE
)/60;
58 double timeDiff
=LocationData
.getInstance().getLongitude()/15;
59 double hour
=(utc
+timeDiff
)%24.0;
67 private static double getAltitudeAngle(){
68 double latitude
=Math
.toRadians(LocationData
.getInstance().getLatitude());
69 double declination
=Math
.toRadians(getDeclinationAngle());
70 double hourAngle
=Math
.toRadians(getHourAngle());
71 double sinAlt
=Math
.cos(latitude
)*Math
.cos(declination
)*Math
.cos(hourAngle
)+Math
.sin(latitude
)*Math
.sin(declination
);
72 return Math
.asin(sinAlt
);
79 private static double getAzimuthAngle(){
80 double latitude
=Math
.toRadians(LocationData
.getInstance().getLatitude());
81 double declination
=Math
.toRadians(getDeclinationAngle());
82 double altitude
=getAltitudeAngle();
83 double cosAz
=(Math
.sin(altitude
)*Math
.sin(latitude
)-Math
.sin(declination
))/(Math
.cos(altitude
)*Math
.cos(latitude
));
84 double azimuth
=Math
.acos(cosAz
);