]> Softwares of Agnibho - solarcompass.git/blob - solarcompass/src/main/java/com/agnibho/android/solarcompass/CompassLogic.java
Initial commit
[solarcompass.git] / solarcompass / src / main / java / com / agnibho / android / solarcompass / CompassLogic.java
1 /**********************************************************************
2 * Title: Solar Compass
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
8 All rights reserved
9 **********************************************************************
10 This file is part of Solar Compass.
11
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.
16
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.
21
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 **********************************************************************/
25
26 package com.agnibho.android.solarcompass;
27
28 import java.util.Calendar;
29 import java.util.TimeZone;
30
31 public class CompassLogic {
32
33 /*
34 Find Compass Angle
35 Unit - Degrees
36 */
37 public static double getAngle(){
38 return Math.toDegrees(getAzimuthAngle());
39 }
40
41 /*
42 Declination Angle
43 Unit - Degrees
44 */
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)));
48 }
49
50 /*
51 Hour Angle
52 Unit - Degrees
53 */
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;
60 return (12-hour)*15;
61 }
62
63 /*
64 Altitude Angle
65 Unit - Radians
66 */
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);
73 }
74
75 /*
76 Azimuth Angle
77 Unit - Radians
78 */
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);
85 if(getHourAngle()<0){
86 if(azimuth>0){
87 azimuth=-azimuth;
88 }
89 }
90 else{
91 if(azimuth<0){
92 azimuth=-azimuth;
93 }
94 }
95 return azimuth;
96 }
97 }