diff --git a/MathPad/MPSumFunction.h b/MathPad/MPSumFunction.h new file mode 100644 index 0000000..4676883 --- /dev/null +++ b/MathPad/MPSumFunction.h @@ -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 diff --git a/MathPad/MPSumFunction.m b/MathPad/MPSumFunction.m new file mode 100644 index 0000000..6608bde --- /dev/null +++ b/MathPad/MPSumFunction.m @@ -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 diff --git a/MathPad/MPSumFunctionLayout.h b/MathPad/MPSumFunctionLayout.h new file mode 100644 index 0000000..9750f51 --- /dev/null +++ b/MathPad/MPSumFunctionLayout.h @@ -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 diff --git a/MathPad/MPSumFunctionLayout.m b/MathPad/MPSumFunctionLayout.m new file mode 100644 index 0000000..d001fe5 --- /dev/null +++ b/MathPad/MPSumFunctionLayout.m @@ -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