You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.7KB

  1. /*
  2. Display scaled distances in 'local' units.
  3. Copyright (C) 2006 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. static int units = units_statute;
  18. int
  19. fmt_setunits(fmt_units u)
  20. {
  21. switch (u) {
  22. case units_statute:
  23. case units_metric:
  24. case units_nautical:
  25. case units_aviation:
  26. units = u;
  27. return 0;
  28. default:
  29. return 1;
  30. }
  31. }
  32. double
  33. fmt_distance(const double distance_meters, const char** tag)
  34. {
  35. double d;
  36. switch (units) {
  37. case units_statute:
  38. d = METERS_TO_FEET(distance_meters);
  39. if (d < 5280) {
  40. *tag = "ft";
  41. } else {
  42. d = METERS_TO_MILES(distance_meters);
  43. *tag = "mi";
  44. }
  45. break;
  46. case units_nautical:
  47. case units_aviation:
  48. d = METERS_TO_NMILES(distance_meters);
  49. *tag = "NM";
  50. break;
  51. case units_metric:
  52. d = distance_meters;
  53. if (d < 1000) {
  54. *tag = "meters";
  55. } else {
  56. d = d / (double) 1000.0;
  57. *tag = "km";
  58. }
  59. break;
  60. default:
  61. fatal("not done yet");
  62. break;
  63. }
  64. return d;
  65. }
  66. double
  67. fmt_altitude(const double distance_meters, const char** tag)
  68. {
  69. double d;
  70. switch (units) {
  71. case units_statute:
  72. case units_aviation:
  73. d = METERS_TO_FEET(distance_meters);
  74. *tag = "ft";
  75. break;
  76. case units_nautical:
  77. d = METERS_TO_NMILES(distance_meters);
  78. *tag = "NM";
  79. break;
  80. case units_metric:
  81. d = distance_meters;
  82. *tag = "meters";
  83. break;
  84. default:
  85. fatal("not done yet");
  86. break;
  87. }
  88. return d;
  89. }
  90. double
  91. fmt_speed(const double distance_meters_sec, const char** tag)
  92. {
  93. double d;
  94. switch (units) {
  95. case units_statute:
  96. d = METERS_TO_MILES(distance_meters_sec) * SECONDS_PER_HOUR ;
  97. *tag = "mph";
  98. break;
  99. case units_nautical:
  100. case units_aviation:
  101. d = METERS_TO_NMILES(distance_meters_sec) * SECONDS_PER_HOUR ;
  102. *tag = "knts";
  103. break;
  104. case units_metric:
  105. d = distance_meters_sec * SECONDS_PER_HOUR;
  106. *tag = "meters/hour";
  107. if (d > 1000.0) {
  108. d /= 1000.0;
  109. *tag = "km/hour";
  110. }
  111. break;
  112. default:
  113. fatal("not done yet");
  114. }
  115. return d;
  116. }