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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
//
// PRCurvesPath.m
// PRICE
//
// Created by Riccardo Mottola on Thu 11 August 2011.
// Copyright 2011 Riccardo Mottola. All rights reserved.
//
// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#import "PRCurvesPath.h"
#import <AppKit/NSColor.h>
@implementation PRCurvesPath
- (id)init
{
self = [super init];
if(self)
{
isEditing = YES;
}
return self;
}
-(void)stroke
{
unsigned i;
[super stroke];
if (isEditing)
for (i = 0; i < [self elementCount]; i++)
{
NSPoint cp[3];
NSBezierPathElement pe;
NSRect cpRect;
pe = [self elementAtIndex: i associatedPoints: cp];
if (pe == NSMoveToBezierPathElement)
{
[[NSColor purpleColor] set];
cpRect = NSMakeRect(cp[2].x - 1, cp[2].y - 1, 3, 3);
[NSBezierPath fillRect: cpRect];
}
else if (pe == NSCurveToBezierPathElement)
{
[[NSColor orangeColor] set];
cpRect = NSMakeRect(cp[0].x - 1, cp[0].y - 1, 3, 3);
[NSBezierPath fillRect: cpRect];
[[NSColor orangeColor] set];
cpRect = NSMakeRect(cp[1].x - 1, cp[1].y - 1, 3, 3);
[NSBezierPath fillRect: cpRect];
[[NSColor purpleColor] set];
cpRect = NSMakeRect(cp[2].x - 1, cp[2].y - 1, 3, 3);
[NSBezierPath fillRect: cpRect];
}
}
}
-(BOOL)isEditing;
{
return isEditing;
}
-(void)setIsEditing: (BOOL)flag
{
isEditing = flag;
}
-(NSArray *)values
{
NSBezierPath *tp;
NSBezierPath *fp;
unsigned i, j;
NSPoint *yVals;
unsigned valsCount;
NSMutableArray *f;
unsigned minX, maxX;
float lastY;
tp = [NSBezierPath bezierPath];
[tp appendBezierPath:(NSBezierPath *)self];
[tp setFlatness: 1.0];
fp = [tp bezierPathByFlatteningPath];
valsCount = [fp elementCount];
yVals = calloc(valsCount, sizeof(NSPoint));
if (yVals == NULL)
return nil;
f = [[NSMutableArray alloc] initWithCapacity: UCHAR_MAX];
for (i = 0; i < valsCount; i++)
{
NSBezierPathElement pe;
NSPoint cp[3];
pe = [fp elementAtIndex: i associatedPoints: cp];
yVals[i] = cp[0];
}
minX = floor(yVals[0].x);
maxX = floor(yVals[valsCount].x);
for (i = 0; i < minX; i++)
[f addObject: [NSNumber numberWithFloat: yVals[0].y]];
for (j = 0; j < valsCount; j++)
{
unsigned k, l;
float y1, y2;
/* find estimation interval (k) and begin/end values y1 y2 */
y1 = yVals[j].y;
l = i;
k = i;
if (j+1 < valsCount)
{
while (k < floor(yVals[j+1].x))
k++;
y2 = yVals[j+1].y;
}
else
{
y2 = y1;
k++;
}
// NSLog(@"%d block %d-%d, y1-y2 %f-%f", j, l, k, y1, y2);
for (; i < k; i++)
{
float a;
a = (float)(i-l)/(k-l);
// NSLog(@"a: %f", a);
[f addObject: [NSNumber numberWithFloat: (y1 + (y2-y1)*a)]];
}
}
lastY = [[f objectAtIndex: [f count]-1] floatValue];
for (; i <= UCHAR_MAX; i++)
[f addObject: [NSNumber numberWithFloat: lastY]];
free(yVals);
return f;
}
@end
|