|
Garry Freemyer wrote: return (theDegrees + 360) / 360;
can't be right, you want modulus, hence %
BTW: I don't like your 57.2957795 constant, what you want there is 180./M_PI so radianToDegrees inverts whatever degreesToRadian does as good as it possibly can.
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text, and PictureBoxes for pictures, not drawings.
modified on Friday, June 10, 2011 12:29 PM
|
|
|
|
|
Good catch. I can't believe I keep replacing the % with /.
I did the changes, and when I fed in longitude and latitude for Paradise CA to Colorado Springs Colorado, I got a wildly unexpected result.
Given the code fragments below the angle I was expecting was something like 57 degrees came back and said it was 1 degree! NORTH!!
I get the formula working for Paradise CA to Colorado Springs and it breaks down when I try for a Destination of San Diego, and if I fix San Diego, it breaks for Colorado Springs. I might as well be passing these figures into the random function. I would scream but you are trying to help me and it would not be polite.
...
#define ParadiseLat 39.743
#define ParadiseLon -121.605
#define SanDiegoLat 32.723
#define SanDiegoLon -117.165
#define ColoSpringsLat 38.835
#define ColoSpringsLon -104.823
...
double lat1 = degreesToRadian(ParadiseLat);
double lon1 = degreesToRadian(ParadiseLon);
double lat2 = degreesToRadian(ColoSpringsLat);
double lon2 = degreesToRadian(ColoSpringsLon);
....
double radianToDegrees(double x)
{
return x * 180./M_PI;
}
...
int latitudeLongitudeLocationsToAngleOfTravelInDegrees(double latRad1, double lonRad1, double latRad2, double lonRad2)
{
//double theRadians = atan2(cos(latRad1)*sin(latRad2)-sin(latRad1)*cos(latRad2)*cos(lonRad2-lonRad1), sin(lonRad2-lonRad1)*cos(latRad2));
//double theDegreesInRad = radianToDegrees(theRadians);
//theDegreesInRad = (theDegreesInRad * PI / 180) + 360;
//int theDegrees = radianToDegrees(theDegreesInRad);
//theDegrees = theDegrees % 360;
double theAngleInRad = atan2(cos(latRad1)*sin(latRad2)-sin(latRad1)*cos(latRad2)*cos(lonRad2-lonRad1), sin(lonRad2-lonRad1)*cos(latRad2));
int theDegrees = radianToDegrees(theAngleInRad);
return (theDegrees + 360) % 360;
// return theDegrees;
}
Been there, done that, forgot why!
|
|
|
|
|
Hi,
I would prefer you show actual code, "..." would not compile, function degreesToRadian now has vanished;
also please make life easier to yourself and us CPians, by dropping the statements you commented out, and by putting the code in PRE tags (use the CODE BLOCK button) that would make it more readable, like so:
#define ParadiseLat 39.743
#define ParadiseLon -121.605
#define SanDiegoLat 32.723
#define SanDiegoLon -117.165
Garry Freemyer wrote: I get the formula working for Paradise CA to Colorado Springs and it breaks down when I try for a Destination of San Diego, and if I fix San Diego, it breaks for Colorado Springs.
I can't imagine what that could possibly mean; the formulae and the code either are correct or they are not, how could you "fix" them for a specific city?
If you need to debug, I recommend you turn the atan2 line into three lines (and insert some Console.WriteLine statements), so you actually see the two values that you are feeding into atan2.
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text, and PictureBoxes for pictures, not drawings.
modified on Friday, June 10, 2011 12:30 PM
|
|
|
|
|
The error is I get a 1 or a zero back from the function calls.
I do not understand where degreestoRadian has gone. I specifically included it.
I agree with the idea of removing commented out code and to format it in a code block.
I am out of time. I cannot solve the problem the way my boss would prefer and so now I am going to say the destination is X units West, or East and Y units North or South of the starting point.
Some day when I am ready I will probably tackle it again, but this problem has become like gambling, I keep thinking I am going to hit the jackpot if I pull the lever one more time, but the trend says its way past time to give up for now.
Been there, done that, forgot why!
|
|
|
|
|
Fixing means messing with it without knowing what I am doing. I certainly do not know what any of these functions do. I'm not a trigonomitrist, I am just some programmer that if given a formula, has a fairly decent chance of translating it to a computer language, but that doesn't mean I know the ins and outs.
Been there, done that, forgot why!
|
|
|
|
|
I had a fortune from a cookie that said something like: "When solving a problem, it's best to know the answer beforehand."
What is the answer you expect?
|
|
|
|
|
Boss was expecting something like San Diego is about 220 degrees (Approximately SW) of Paradise CA. I am afraid the answer is not going to be so simple. Its got to be Starting Heading in the form of ~0 .. 360, Ending heading something like ~0 .. 360 degrees).
Been there, done that, forgot why!
|
|
|
|
|
According to Google maps, San Diego, CA is South East of Paradise, CA
Clickety[^]
The course from the origin is 152 degrees. From magnetic north it's 136 based on this:
Clickety[^]
|
|
|
|
|
Yeah, right, next you'll say Reno is west of Los Angeles.
|
|
|
|
|
This is far from "simple geography compass problem", in fact there are a number of answers and ways to calculate distance/bearing to a point. The earth being roughly sperical means that there are a few ways to calculate a general "route" between two places on the surface.
If you want to deal with this the very simple way its Euclidean distance[^] that you want.
I think this[^] is more what you're after though!
|
|
|
|
|
You can simplify the problem by imagining the earth to be a rectangular array, with the x coordinate going from 0 to 360 and the y coordinate going from -90 to 90.
The two cities' coordinates map to two points on this rectangular array. If the difference between the two cities' longitudes is more than 180, add 360 to the lower longitude, extending the map.
Now you just have to get the angle between the line segment joining these two points, and a horizontal ray starting from the start city, and going to the right.
Many sites have the formula for the angle between two line segments, like http://www.vb-helper.com/howto_find_angles.html[^]
|
|
|
|
|
Hi Garry,
I had some spare time today, so I decided to tackle your geodesic problem. Here is my C# code
(I tend to use C#, the essence is in method Calc(); I trust you can do the same thing in C):
using System;
using System.Text;
namespace LongLat {
class Program {
class City {
public string Name;
public double LongitudeDegrees;
public double LatitudeDegrees;
public double LongitudeRadians;
public double LatitudeRadians;
public City(string name, double longitude, double latitude) {
this.Name=name;
this.LongitudeDegrees=longitude;
this.LatitudeDegrees=latitude;
this.LongitudeRadians=longitude*Math.PI/180.0;
this.LatitudeRadians=latitude*Math.PI/180.0;
}
public override string ToString() {
return Name+"("+LongitudeDegrees+", "+LatitudeDegrees+")";
}
}
static void Main(string[] args) {
City Brussels=new City("Brussels", 4.35, 50.85);
City Antwerp=new City("Antwerp", 4.40, 51.21);
City AntwerpEast=new City("East-of-Antwerp", 5.40, 51.21);
City AntwerpWest=new City("West-of-Antwerp", 3.40, 51.21);
City AntwerpNorth=new City("North-of-Antwerp", 4.40, 52.21);
City AntwerpSouth=new City("South-of-Antwerp", 4.40, 50.21);
City Paris=new City("Paris", 2.34, 48.85);
City Paradise=new City("Paradise", -121.605, 39.743);
City SanDiego=new City("San-Diego", -117.165, 32.723);
City ColoStrings=new City("Colorado-Springs", -104.823, 38.835);
Calc(Antwerp, AntwerpEast);
Calc(Antwerp, AntwerpWest);
Calc(Antwerp, AntwerpNorth);
Calc(Antwerp, AntwerpSouth);
Calc(Brussels, Antwerp);
Calc(Brussels, Paris);
Calc(Paradise, SanDiego);
Calc(Paradise, ColoStrings);
Console.ReadLine();
}
static void log(string s) {
Console.WriteLine(s);
}
static double Calc(City city1, City city2) {
double deltaLong=city2.LongitudeRadians-city1.LongitudeRadians;
double y=Math.Cos(city1.LatitudeRadians)*Math.Sin(city2.LatitudeRadians)-
Math.Sin(city1.LatitudeRadians)*Math.Cos(city2.LatitudeRadians)*Math.Cos(deltaLong);
double x=Math.Sin(deltaLong)*Math.Cos(city2.LatitudeRadians);
double bearing=Math.Atan2(y, x);
bearing=180.0*bearing/Math.PI;
log("Bearing from "+city1+" to "+city2+" = "+bearing.ToString("N2"));
return bearing;
}
}
}
and this is the output:
Bearing from Antwerp(4.4, 51.21) to East-of-Antwerp(5.4, 51.21) = 0.39
Bearing from Antwerp(4.4, 51.21) to West-of-Antwerp(3.4, 51.21) = 179.61
Bearing from Antwerp(4.4, 51.21) to North-of-Antwerp(4.4, 52.21) = 90.00
Bearing from Antwerp(4.4, 51.21) to South-of-Antwerp(4.4, 50.21) = -90.00
Bearing from Brussels(4.35, 50.85) to Antwerp(4.4, 51.21) = 85.03
Bearing from Brussels(4.35, 50.85) to Paris(2.34, 48.85) = -123.72
Bearing from Paradise(-121.605, 39.743) to San-Diego(-117.165, 32.723) = -61.63
Bearing from Paradise(-121.605, 39.743) to Colorado-Springs(-104.823, 38.835) = 1.37
The above Calc() method is basically identical to your original code, so I do not know how you got wrong results in the first place.
Hope this helps.
|
|
|
|
|
Wow that's a nice bit of work!
I got the same results as you, but since Colorado Springs is east and a bit south of Paradise and since east is 90 degrees, I was expecting something like maybe 107 degrees not 1.37. Your answer seems to be in the form of -180 ... +180 and I can't seem to get the results normalized to the range of 0 .. 360 where 0 and 360 are north.
If I take -180 and +180 to be north than I guess 1.37 is roughly south, but Colorado Springs is East not south so I am confused.
I guess what I do not understand is how to arrive at a range of 00 ... 360 as described above.
Been there, done that, forgot why!
|
|
|
|
|
Hi Garry,
no no, the results use zero for East, 90 for North, etc, same as regular two-dimensional geometry.
Check with the AntwerpEast etc. cities I included for testing.
If you want North=0, you should subtract 90 degrees from the end result (then west=90, ...).
If you want North=0 and East=90, you need to invert the sign.
If you want 0...360 rather than -180...180, you should do bearing=(bearing+360)%360 as you have done earlier yourself.
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:30 PM
|
|
|
|
|
Thanks much!
I tried the third option on this result already and the problem I am having is that % will not work with floats, doubles but only with integers I get a compile error when I try it.
x % y will not compile unless the types for x and y are integer only.
So I tried multiplication to get the number rounded up to an integer, and then multiply the 360 by the same factor and then do that math and I am trying to work with that.
I suspect I may have to invert the signs first and then do the third step or I might not get the expected results I need....
0 .. 360 where 0 and 360 are north and 90 is East, and West is 270.
Been there, done that, forgot why!
|
|
|
|
|
Garry Freemyer wrote: x % y will not compile
Sorry, my mistake.
For 0=North, 90=East, 180=South, 270=West, this is what I would add after bearing=180.0*bearing/Math.PI; :
bearing=90.0-bearing;
if (bearing<0.0) bearing+=360.0;
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:31 PM
|
|
|
|
|
Oh this is looking promising here. I got the correct bearings out of this when I asked about paradise to san diego and from paradise to colorado springs ... this is looking good!!
I'll do more testing and let you know but it's looking good so far!
Been there, done that, forgot why!
|
|
|
|
|
Another idea to get North=0, East=90 is even simpler: just swap the formulae for x and y!
You still need the negative-test if you want results in [0, 360) only.
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:31 PM
|
|
|
|
|
Hmm Interesting, so just swap the arguments.
Well, I might try it later, I am still afraid of this monster, and am inclined to think that if it's working don't be too fast to try to fix it right after I have gone past the 12th round and my entire being is still reeling from it all.
If your solution works, you might consider posting this as an example on Code Project, in C# on how to find this ....
After searching for three weeks for a complete solution, I can tell you that there are dozens of others who like me could not find a solution ... Yours is the only complete solution with code that addresses the question asked. I found dozens of replies to folks who asked my question, but got the answer in the form of bearings where east was zero ... Something that should have been mentioned or they did not finish answering the question or left functions out of the code.
Been there, done that, forgot why!
|
|
|
|
|
Gee, all I did was take the formula you were using, and write a C# test app, making sure to include the greater Antwerp region to make sure the results made sense, and they did to me.
I had to look on the map to find and see relative position of Paradise and San-Diego, and I accepted the outcome. But then for me zero degrees has always pointed to the right, as with a protractor[^] (another word I learned todsy).
If you want to see an article on the subject, I suggest we work together; maybe you could do text and images, I could take care of coding and downloads (I could proofread too, finding most typo's, but I trust you would be better as far as vocabulary, grammar and style go).
|
|
|
|
|
Well, yeah I do have a different meaning to fix something.
I would be willing to do the wording, and put in the final code.
All articles I saw about converting to the range I needed said to use a form of a modulus operator, but NONE of them took into account that the modulus operator required integer operators.
Reminds me of years ago, I wanted to write a screen saver but all the code out there fell on it's face, and then I wanted to write one that worked on multiple monitors. The existing examples were not compilable or incomplete.
So I wrote one in C# under Visual Studio that worked with 2003, or 2005 with no mods. Folks loved it, as far as I know it's the best out there. You can replace my functions with your own graphics functions and plug it right in and go.
I spent weeks trying to iron out a problem where the screen saver would just mysteriously lock up. I finally got it fixed and then a windows update caused it to come back again.
I was not willing to go revisit the thing and go through weeks of hell, where the thing might crash immediately or crash in 2 days of constant running.
If you are curious it should be among my articles. I put it on Code Project a few years ago.
Been there, done that, forgot why!
|
|
|
|
|
Hi Garry,
I did send you an email message several days ago trying to get in touch with you directly, but since the CP email is not working reliably again, I now suggest you create a geodesic problem solvers group, which I could join, so we can collaborate on the article.
Groups are a new feature on CP, I don't know exactly how it works, but we'll see...
Regards,
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:32 PM
|
|
|
|
|
Garry Freemyer wrote: don't be too fast to try to fix it
"to fix" seems to have a special meaning with you
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:32 PM
|
|
|
|
|
I am trying to to do a similar thing in C# (don't worry I am also a C++ developer).
Did you managed to work this out.
I actually need to plot a point on a line given the start point and distance. I also happen to know the end point.
Therefore I should be able to calculate the bearing between to points, then use this bearing, distance and start point to work out the lat long.
However I am stuck working out the bearing!
Can you help?
|
|
|
|
|
Please can some one help me how to split a single mp3 file into 2 or 3 mp3 files??
Thanx
Noor
|
|
|
|
|