English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Analysis of the method to calculate the driving direction of a car in C#

Content

1This article analyzes the method of calculating the car's travel direction in C#. Share it with everyone for reference, as follows:2.  Scenario: Given the car's travel process,1a GPS coordinate point A (n1,e

2),B(e),calculate the direction it travels.2+c2-b2)/2. The analysis: As shown in the figure above, knowing two points A, B, we can assume a point C, so that the three points form a right triangle. Now it is easy to calculate the GPS coordinates of points A, B, and C, and the lengths of the opposite sides a, b, and c of the three angles. According to the cosine rule, CosB = (a

3.C# implementation code.

/// <summary>
///Calculate the distance between two GPS coordinates.
/// </summary>
/// <param name="n1">the latitude coordinate of the first point</param>
/// <param name="e1">the longitude coordinate of the first point</param>
/// <param name="n2">the latitude coordinate of the second point</param>
/// <param name="e2">the longitude coordinate of the second point</param>
/// <returns></returns>
public static double Distance(double n1, double e1, double n2, double e2)
{
  double jl_jd = 102834.74258026089786013677476285;
  double jl_wd = 111712.69150641055729984301412873;
  double b = Math.Abs((e1 - e2) * jl_jd);
  double a = Math.Abs((n1 - n2) * jl_wd);
  return Math.Sqrt((a * a + b * b));
}
/// <summary>
/// Given two GPS points of a car's travel, calculate the direction of the car's travel.
/// </summary>
/// <param name="n1">the latitude of the first GPS point</param>
/// <param name="e1">the longitude of the first GPS point</param>
/// <param name="n2">the latitude of the second GPS point</param>
/// <param name="e2">the longitude of the second GPS point</param>
/// <returns></returns>
public static double GetBusDirection( double n1,double e1, double n2, double e2)
{
  double e3 = 0;
  double n3 = 0;
  e3 = e1 + 0.005;
  n3 = n1;
  double a = 0;
  double b = 0;
  double c = 0;
  a = Distance(e1, n1, e3, n3;
  b = Distance(e3, n3, e2, n2;
  c = Distance(e1, n1, e2, n2;
  double cosB = 0;
  if ((a * c) != 0)
  {
 cosB = (a * a + c * c - b * b) / (2 * a * c);
  }
  double B = Math.Acos(cosB) * 180 / Math.PI;
  if(n2<n1)
  {
 B=180+(180-B);
  }
  return B;
}

Readers who are interested in more C# related content can check the special topics on this site: 'Summary of C# String Operation Techniques', 'Summary of C# Array Operation Techniques', 'Summary of XML File Operation Techniques in C#', 'Tutorial of Common Controls Usage in C#', 'Summary of Thread Usage Techniques in C# Program Design', 'Summary of Excel Operation Techniques in C#', 'Summary of WinForm Control Usage', 'C# Data Structures and Algorithms Tutorial', and 'C# Object-Oriented Program Design Tutorial'.

I hope this article will be helpful to everyone's C# program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) for complaints, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like