PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 28#import "RMFoundation.h"
 29
 30bool RMProjectedPointEqualToProjectedPoint(RMProjectedPoint point1, RMProjectedPoint point2)
 31{
 32	return point1.easting == point2.easting && point2.northing == point2.northing;
 33}
 34
 35bool RMProjectedRectInterectsProjectedRect(RMProjectedRect rect1, RMProjectedRect rect2)
 36{
 37	double minEasting1 = rect1.origin.easting;
 38	double maxEasting1 = rect1.origin.easting + rect1.size.width;
 39	double minNorthing1 = rect1.origin.northing;
 40	double maxNorthing1 = rect1.origin.northing + rect1.size.height;
 41
 42	double minEasting2 = rect2.origin.easting;
 43	double maxEasting2 = rect2.origin.easting + rect2.size.width;
 44	double minNorthing2 = rect2.origin.northing;
 45	double maxNorthing2 = rect2.origin.northing + rect2.size.height;
 46
 47	return ((minEasting1 <= minEasting2 && minEasting2 <= maxEasting1) || (minEasting2 <= minEasting1 && minEasting1 <= maxEasting2))
 48		&& ((minNorthing1 <= minNorthing2 && minNorthing2 <= maxNorthing1) || (minNorthing2 <= minNorthing1 && minNorthing1 <= maxNorthing2));
 49	
 50}
 51
 52bool RMProjectedSizeEqualToProjectedSize(RMProjectedSize size1, RMProjectedSize size2) {
 53    return ((size1.width == size2.width) && (size1.height == size2.height));
 54}
 55
 56bool RMProjectedRectEqualToProjectedRect(RMProjectedRect rect1, RMProjectedRect rect2) {
 57    return (RMProjectedPointEqualToProjectedPoint(rect1.origin, rect2.origin) && RMProjectedSizeEqualToProjectedSize(rect1.size, rect2.size));
 58}
 59
 60RMProjectedPoint RMScaleProjectedPointAboutPoint(RMProjectedPoint point, float factor, RMProjectedPoint pivot)
 61{
 62	point.easting = (point.easting - pivot.easting) * factor + pivot.easting;
 63	point.northing = (point.northing - pivot.northing) * factor + pivot.northing;
 64	
 65	return point;
 66}
 67
 68RMProjectedRect  RMScaleProjectedRectAboutPoint (RMProjectedRect rect,   float factor, RMProjectedPoint pivot)
 69{
 70	rect.origin = RMScaleProjectedPointAboutPoint(rect.origin, factor, pivot);
 71	rect.size.width *= factor;
 72	rect.size.height *= factor;
 73	
 74	return rect;
 75}
 76
 77RMProjectedPoint RMTranslateProjectedPointBy(RMProjectedPoint point, RMProjectedSize delta)
 78{
 79	point.easting += delta.width;
 80	point.northing += delta.height;
 81	return point;
 82}
 83
 84RMProjectedRect  RMTranslateProjectedRectBy(RMProjectedRect rect,   RMProjectedSize delta)
 85{
 86	rect.origin = RMTranslateProjectedPointBy(rect.origin, delta);
 87	return rect;
 88}
 89
 90RMProjectedPoint  RMMakeProjectedPoint (double easting, double northing)
 91{
 92	RMProjectedPoint point = {
 93		easting, northing
 94	};
 95	
 96	return point;
 97}
 98
 99RMProjectedSize RMMakeProjectedSize(double width, double height) {
100    RMProjectedSize size = {
101        width, height
102    };
103    return size;
104}
105
106RMProjectedRect  RMMakeProjectedRect (double easting, double northing, double width, double height)
107{
108	RMProjectedRect rect = {
109		{easting, northing},
110		{width, height}
111	};
112	
113	return rect;
114}
115
116double RMProjectedRectGetMidEasting(RMProjectedRect rect) {
117    return (rect.origin.easting + rect.size.width / 2);
118}
119
120double RMProjectedRectGetMidNorthing(RMProjectedRect rect){
121    return (rect.origin.northing + rect.size.height / 2);
122}
123
124
125