里克的悖论_玩了一整天的小程序

昨天看,在一片文章里看到了里克的悖论,觉得很有意思,就写了一个小程序,通过这个程序画一个里克悖论图,太好玩儿了,玩了一整天.
(附:我看到的那篇)
先说说什么是里克的悖论:(图片出自我读的那篇)

里克的悖论_玩了一整天的小程序_第1张图片
上看到的一片文章

程序画出来的效果:

里克的悖论_玩了一整天的小程序_第2张图片
程序画图效果.png
//
//  ViewController.m
//  里克的悖论
//
//  Created by sb on 16/11/17.
//  Copyright © 2016年 sb. All rights reserved.
//

#import "ViewController.h"
#import "LineModel.h"
#import "DrawingBoardView.h"

//百分比
#define kPercent 0.12
#define kBoardWH 200.00f
//循环次数
#define kCycleNum 700

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self drawingSibianxing];
    [self drawingSanjiao];
    [self drawingBabianxing];
}
//画四边形
-(void)drawingSibianxing{
    CGPoint p0 = CGPointMake(0, 0);
    CGPoint p1 = CGPointMake(kBoardWH, 0);
    CGPoint p2 = CGPointMake(kBoardWH, kBoardWH);
    CGPoint p3 = CGPointMake(0, kBoardWH);
    CGPoint p4 = CGPointMake(0, 0);
    
    LineModel *line0 = [[LineModel alloc] init];
    line0.pointStart = p0;
    line0.pointEnd = p1;
    line0.pointNewStart = p0;
    
    LineModel *line1 = [[LineModel alloc] initWith:p1 and:p2 andPercent:kPercent];
    LineModel *line2 = [[LineModel alloc] initWith:p2 and:p3 andPercent:kPercent];
    LineModel *line3 = [[LineModel alloc] initWith:p3 and:p4 andPercent:kPercent];
    
    NSMutableArray *lines = [NSMutableArray array];
    [lines addObject:line0];
    [lines addObject:line1];
    [lines addObject:line2];
    [lines addObject:line3];
    
    for (int i=4; i
//
//  LineModel.m
//  里克的悖论
//
//  Created by sb on 16/11/17.
//  Copyright © 2016年 sb. All rights reserved.
//

#import "LineModel.h"

@implementation LineModel

-(instancetype)initWith:(CGPoint) pointStart and:(CGPoint)pointEnd andPercent:(CGFloat)percent{
    self = [[LineModel alloc] init];
    self.pointStart = pointStart;
    self.pointEnd = pointEnd;
    self.pointNewStart = [self getNewStartPointWith:pointStart and:pointEnd andPercent:(CGFloat)percent];
    return self;
}

-(CGPoint)getNewStartPointWith:(CGPoint)pointStart and:(CGPoint)pointEnd andPercent:(CGFloat)percent{
    
    CGFloat x = (1-percent)*pointStart.x + percent*pointEnd.x;
    CGFloat y = (1-percent)*pointStart.y + percent*pointEnd.y;
    
    CGPoint newStartPoint = CGPointMake(x, y);
    return newStartPoint;
}
-(void)logOutLine{
    NSLog(@"pointStart:(%.2f,%.2f) -- pointEnd:(%.2f,%.2f)  pointNewStart:(%.2f,%.2f)",self.pointStart.x,self.pointStart.y,self.pointEnd.x,self.pointEnd.y,self.pointNewStart.x,self.pointNewStart.y);
}

@end

//
//  DrawingBoardView.m
//  里克的悖论
//
//  Created by sb on 16/11/17.
//  Copyright © 2016年 sb. All rights reserved.
//

#import "DrawingBoardView.h"
#import "LineModel.h"

@implementation DrawingBoardView

-(void)drawRect:(CGRect)rect{
    //获得处理的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    //指定直线样式
    CGContextSetLineCap(context, kCGLineCapSquare);
    
    //直线宽度
    CGContextSetLineWidth(context, 1);
    //设置颜色
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);

    //开始绘制
    CGContextBeginPath(context);
    
    LineModel *lineStart = [self.lines firstObject];
    //画笔移动到原点
    CGContextMoveToPoint(context, lineStart.pointStart.x, lineStart.pointStart.y);
    
    for (LineModel *line in _lines) {
        //下一点
        CGContextAddLineToPoint(context, line.pointEnd.x, line.pointEnd.y);
    }
    //绘制完成
    CGContextStrokePath(context);
}
@end

你可能感兴趣的:(里克的悖论_玩了一整天的小程序)