summaryrefslogtreecommitdiff
path: root/trunk/src/nav/processLoc.java
blob: fdcd86307102d1fc09ed87882ce9f2580aefe11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * processLoc.java
 *
 * Created on July 14, 2007, 4:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package nav;

import java.lang.Thread;
import javax.microedition.location.*;

import gui.*;

/**
 *
 * @author manut
 */
public class processLoc extends Thread {
    
    private geoc gui;
    private Landmark dest;
    private Location loc;
    private Coordinates lastloc;
    private Coordinates north;
    
    /** Creates a new instance of processLoc */
    public processLoc(geoc _gui) {
        gui = _gui;
        north = new Coordinates(90, 0, 0);
        lastloc = null;
    }
    
    public void set(Location _loc, Landmark _dest){
        
        loc  = _loc;
        dest = _dest;
        
    }
    
    private double arcsin(double azimuth){
        double calc;
        double last_calc = 0;

        for(double i = -1; i <= 1; i=i+0.001){
            calc = java.lang.Math.sin(i);
            if ( calc == azimuth ){
                return i;
            } else if (last_calc <= azimuth && calc > azimuth){
                    return i;
            } else
                last_calc = calc;
        }
        return -2;
    }
    
    public void run(){
        //  calc course, distance
        // A = LastPosition
        // B = Destination
        // C = Northpole
        // D = ActPosition
        // E = going further on straight forward, until you meet BC
        //
        // alphaI  =  arcsin( ( CD * sin(gammaI) ) / AB )
        // betaI   =  arcsin( ( CD * sin(gammaII)) / BD )
        // deltaI  = 180 - alphaI - gammaI
        // deltaII = 180 - deltaI
        // epsilI  = 180 - deltaII - gammaII
        // epsilII = 180 - epsilI
        // gesucht = 180 - epsilII - betaI
        // --> courseCorrection =  - 180 - arcsin( (CD * sin(gammaI)) / AB ) - gammaI - gammaII - arcsin( CD * (sin(gammaII)) / BD )
        
       if( (lastloc!= null) && loc.isValid())
       {
            
            float gammaI  = lastloc.azimuthTo(loc.getQualifiedCoordinates());
            float gammaII = loc.getQualifiedCoordinates().azimuthTo(dest.getQualifiedCoordinates());
            float AB      = lastloc.distance(dest.getQualifiedCoordinates());
            float BD      = loc.getQualifiedCoordinates().distance(dest.getQualifiedCoordinates());
            float CD      = loc.getQualifiedCoordinates().distance(north);

            double actC = ( - 180 - arcsin( (CD * java.lang.Math.sin(gammaI)) / AB ) - gammaI - gammaII - arcsin( CD * (java.lang.Math.sin(gammaII)) / BD ) ) % 360;
            int actCourse = (int) actC;
            if (actCourse < 0)
                actCourse = actCourse+360;

            // set GUI
            gui.get_stringItemActNorth().setText(Coordinates.convert(loc.getQualifiedCoordinates().getLatitude(), 2));
            gui.get_stringItemActEast().setText(Coordinates.convert(loc.getQualifiedCoordinates().getLongitude(), 2));
            gui.get_stringItemActHeight().setText(String.valueOf(loc.getQualifiedCoordinates().getAltitude()));
            if ( (actCourse != 112) )
              gui.get_stringItemCourse().setText(String.valueOf(actCourse));
            gui.get_stringItemDistance().setText(String.valueOf(BD));

        }
        
        if ( loc.isValid() )
           lastloc = loc.getQualifiedCoordinates();
    }
    
}