1

我想在 XCode 中的图表上创建蝙蝠侠方程,作为朋友在生日那天打开应用程序时送给他的礼物。所以我找到了蝙蝠侠方程的方程。

方程是:

((x/7)2 Sqrt[Abs[Abs[x] – 3]/(Abs[x] – 3)] + (y/3)2 Sqrt[Abs[y + (3 Sqrt[33])/7]/(y + (3 Sqrt[33])/7)] – 1)(Abs[x/2] – ((3 Sqrt[33] – 7)/112) x2 – 3 + Sqrt[1 - (Abs[Abs[x] – 2] – 1)2 ] – y) (9 Sqrt[Abs[(Abs[x] – 1) (Abs[x] – 3/4)]/((1 – Abs[x]) (Abs[x] – 3/4))] – 8 Abs[x] – y) (3 Abs[x] + .75 Sqrt[Abs[(Abs[x] – 3/4) (Abs[x] – 1/2)]/((3/4 – Abs[x]) (Abs[x] – 1/2))] – y) (9/4 Sqrt[Abs[(x - 1/2) (x + 1/2)]/((1/2 – x) (1/2 + x))] – y) ((6 Sqrt[10])/7 + (3/2 – Abs[x]/2) Sqrt[Abs[Abs[x] – 1]/(Abs[x] – 1)] – (6 Sqrt[10])/14 Sqrt[4 - (Abs[x] – 1)2 ] – y) == 0

我想将其转换为我拥有的编码图:

@implementation GraphView


- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    // preparing the color
    CGFloat lightGrey[4] = {1.0f, 1.0f, 1.0f, 1.0f};
    CGContextSetStrokeColor(context, lightGrey);
    CGContextBeginPath(context); // beginning of the paths

    CGPoint middlePoint;
    middlePoint.x = self.frame.origin.x + self.frame.size.width / 2;
    middlePoint.y = self.frame.origin.y + self.frame.size.height / 2;

    // X axis
    CGContextMoveToPoint(context, 0, middlePoint.y);
    CGContextAddLineToPoint(context, self.frame.size.width, middlePoint.y);
    CGContextDrawPath(context, kCGPathStroke);

    // Y axis
    CGContextMoveToPoint(context, middlePoint.x, 0);
    CGContextAddLineToPoint(context, middlePoint.x, self.frame.size.height);
    CGContextDrawPath(context, kCGPathStroke);

    // drawing the X axis
    CGFloat xaxis = self.bounds.size.height;
    NSLog(@"%f", xaxis);
    CGFloat xNumOfGaps = xaxis / 120;

    // main gaps

       CGFloat xMainGap = 120;
        for (int i = 0; i < xNumOfGaps; i++) {
        CGContextMoveToPoint(context, (self.bounds.size.width/2)-5, xMainGap);
        CGContextAddLineToPoint(context, (self.bounds.size.width/2)+5, xMainGap);
            xMainGap = xMainGap + 120;
        }

    // vertical grid lines
    for (int i = 0; i < self.frame.size.width; i += 32) {
        CGContextMoveToPoint(context, i, 0);
        CGContextAddLineToPoint(context, i, self.frame.size.height);
        UIColor *greyLine = [[UIColor alloc]initWithRed:0.7 green:0.7 blue:0.7 alpha:0.5];
        [greyLine setStroke];
        CGContextDrawPath(context, kCGPathStroke);
    }

    // horizontal grid lines
    for (int i = 0; i < self.frame.size.height; i += 34) {
        CGContextMoveToPoint(context, 0, i);
        CGContextAddLineToPoint(context, self.frame.size.width, i);
        UIColor *greyLine = [[UIColor alloc]initWithRed:0.7 green:0.7 blue:0.7 alpha:0.5];
        [greyLine setStroke];
        CGContextDrawPath(context, kCGPathStroke);
    }



//    CGContextSetLineWidth(context, 1);
//    CGContextSetLineJoin(context, kCGLineJoinRound);
//    CGFloat amplitude = self.bounds.size.height/2 / 4;
//
//    // drawing the sine wave
//    for(CGFloat x = 0; x < self.frame.size.width; x += 0.5) {
//        CGFloat y = amplitude * sinf(2 * M_PI * (x / self.frame.size.width) * 2) + self.frame.size.height/2;
//        if(x == 0) {
//            CGContextMoveToPoint(context, x, y);
//        } else {
//            CGContextAddLineToPoint(context, x, y);
//        }
//    }
//    // drawing the cos wave
//    for(CGFloat x = 0; x < self.frame.size.width; x += 0.5) {
//        CGFloat y = amplitude * cosf(2 * M_PI * (x / self.frame.size.width) * 2) + self.frame.size.height/2;
//        if(x == 0) {
//            CGContextMoveToPoint(context, x, y);
//        } else {
//            CGContextAddLineToPoint(context, x, y);
//        }
//    }
//
// //drawing the tan wave
//for(CGFloat x = 0; x < self.frame.size.width; x += 0.5) {
//    CGFloat y = amplitude * tanf(2 * M_PI * (x / self.frame.size.width) * 2) + self.frame.size.height/2;
//    if(x == 0) {
//        CGContextMoveToPoint(context, x, y);
//    } else {
//        CGContextAddLineToPoint(context, x, y);
//    }
//}
//[[UIColor redColor] setStroke];
//CGContextDrawPath(context, kCGPathStroke);
}

@end
4

0 回答 0