Click here to Skip to main content
15,909,822 members
Home / Discussions / C#
   

C#

 
GeneralRe: I keep getting zero as the angle no matter what. Pin
Garry Freemyer13-Jan-09 14:06
Garry Freemyer13-Jan-09 14:06 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
PIEBALDconsult12-Jan-09 16:12
mvePIEBALDconsult12-Jan-09 16:12 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer12-Jan-09 16:22
Garry Freemyer12-Jan-09 16:22 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Jason Lepack (LeppyR64)12-Jan-09 16:47
Jason Lepack (LeppyR64)12-Jan-09 16:47 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
PIEBALDconsult12-Jan-09 19:49
mvePIEBALDconsult12-Jan-09 19:49 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
J4amieC12-Jan-09 22:19
J4amieC12-Jan-09 22:19 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
Alan Balkany13-Jan-09 4:28
Alan Balkany13-Jan-09 4:28 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
Luc Pattyn14-Jan-09 3:24
sitebuilderLuc Pattyn14-Jan-09 3:24 
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;
			// constructor takes long/lat in degrees and stores them in radians
			// warning: we use fractions of degrees, not minutes and seconds
			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;
			}
			// show the info on this city
			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);
		}

		// takes two cities and calculates bearing from city1 to city2 in degrees
		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;
			// show 2 decimals
			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.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

I use ListBoxes for line-oriented text, and PictureBoxes for pictures, not drawings.


GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 9:43
Garry Freemyer14-Jan-09 9:43 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn14-Jan-09 9:51
sitebuilderLuc Pattyn14-Jan-09 9:51 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 10:28
Garry Freemyer14-Jan-09 10:28 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn14-Jan-09 10:40
sitebuilderLuc Pattyn14-Jan-09 10:40 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 11:05
Garry Freemyer14-Jan-09 11:05 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn14-Jan-09 11:03
sitebuilderLuc Pattyn14-Jan-09 11:03 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 11:16
Garry Freemyer14-Jan-09 11:16 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
Luc Pattyn14-Jan-09 11:30
sitebuilderLuc Pattyn14-Jan-09 11:30 
GeneralRe: Beaten and defeated by this simple geography compass problem. Pin
Garry Freemyer14-Jan-09 11:44
Garry Freemyer14-Jan-09 11:44 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn19-Jan-09 11:00
sitebuilderLuc Pattyn19-Jan-09 11:00 
AnswerRe: Beaten and defeated by this simple geography compass problem. [modified] Pin
Luc Pattyn14-Jan-09 11:34
sitebuilderLuc Pattyn14-Jan-09 11:34 
AnswerRe: Beaten and defeated by this simple geography compass problem. Pin
RickNash27-Apr-09 4:27
RickNash27-Apr-09 4:27 
QuestionSplit Mp3 File Pin
Muhammad Noor12-Jan-09 15:12
Muhammad Noor12-Jan-09 15:12 
AnswerRe: Split Mp3 File Pin
Dragonfly_Lee12-Jan-09 17:46
Dragonfly_Lee12-Jan-09 17:46 
AnswerRe: Split Mp3 File Pin
Guffa12-Jan-09 20:11
Guffa12-Jan-09 20:11 
QuestionC# Socket diconnect Help needed Pin
alb1081112-Jan-09 14:19
alb1081112-Jan-09 14:19 
Questionhow can made clinic project in c#.net? Pin
mohammedali200612-Jan-09 13:17
mohammedali200612-Jan-09 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.