/MapView/Map/RMFoundation.c

http://github.com/route-me/route-me · C · 125 lines · 75 code · 24 blank · 26 comment · 14 complexity · db8897965c7f006415215070577ffbbc MD5 · raw file

  1. //
  2. // RMFoundation.c
  3. //
  4. // Copyright (c) 2008-2009, Route-Me Contributors
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. // POSSIBILITY OF SUCH DAMAGE.
  27. #import "RMFoundation.h"
  28. bool RMProjectedPointEqualToProjectedPoint(RMProjectedPoint point1, RMProjectedPoint point2)
  29. {
  30. return point1.easting == point2.easting && point2.northing == point2.northing;
  31. }
  32. bool RMProjectedRectInterectsProjectedRect(RMProjectedRect rect1, RMProjectedRect rect2)
  33. {
  34. double minEasting1 = rect1.origin.easting;
  35. double maxEasting1 = rect1.origin.easting + rect1.size.width;
  36. double minNorthing1 = rect1.origin.northing;
  37. double maxNorthing1 = rect1.origin.northing + rect1.size.height;
  38. double minEasting2 = rect2.origin.easting;
  39. double maxEasting2 = rect2.origin.easting + rect2.size.width;
  40. double minNorthing2 = rect2.origin.northing;
  41. double maxNorthing2 = rect2.origin.northing + rect2.size.height;
  42. return ((minEasting1 <= minEasting2 && minEasting2 <= maxEasting1) || (minEasting2 <= minEasting1 && minEasting1 <= maxEasting2))
  43. && ((minNorthing1 <= minNorthing2 && minNorthing2 <= maxNorthing1) || (minNorthing2 <= minNorthing1 && minNorthing1 <= maxNorthing2));
  44. }
  45. bool RMProjectedSizeEqualToProjectedSize(RMProjectedSize size1, RMProjectedSize size2) {
  46. return ((size1.width == size2.width) && (size1.height == size2.height));
  47. }
  48. bool RMProjectedRectEqualToProjectedRect(RMProjectedRect rect1, RMProjectedRect rect2) {
  49. return (RMProjectedPointEqualToProjectedPoint(rect1.origin, rect2.origin) && RMProjectedSizeEqualToProjectedSize(rect1.size, rect2.size));
  50. }
  51. RMProjectedPoint RMScaleProjectedPointAboutPoint(RMProjectedPoint point, float factor, RMProjectedPoint pivot)
  52. {
  53. point.easting = (point.easting - pivot.easting) * factor + pivot.easting;
  54. point.northing = (point.northing - pivot.northing) * factor + pivot.northing;
  55. return point;
  56. }
  57. RMProjectedRect RMScaleProjectedRectAboutPoint (RMProjectedRect rect, float factor, RMProjectedPoint pivot)
  58. {
  59. rect.origin = RMScaleProjectedPointAboutPoint(rect.origin, factor, pivot);
  60. rect.size.width *= factor;
  61. rect.size.height *= factor;
  62. return rect;
  63. }
  64. RMProjectedPoint RMTranslateProjectedPointBy(RMProjectedPoint point, RMProjectedSize delta)
  65. {
  66. point.easting += delta.width;
  67. point.northing += delta.height;
  68. return point;
  69. }
  70. RMProjectedRect RMTranslateProjectedRectBy(RMProjectedRect rect, RMProjectedSize delta)
  71. {
  72. rect.origin = RMTranslateProjectedPointBy(rect.origin, delta);
  73. return rect;
  74. }
  75. RMProjectedPoint RMMakeProjectedPoint (double easting, double northing)
  76. {
  77. RMProjectedPoint point = {
  78. easting, northing
  79. };
  80. return point;
  81. }
  82. RMProjectedSize RMMakeProjectedSize(double width, double height) {
  83. RMProjectedSize size = {
  84. width, height
  85. };
  86. return size;
  87. }
  88. RMProjectedRect RMMakeProjectedRect (double easting, double northing, double width, double height)
  89. {
  90. RMProjectedRect rect = {
  91. {easting, northing},
  92. {width, height}
  93. };
  94. return rect;
  95. }
  96. double RMProjectedRectGetMidEasting(RMProjectedRect rect) {
  97. return (rect.origin.easting + rect.size.width / 2);
  98. }
  99. double RMProjectedRectGetMidNorthing(RMProjectedRect rect){
  100. return (rect.origin.northing + rect.size.height / 2);
  101. }