ScreenProjection.m
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// ScreenProjection.m
// Images
//
// Created by Joseph Gentle on 28/08/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "ScreenProjection.h"
@implementation ScreenProjection
@synthesize scale, topLeft;
-(id) initWithBounds: (CGRect) _bounds
{
if (![super init])
return nil;
bounds = _bounds;
topLeft.x = 0;
topLeft.y = 0;
scale = 1;
return self;
}
-(void) moveToMercator: (MercatorPoint) point
{
topLeft = point;
topLeft.x -= bounds.size.width * scale / 2;
topLeft.y -= bounds.size.height * scale / 2;
}
-(void) moveToLatLong: (CLLocationCoordinate2D) point;
{
[self moveToMercator:[Mercator toMercator:point]];
}
-(void) moveBy: (CGSize) delta
{
topLeft.x -= delta.width * scale;
topLeft.y += delta.height * scale;
}
-(void) zoomByFactor: (float) zoomFactor Near:(CGPoint) center
{
// NSLog(@"zoomBy: %f", zoomFactor);
topLeft.x += center.x * scale;
topLeft.y += (bounds.size.height - center.y) * scale;
scale /= zoomFactor;
topLeft.x -= center.x * scale;
topLeft.y -= (bounds.size.height - center.y) * scale;
}
- (void)zoomBy: (float) factor
{
scale *= factor;
}
-(CGPoint) projectMercatorPoint: (MercatorPoint) mercator
{
CGPoint point;
point.x = (mercator.x - topLeft.x) / scale;
point.y = -(mercator.y - topLeft.y) / scale;
return point;
}
-(CGRect) projectMercatorRect: (MercatorRect) mercator
{
CGRect rect;
rect.origin = [self projectMercatorPoint: mercator.origin];
mercator.size.width = rect.size.width / scale;
mercator.size.height = rect.size.height / scale;
return rect;
}
-(MercatorPoint) projectInversePoint: (CGPoint) point
{
MercatorPoint mercator;
mercator.x = (scale * point.x) + topLeft.x;
mercator.y = -(scale * point.y) + topLeft.y;
return mercator;
}
-(MercatorRect) projectInverseRect: (CGRect) rect
{
MercatorRect mercator;
mercator.origin = [self projectInversePoint: rect.origin];
mercator.size.width = rect.size.width * scale;
mercator.size.height = rect.size.height * scale;
return mercator;
}
-(CGRect) screenBounds
{
return bounds;
}
-(MercatorRect) mercatorBounds
{
MercatorRect rect;
rect.origin = topLeft;
rect.size.width = bounds.size.width * scale;
rect.size.height = bounds.size.height * scale;
return rect;
}
@end