-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathXNLineData.m
106 lines (78 loc) · 2.42 KB
/
XNLineData.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
//
// XNLineData.m
// XNMaths
//
// Created by Нат Гаджибалаев on 23.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#pragma mark -
#pragma mark Imports
#import "XNFLoatRange.h"
#import "XNLineData.h"
#import "XNFunction.h"
#pragma mark -
#pragma mark LineData class implementation
@implementation XNLineData
@synthesize xData, yData;
@synthesize xRange, yRange;
@synthesize quality, pointsCount;
#pragma mark -
#pragma mark Class init methods
+ (XNLineData *) lineDataWithFunction: (XNFunction*)aFunction inRange: (XNFloatRange*)range withQuality: (NSUInteger) lineQuality
{
return [[XNLineData alloc] initWithFunction:aFunction inRange:range withQuality:lineQuality ];
}
+ (XNLineData *) lineDataWithXData: (CGFloat*)x yData:(CGFloat*)y pointsCount: (NSUInteger) count;
{
return [[XNLineData alloc] initWithXData: x yData: y pointsCount: count ];
}
#pragma mark -
#pragma mark Instance init methods
- (XNLineData *) initWithXData: (CGFloat*)x yData:(CGFloat*)y pointsCount: (NSUInteger) count
{
self = [super init];
xData = x;
yData = y;
pointsCount = count;
xRange = [XNFloatRange rangeWithCArray:xData withCapacity:pointsCount ];
yRange = [XNFloatRange rangeWithCArray:yData withCapacity:pointsCount ];
quality = (NSUInteger)(xRange.length / pointsCount);
return self;
}
- (XNLineData *)initWithFunction: (XNFunction*)aFunction inRange: (XNFloatRange*)newRange withQuality: (NSUInteger) lineQuality
{
self = [super init];
// set new line quality;
quality = lineQuality;
xRange = newRange;
// calculate points count
CGFloat strechLength = [xRange length];
NSUInteger unitsCount = (NSUInteger) strechLength;
pointsCount = quality * unitsCount;
// init data containers
xData = calloc(pointsCount, sizeof(CGFloat));
yData = calloc(pointsCount, sizeof(CGFloat));
// set up default y range.
yRange = [XNFloatRange rangeWithMin:[aFunction valueWithFloat: xRange.min] max:[aFunction valueWithFloat: xRange.min]];
// calculate data and set min/max values
for(NSInteger i = 0; i < pointsCount; i++){
xData[i] = xRange.min + (strechLength / pointsCount)*i;
yData[i] = [aFunction valueWithFloat: xData[i]];
if( yData[i] < yRange.min ){
yRange.min = yData[i];
}
if( yData[i] > yRange.max ){
yRange.max = yData[i];
}
}
return self;
}
- (void) dealloc
{
free(xData);
free(yData);
[xRange release];
[yRange release];
[super dealloc];
}
@end