昨天看簡書,在一片文章里看到了里克的悖論,覺得很有意思,就寫了一個小程序,通過這個程序畫一個里克悖論圖,太好玩兒了,玩了一整天.
(附:我看到的那篇簡書)
先說說什么是里克的悖論:(圖片出自我讀的那篇簡書)
簡書上看到的一片文章
程序畫出來的效果:
程序畫圖效果.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
//循環(huán)次數(shù)
#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);
//設(shè)置顏色
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