forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNLinearSpline.m
93 lines (73 loc) · 2.03 KB
/
XNLinearSpline.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
//
// XNLinearSpline.m
// XNMaths
//
// XNLinearSplineElements functions (builders)
//
// XNLinearSpline implementation
// Interpolates a 2D function in some points array with a linear spline.
//
//
// Created by Нат Гаджибалаев on 20.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#import "XNLinearSpline.h"
#import "XNLineData.h"
#pragma mark -
#pragma mark Spline interploation element data
//
// Make spline element.
XNLinearSplineElement XNMakeLinearSplineElement(CGFloat a, CGFloat b)
{
XNLinearSplineElement element;
element.a = a;
element.b = b;
return element;
};
#pragma mark -
#pragma mark Spline class implementation.
@implementation XNLinearSpline
+ (XNLinearSpline *) splineWithPoints: (NSArray *)aPoints
{
return [[XNLinearSpline alloc] initWithPoints: aPoints];
}
- (XNLinearSpline *) initWithPoints: (NSArray *)aPoints
{
// initialize self reference.
self = [super init];
// init approx points
approximationPoints = [aPoints copy];
return self;
}
#pragma mark -
#pragma mark Value getters
- (float) valueWithFloat: (CGFloat) a_X
{
for(NSInteger i = 1; i < approximationPoints.count; i++){
NSPoint startPoint = [[approximationPoints objectAtIndex:i-1] pointValue];
NSPoint endPoint = [[approximationPoints objectAtIndex:i] pointValue];
if( startPoint.x < a_X && endPoint.x > a_X ){
return interpolationElements[i].a * a_X + interpolationElements[i].b;
}
}
return 0;
}
- (XNLineData*) lineData
{
CGFloat* x = calloc(approximationPoints.count, sizeof(CGFloat));
CGFloat* y = calloc(approximationPoints.count, sizeof(CGFloat));
for( NSUInteger i = 0; i < approximationPoints.count; i++ ){
x[i] = [[approximationPoints objectAtIndex:i] pointValue].x;
y[i] = [[approximationPoints objectAtIndex:i] pointValue].y;
}
return [XNLineData lineDataWithXData:x yData:y pointsCount:approximationPoints.count];
}
#pragma mark -
#pragma mark Dealloc
- (void) dealloc
{
free(interpolationElements);
[approximationPoints release];
[super dealloc];
}
@end