-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxesDrawer.m
executable file
·163 lines (138 loc) · 5.73 KB
/
AxesDrawer.m
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// AxesDrawer.m
//
// Created for Stanford University CS193p Fall 2010.
//
#import "AxesDrawer.h"
@implementation AxesDrawer
#define ANCHOR_CENTER 0
#define ANCHOR_TOP 1
#define ANCHOR_LEFT 2
#define ANCHOR_BOTTOM 3
#define ANCHOR_RIGHT 4
#define HASH_MARK_FONT_SIZE 12.0
#define HORIZONTAL_TEXT_MARGIN 6
#define VERTICAL_TEXT_MARGIN 3
+ (void)drawString:(NSString *)text atPoint:(CGPoint)location withAnchor:(int)anchor
{
if ([text length])
{
UIFont *font = [UIFont systemFontOfSize:HASH_MARK_FONT_SIZE];
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = location.x - textRect.size.width / 2;
textRect.origin.y = location.y - textRect.size.height / 2;
switch (anchor) {
case ANCHOR_TOP: textRect.origin.y += textRect.size.height / 2 + VERTICAL_TEXT_MARGIN; break;
case ANCHOR_LEFT: textRect.origin.x += textRect.size.width / 2+ HORIZONTAL_TEXT_MARGIN; break;
case ANCHOR_BOTTOM: textRect.origin.y -= textRect.size.height / 2 + VERTICAL_TEXT_MARGIN; break;
case ANCHOR_RIGHT: textRect.origin.x -= textRect.size.width / 2+ HORIZONTAL_TEXT_MARGIN; break;
}
[text drawInRect:textRect withFont:font];
}
}
#define HASH_MARK_SIZE 3
#define MIN_PIXELS_PER_HASHMARK 25
+ (void)drawHashMarksInRect:(CGRect)bounds originAtPoint:(CGPoint)axisOrigin scale:(CGFloat)pointsPerUnit
{
if (!pointsPerUnit) return;
if (((axisOrigin.x < bounds.origin.x) || (axisOrigin.x > bounds.origin.x+bounds.size.width)) &&
((axisOrigin.y < bounds.origin.y) || (axisOrigin.y > bounds.origin.y+bounds.size.height))) {
return;
}
int unitsPerHashmark = MIN_PIXELS_PER_HASHMARK * 2 / pointsPerUnit;
if (!unitsPerHashmark) unitsPerHashmark = 1;
CGFloat pixelsPerHashmark = pointsPerUnit * unitsPerHashmark;
BOOL boundsContainsOrigin = CGRectContainsPoint(bounds, axisOrigin);
if (boundsContainsOrigin) {
if ((axisOrigin.x - pixelsPerHashmark < bounds.origin.x) &&
(axisOrigin.x + pixelsPerHashmark > bounds.origin.x + bounds.size.width) &&
(axisOrigin.y - pixelsPerHashmark < bounds.origin.y) &&
(axisOrigin.y + pixelsPerHashmark > bounds.origin.y + bounds.size.height)) {
return;
}
} else {
if ((axisOrigin.y >= bounds.origin.y) &&
(axisOrigin.y <= bounds.origin.y+bounds.size.height) &&
(bounds.size.width <= pixelsPerHashmark)) {
return;
}
if ((axisOrigin.x >= bounds.origin.x) &&
(axisOrigin.x <= bounds.origin.x+bounds.size.width) &&
(bounds.size.height <= pixelsPerHashmark)) {
return;
}
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
int started = NO;
int stillGoing = YES;
for (int offset = unitsPerHashmark; !started || stillGoing; offset += unitsPerHashmark)
{
NSString *positiveLabel = nil;
NSString *negativeLabel = nil;
BOOL drew = NO;
CGFloat scaledOffset = floor(offset * pointsPerUnit);
CGPoint hashMarkPoint;
hashMarkPoint.x = axisOrigin.x+scaledOffset;
hashMarkPoint.y = axisOrigin.y;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x, hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context, hashMarkPoint.x, hashMarkPoint.y+HASH_MARK_SIZE);
if (!positiveLabel) positiveLabel = [NSString stringWithFormat:@"%d", offset];
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
drew = YES;
}
hashMarkPoint.x = axisOrigin.x-scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x, hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context, hashMarkPoint.x, hashMarkPoint.y+HASH_MARK_SIZE);
if (boundsContainsOrigin) negativeLabel = positiveLabel;
if (!negativeLabel) negativeLabel = [NSString stringWithFormat:@"%d", (boundsContainsOrigin ? offset : -offset)];
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
drew = YES;
}
hashMarkPoint.x = axisOrigin.x;
hashMarkPoint.y = axisOrigin.y-scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x-HASH_MARK_SIZE, hashMarkPoint.y);
CGContextAddLineToPoint(context, hashMarkPoint.x+HASH_MARK_SIZE, hashMarkPoint.y);
if (!positiveLabel) {
if (boundsContainsOrigin) positiveLabel = negativeLabel;
if (!positiveLabel) positiveLabel = [NSString stringWithFormat:@"%d", offset];
}
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
drew = YES;
}
hashMarkPoint.y = axisOrigin.y+scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x-HASH_MARK_SIZE, hashMarkPoint.y);
CGContextAddLineToPoint(context, hashMarkPoint.x+HASH_MARK_SIZE, hashMarkPoint.y);
if (!negativeLabel) {
if (boundsContainsOrigin) negativeLabel = positiveLabel;
if (!negativeLabel) negativeLabel = [NSString stringWithFormat:@"%d", (boundsContainsOrigin ? offset : -offset)];
}
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
drew = YES;
}
positiveLabel = nil;
negativeLabel = nil;
if (drew) started = YES;
stillGoing = drew;
}
CGContextStrokePath(context);
}
+ (void)drawAxesInRect:(CGRect)bounds originAtPoint:(CGPoint)axisOrigin scale:(CGFloat)pointsPerUnit
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, bounds.origin.x, axisOrigin.y);
CGContextAddLineToPoint(context, bounds.origin.x+bounds.size.width, axisOrigin.y);
CGContextMoveToPoint(context, axisOrigin.x, bounds.origin.y);
CGContextAddLineToPoint(context, axisOrigin.x, bounds.origin.y+bounds.size.height);
CGContextStrokePath(context);
[self drawHashMarksInRect:bounds originAtPoint:axisOrigin scale:pointsPerUnit];
UIGraphicsPopContext();
}
@end