Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with Bearing calculation appearing as NaN

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Bearing calculation appearing as NaN

    Im getting an NaN error when displaying a calculation. The programme requires the user the enter a bearing and distance from an inital point (this being 0,0). It then findest the closest station and works out the distance. The issue im getting is when it is trying to work out what direction the closest station is situated based on degrees. It coud be something simple that the IF is wrong but i'm getting very confused. Can anyone please help.

    import javax.swing.*;

    public class Assignment {

    public static void main(String[] args) {
    double tempBearing;
    double tempLength;
    double distanceToClosest;
    double stationx;
    double stationy;
    double currentx;
    double currenty;
    double bearing;
    double degree;


    //an array of stations
    Station[] stations = new Station[16];
    //populate the array with stations
    stations[0] = new Station("Windmill Hill",new Location(60.0,12.0));
    stations[1] = new Station("Trim Bridge",new Location(63.0,6.0));
    stations[2] = new Station("Pirac Cresent",new Location(80.0,8.0));
    stations[3] = new Station("Easton",new Location(85.0,12.0));
    stations[4] = new Station("Parkway",new Location(102.0,9.0));
    stations[5] = new Station("Temple Fields",new Location(140.0,7.0));
    stations[6] = new Station("St Dennis",new Location(180.0,1.0));
    stations[7] = new Station("Moxbridge",new Location(180.0,2.0));
    stations[8] = new Station("Shakespeare Court",new Location(192.0,5.0));
    stations[9] = new Station("Weston-On-Shore",new Location(232.0,14.0));
    stations[10] = new Station("Jacobs Well",new Location(240.0,3.0));
    stations[11] = new Station("Central",new Location(243.0,5.0));
    stations[12] = new Station("Newbridge",new Location(245.0,11.0));
    stations[13] = new Station("Tivoli",new Location(275.0,6.0));
    stations[14] = new Station("Clifton Street",new Location(285.0,11.0));
    stations[15] = new Station("St Judes Hill",new Location(355.0,4.0));

    //an array of Lines
    LineName[] Line = new LineName[16];
    //populate the array with LineNames
    Line[0] = new LineName ("Docks Line");
    Line[1] = new LineName ("Docks Line");
    Line[2] = new LineName ("Gyratory Line");
    Line[3] = new LineName ("Brunel Line");
    Line[4] = new LineName ("Brunel Line");
    Line[5] = new LineName ("Gyratory Line");
    Line[6] = new LineName ("Docks Line");
    Line[7] = new LineName ("Brunel Line");
    Line[8] = new LineName ("Gyratory Line");
    Line[9] = new LineName ("Docks Line");
    Line[10] = new LineName ("Docks Line");
    Line[11] = new LineName ("Docks Line");
    Line[12] = new LineName ("Docks Line");
    Line[13] = new LineName ("Brunel Line");
    Line[14] = new LineName ("Brunel Line");
    Line[15] = new LineName ("Gyratory Line");


    //get current location and create a location object

    String temp = JOptionPane.showInputDialog("Bearing to current location?");
    tempBearing = Double.parseDouble(temp);
    temp = JOptionPane.showInputDialog("distance from origin of current?");
    tempLength = Double.parseDouble(temp);

    Location current = new Location(tempBearing, tempLength);
    Station closest = stations[0];
    LineName line = Line[0];
    distanceToClosest = current.calcDistance(stations[0].location);

    for(int i = 1; i < stations.length; i++){
    double tempDist = current.calcDistance(stations[i].location);

    if(tempDist < distanceToClosest){
    closest = stations[i];
    line = Line[i];
    distanceToClosest = tempDist;
    }//if (tempDist < distanceToClosest)
    }//for loop

    currentx = tempLength * Math.sin(Math.toRadians(tempBearing));
    currenty = tempLength * Math.cos(Math.toRadians(tempBearing));
    stationx = current.calcx(stations[0].location);
    stationy = current.calcy(stations[0].location);
    bearing = Math.asin((stationx - currentx ) / distanceToClosest );
    bearing = Math.toDegrees(bearing);

    if ((stationy >= currenty) && (stationx >= currentx)) { degree = bearing ;
    }
    else if ((stationy <= currenty) && (stationx >= currentx)) { degree = (180.0 - bearing) ;
    }
    else if ((stationy <= currenty) && (stationx <= currentx)) { degree = (180.0 + bearing) ;
    }
    else {
    degree = 360.0 - bearing;
    }



    JOptionPane.showMessageDialog(null, "the closest station is " + closest.name + " on " + line.Lname + "."

    + "\nIt is " + distanceToClosest + " away." + " Bearing of " + degree);

    }
    }
    class Station{ // a station class

    Location location;
    String name;

    public Station(String n, Location l){
    name = n;
    location = l;
    }
    }
    class LineName{ // a station line class

    String Lname;

    public LineName(String r){

    Lname = r;
    }
    }
    class Location{
    double bearing;
    double length;

    public Location(){
    bearing = 0.0;
    length = 0.0;
    }
    public Location(double b, double l){
    bearing = b;
    length = l;
    }
    public double calcDistance(Location l){
    double angle = Math.abs(bearing - l.bearing);//calculating the angle between the bearings
    double temp = (length*length)+(l.length*l.length)-(2*length*l.length*Math.cos(Math.toRadians(angle)) );
    return Math.pow(temp, 0.5);
    }
    public double calcx(Location l){
    double tempx= l.length * Math.sin(Math.toRadians(l.bearing));
    return tempx;
    }
    public double calcy(Location l){
    double tempy= l.length * Math.cos(Math.toRadians(l.bearing));
    return tempy;


    }

    }//location


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Problem with Bearing calculation appearing as NaN

    Prynhawn da Welshcrossy

    I suggest you read the documentation for Math.asin() here, then put println()'s into your program to see the value returned from computing (stationx - currentx) / distanceToClosest.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Bearing calculation appearing as NaN

    Prynhawn da Newbie, thanks for the quick response.

    I read that but didnt really make much sense to me. I have put in a bearing of 180 and a length of 40 the (stationx - currentx) / distanceToClosest calulcated 18.20?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Problem with Bearing calculation appearing as NaN

    The API reads; " If the argument is NaN or its absolute value is greater than 1, then the result is NaN ".
    Make sense?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Trigonometry - Bearing
    By L8TON in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 18th, 2012, 06:03 PM
  2. Replies: 1
    Last Post: August 15th, 2011, 09:26 AM
  3. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM
  4. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM
  5. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM