Implemented the Sum Function
This commit is contained in:
19
MathPad/MPSumFunction.h
Normal file
19
MathPad/MPSumFunction.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// MPSumFunction.h
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 22.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "MPFunction.h"
|
||||||
|
|
||||||
|
@class MPSumFunction, MPExpression;
|
||||||
|
|
||||||
|
@interface MPSumFunction : MPFunction
|
||||||
|
|
||||||
|
@property (nonatomic, strong) MPExpression *startExpression;
|
||||||
|
@property (nonatomic, strong) MPExpression *targetExpression;
|
||||||
|
@property (nonatomic, strong) MPExpression *sumExpression;
|
||||||
|
|
||||||
|
@end
|
||||||
120
MathPad/MPSumFunction.m
Normal file
120
MathPad/MPSumFunction.m
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
//
|
||||||
|
// MPSumFunction.m
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 22.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "MPSumFunction.h"
|
||||||
|
#import "MPExpression.h"
|
||||||
|
|
||||||
|
@implementation MPSumFunction
|
||||||
|
|
||||||
|
#pragma mark Creation Methods
|
||||||
|
|
||||||
|
- (instancetype)init
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_startExpression = [[MPExpression alloc] init];
|
||||||
|
_startExpression.parent = self;
|
||||||
|
_targetExpression = [[MPExpression alloc] init];
|
||||||
|
_targetExpression.parent = self;
|
||||||
|
_sumExpression = [[MPExpression alloc] init];
|
||||||
|
_sumExpression.parent = self;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark Working With the Expression Tree
|
||||||
|
|
||||||
|
- (NSUInteger)numberOfChildren
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (MPExpression *)childAtIndex:(NSUInteger)index
|
||||||
|
{
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return self.startExpression;
|
||||||
|
case 1:
|
||||||
|
return self.targetExpression;
|
||||||
|
case 2:
|
||||||
|
return self.sumExpression;
|
||||||
|
default:
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray *)children
|
||||||
|
{
|
||||||
|
return @[self.startExpression, self.targetExpression, self.sumExpression];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setChild:(MPExpression *)child
|
||||||
|
atIndex:(NSUInteger)index
|
||||||
|
{
|
||||||
|
switch (index) {
|
||||||
|
// TODO: Copy child?
|
||||||
|
case 0:
|
||||||
|
self.startExpression.parent = nil;
|
||||||
|
self.startExpression = child;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
self.targetExpression.parent = nil;
|
||||||
|
self.targetExpression = child;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
self.sumExpression.parent = nil;
|
||||||
|
self.sumExpression = child;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
child.parent = self;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark Evaluating Functions
|
||||||
|
|
||||||
|
- (double)doubleValue
|
||||||
|
{
|
||||||
|
#warning Implementation
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark Working With Functions
|
||||||
|
|
||||||
|
- (BOOL)isEqualToFunction:(MPFunction *)aFunction
|
||||||
|
{
|
||||||
|
if (![aFunction isKindOfClass:[MPSumFunction class]]) {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
MPSumFunction *sumFunction = (MPSumFunction *)aFunction;
|
||||||
|
if (![self.startExpression isEqualToExpression:sumFunction.startExpression]) {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
if (![self.targetExpression isEqualToExpression:sumFunction.targetExpression]) {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
return [self.sumExpression isEqualToExpression:sumFunction.sumExpression];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)description
|
||||||
|
{
|
||||||
|
return [NSString stringWithFormat:@"Sum(From: %@; To: %@; Using: %@)", self.startExpression, self.targetExpression, self.sumExpression];
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - NSCopying
|
||||||
|
|
||||||
|
- (id)copyWithZone:(NSZone *)zone
|
||||||
|
{
|
||||||
|
MPSumFunction *copy = [[MPSumFunction allocWithZone:zone] init];
|
||||||
|
copy.startExpression = [self.startExpression copy];
|
||||||
|
copy.targetExpression = [self.targetExpression copy];
|
||||||
|
copy.sumExpression = [self.sumExpression copy];
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
17
MathPad/MPSumFunctionLayout.h
Normal file
17
MathPad/MPSumFunctionLayout.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// MPSumFunctionLayout.h
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 23.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "MPFunctionLayout.h"
|
||||||
|
|
||||||
|
@class MPSumFunctionLayout, MPSumFunction;
|
||||||
|
|
||||||
|
@interface MPSumFunctionLayout : MPFunctionLayout
|
||||||
|
|
||||||
|
- (MPSumFunction *)sumFunction;
|
||||||
|
|
||||||
|
@end
|
||||||
35
MathPad/MPSumFunctionLayout.m
Normal file
35
MathPad/MPSumFunctionLayout.m
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
//
|
||||||
|
// MPSumFunctionLayout.m
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 23.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "MPSumFunctionLayout.h"
|
||||||
|
#import "MPSumFunction.h"
|
||||||
|
|
||||||
|
@implementation MPSumFunctionLayout
|
||||||
|
|
||||||
|
- (MPSumFunction *)sumFunction
|
||||||
|
{
|
||||||
|
return (MPSumFunction *)self.function;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSSize)calculateSize
|
||||||
|
{
|
||||||
|
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"∑" attributes:@{NSFontAttributeName: [NSFont fontWithName:@"HelveticaNeue" size:50.0]}];
|
||||||
|
[self.textStorage setAttributedString:text];
|
||||||
|
NSRange glyphRange = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
|
||||||
|
return [self.layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:self.textContainer].size;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)drawFunctionAtPoint:(NSPoint)point
|
||||||
|
{
|
||||||
|
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"∑" attributes:@{NSFontAttributeName: [NSFont fontWithName:@"HelveticaNeue" size:50.0]}];
|
||||||
|
[self.textStorage setAttributedString:text];
|
||||||
|
NSRange glyphRange = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
|
||||||
|
[self.layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:point];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
Reference in New Issue
Block a user