#import "CurveView.h"

@implementation CurveView

- (id)init {
    _xmin= -4;
    _xmax= 4;
    _ymin= -4;
    _ymax= 4;
    _xdelta= 0.02;

    _scalefactor= 1;
    return self;
}
- (void)startrescale
{
    _scalefactor= 1;
}
- (void)endrescale
{
    _scalefactor= 1;
}
- (void)rescale:(double)factor
{
    _xmin /= _scalefactor;
    _xmax /= _scalefactor;
    _ymin /= _scalefactor;
    _ymax /= _scalefactor;
    _xdelta /= _scalefactor;

    _scalefactor= factor;

    _xmin *= _scalefactor;
    _xmax *= _scalefactor;
    _ymin *= _scalefactor;
    _ymax *= _scalefactor;
    _xdelta *= _scalefactor;
}

- (CGPoint)translate:(CGRect)bounds point:(CGPoint)point
{
    double xdisp= (point.x-_xmin)/(_xmax-_xmin)*(bounds.size.width-1)+bounds.origin.x;
    double ydisp= (-point.y-_ymin)/(_ymax-_ymin)*(bounds.size.height-1)+bounds.origin.y;
    return CGPointMake(xdisp, ydisp);
}
- (CGPoint)translateBack:(CGRect)bounds point:(CGPoint)point
{
    double xval= _xmin+(point.x-bounds.origin.x)*(_xmax-_xmin)/(bounds.size.width-1);
    double yval= -(_ymin+(point.y-bounds.origin.y)*(_ymax-_ymin)/(bounds.size.height-1));

    return CGPointMake(xval,yval);
}
- (NSArray*)translateCurve:(CGRect)bounds curve:(NSArray*)curve
{
    NSMutableArray* xcurve= [[NSMutableArray alloc] init];
    for (int ii=0 ; ii<[curve count] ; ii++) {
        NSArray *part= [curve objectAtIndex:ii];
        NSMutableArray *xpart= [[NSMutableArray alloc] init];
        [xcurve addObject:xpart];
        for (int jj=0 ; jj<[part count] ; jj++)
        {
            [xpart addObject:[NSValue valueWithCGPoint:[self translate:bounds point:[[part objectAtIndex:jj] CGPointValue]]]];
        }
    }
    return [xcurve autorelease];
}
@end

