-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCGraph.m
108 lines (78 loc) · 1.74 KB
/
SCGraph.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
//
// SCGraph.m
// Alarm-Clock
//
// Created by Robert Palmer on 31.01.10.
// Copyright 2010 Robert. All rights reserved.
//
#import "SCGraph.h"
#import "SC2DScale.h"
@interface SCGraph ()
- (BOOL)isValidPoint: (NSPoint) point;
- (void)removePastPoints;
@end
@implementation SCGraph
@synthesize scaler;
@synthesize dirty;
- (id) init {
self = [super init];
if (self != nil) {
points = [[NSMutableArray alloc] init];
dirty = NO;
}
return self;
}
- (void) dealloc {
[scaler release];
[points release];
[super dealloc];
}
- (void)addPoint: (NSPoint)point {
[points addObject:[NSValue valueWithPoint: point]];
self.dirty = YES;
}
- (void)draw: (CGContextRef) context {
if ([points count] == 0) {
return;
}
[self removePastPoints];
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
CGContextBeginPath(context);
NSPoint point = [[points objectAtIndex:0] pointValue];
point = [scaler scaleValue:point];
CGContextMoveToPoint(context, point.x, point.y);
for (int i = 1; i < [points count]; i++) {
NSPoint point = [[points objectAtIndex:i] pointValue];
if ([self isValidPoint: point]) {
point = [scaler scaleValue:point];
CGContextAddLineToPoint(context, point.x, point.y);
}
}
CGContextStrokePath(context);
self.dirty = NO;
}
- (float)width {
return [[points lastObject] pointValue].x;
}
#pragma mark -
#pragma mark Private Methods
- (BOOL)isValidPoint: (NSPoint) point {
if (point.x < scaler.fromRect.origin.x) {
return NO;
}
return YES;
}
- (void)removePastPoints
{
if ([points count] < 3) {
return;
}
for (int i = 1; i < [points count]; i++) {
if (![self isValidPoint: [[points objectAtIndex:i] pointValue]]) {
[points removeObjectAtIndex:0];
} else {
return;
}
}
}
@end