forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNFunctionOf2D.m
81 lines (64 loc) · 2.04 KB
/
XNFunctionOf2D.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
//
// XNFunctionOf2D.m
// XNMaths
//
// Created by Нат Гаджибалаев on 01.12.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#pragma mark -
#pragma mark Imports
#import "XNFunctionOf2D.h"
#import "GCMathParser.h"
#pragma mark -
#pragma mark XNFunctionOf2D class implementation
@implementation XNFunctionOf2D
@synthesize expression, firstArgumentName, secondArgumentName;
#pragma mark -
#pragma mark Class initialization
+ (XNFunctionOf2D*) functionWithExpression: (NSString*)aExpression
{
return [[[XNFunctionOf2D alloc] initWithExpression:aExpression] autorelease];
}
+ (XNFunctionOf2D*) functionWithExpression: (NSString*)aExpression firstArgument: (NSString*) aFirstArgumentName secondArgument: (NSString*) aSecondArgumentName
{
return [[[XNFunctionOf2D alloc] initWithExpression:aExpression firstArgument: aFirstArgumentName secondArgument: aSecondArgumentName] autorelease];
}
#pragma mark -
#pragma mark Instance inititalization
- (XNFunctionOf2D*) initWithExpression: (NSString*)aExpression
{
self = [super init];
parser = [[GCMathParser parser] retain];
expression = [aExpression copy];
// default argument names
firstArgumentName = @"x";
secondArgumentName = @"y";
return self;
}
- (XNFunctionOf2D*) initWithExpression: (NSString*)aExpression firstArgument: (NSString*) aFirstArgumentName secondArgument: (NSString*) aSecondArgumentName
{
self = [super init];
parser = [[GCMathParser parser] retain];
expression = [aExpression copy];
firstArgumentName = [aFirstArgumentName copy];
secondArgumentName = [aSecondArgumentName copy];
return self;
}
#pragma mark -
#pragma mark Value getters
- (CGFloat) valueWithX: (CGFloat) a_X y: (CGFloat) a_Y
{
//TODO: Optimize this. Read GCMathParser first.
[parser setSymbolValue: a_X forKey: firstArgumentName];
[parser setSymbolValue: a_Y forKey: secondArgumentName];
return (CGFloat)[parser evaluate: expression];
}
#pragma mark -
#pragma mark Deallocation method
- (void) dealloc
{
[expression release];
[parser release];
[super dealloc];
}
@end