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

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

简书上看到的一片文章

程序画出来的效果:

程序画图效果.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<kCycleNum; i++) {
        LineModel *lineS = [lines objectAtIndex:(i-1)];
        LineModel *lineE = [lines objectAtIndex:(i-3)];
        CGPoint pointStart = lineS.pointEnd;
        CGPoint pointEnd = lineE.pointNewStart;
        LineModel *line = [[LineModel alloc] initWith:pointStart and:pointEnd andPercent:kPercent];
        
        [lines addObject:line];
    }
    
    CGFloat x = ([UIScreen mainScreen].bounds.size.width - kBoardWH)*0.5;
    CGFloat y = ([UIScreen mainScreen].bounds.size.height - kBoardWH)*0.5 - 200;
    DrawingBoardView *drawingBoardView = [[DrawingBoardView alloc] initWithFrame:CGRectMake(x, y, kBoardWH, kBoardWH)];
    drawingBoardView.lines = lines;
    drawingBoardView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:drawingBoardView];
}
//画三角形
-(void)drawingSanjiao{
    CGPoint p0 = CGPointMake(0, 0);
    CGPoint p1 = CGPointMake(kBoardWH, 0);
    CGPoint p2 = CGPointMake(kBoardWH*0.5, kBoardWH*0.86);
    CGPoint p3 = 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];
    
    NSMutableArray *lines = [NSMutableArray array];
    [lines addObject:line0];
    [lines addObject:line1];
    [lines addObject:line2];
    
    for (int i=3; i<kCycleNum; i++) {
        LineModel *lineS = [lines objectAtIndex:(i-1)];
        LineModel *lineE = [lines objectAtIndex:(i-2)];
        CGPoint pointStart = lineS.pointEnd;
        CGPoint pointEnd = lineE.pointNewStart;
        LineModel *line = [[LineModel alloc] initWith:pointStart and:pointEnd andPercent:kPercent];
        
        [lines addObject:line];
    }
    
    CGFloat x = ([UIScreen mainScreen].bounds.size.width - kBoardWH)*0.5;
    CGFloat y = ([UIScreen mainScreen].bounds.size.height - kBoardWH)*0.5 +200 +40;
    DrawingBoardView *drawingBoardView = [[DrawingBoardView alloc] initWithFrame:CGRectMake(x, y, kBoardWH, kBoardWH)];
    drawingBoardView.lines = lines;
    drawingBoardView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:drawingBoardView];
}
//画八边形
-(void)drawingBabianxing{
    float f1 = 0.3;
    float f2 = 1-f1;
    CGPoint p0 = CGPointMake(0, kBoardWH*f1);
    CGPoint p1 = CGPointMake(kBoardWH*f1, 0);
    CGPoint p2 = CGPointMake(kBoardWH*f2, 0);
    
    CGPoint p3 = CGPointMake(kBoardWH,kBoardWH*f1);
    CGPoint p4 = CGPointMake(kBoardWH, kBoardWH*f2);
    
    CGPoint p5 = CGPointMake(kBoardWH*f2, kBoardWH);
    CGPoint p6 = CGPointMake(kBoardWH*f1, kBoardWH);
    CGPoint p7 = CGPointMake(0, kBoardWH*f2);
    CGPoint p8 = CGPointMake(0, kBoardWH*f1);
    
    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];
    LineModel *line4 = [[LineModel alloc] initWith:p4 and:p5 andPercent:kPercent];
    LineModel *line5 = [[LineModel alloc] initWith:p5 and:p6 andPercent:kPercent];
    LineModel *line6 = [[LineModel alloc] initWith:p6 and:p7 andPercent:kPercent];
    LineModel *line7 = [[LineModel alloc] initWith:p7 and:p8 andPercent:kPercent];
    
    NSMutableArray *lines = [NSMutableArray array];
    [lines addObject:line0];
    [lines addObject:line1];
    [lines addObject:line2];
    [lines addObject:line3];
    [lines addObject:line4];
    [lines addObject:line5];
    [lines addObject:line6];
    [lines addObject:line7];
    
    for (int i=8; i<kCycleNum; i++) {
        LineModel *lineS = [lines objectAtIndex:(i-1)];
        LineModel *lineE = [lines objectAtIndex:(i-7)];
        CGPoint pointStart = lineS.pointEnd;
        CGPoint pointEnd = lineE.pointNewStart;
        LineModel *line = [[LineModel alloc] initWith:pointStart and:pointEnd andPercent:kPercent];
        
        [lines addObject:line];
    }
    
    
    CGFloat x = ([UIScreen mainScreen].bounds.size.width - kBoardWH)*0.5;
    CGFloat y = ([UIScreen mainScreen].bounds.size.height - kBoardWH)*0.5 +20;
    DrawingBoardView *drawingBoardView = [[DrawingBoardView alloc] initWithFrame:CGRectMake(x, y, kBoardWH, kBoardWH)];
    drawingBoardView.lines = lines;
    drawingBoardView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:drawingBoardView];
}

@end

//
//  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.安装 sudo gem install sass 需要获取权限2.sass监视scss的变化 注意红线部分不能加空格
    司马捷阅读 3,153评论 0 0
  • 天然御泽益清香,地处甘文甲四方。茗叶芬芳涎欲滴,凝神细品沁心房。偶遇诗句,因而引我回忆起一夜平遥。 我曾经在梦中来...
    一庭月色阅读 2,795评论 0 0
  • 风险管理就是以最小的成本获取最最大的安全保障,对于可能性大,损失小的风险,要懂的降低可能性,想办法预防。
    临淄茂业DDM王春梅阅读 906评论 0 0
  • 很久没有更新追源了。 这几天,上海天气阴阴的,心情说不上好与坏,就如此刻这室内的空间,又闷又潮。 一直奋力奔跑,总...
    胖咸鱼sunshine阅读 6,308评论 1 3

友情链接更多精彩内容