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

昨天看簡書,在一片文章里看到了里克的悖論,覺得很有意思,就寫了一個小程序,通過這個程序畫一個里克悖論圖,太好玩兒了,玩了一整天.
(附:我看到的那篇簡書)
先說說什么是里克的悖論:(圖片出自我讀的那篇簡書)

簡書上看到的一片文章

程序畫出來的效果:

程序畫圖效果.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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末唆涝,一起剝皮案震驚了整個濱河市爹脾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌淤年,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡胎撇,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門殖氏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晚树,“玉大人,你說我怎么就攤上這事雅采【粼鳎” “怎么了慨亲?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長宝鼓。 經(jīng)常有香客問我刑棵,道長,這世上最難降的妖魔是什么愚铡? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任蛉签,我火速辦了婚禮,結(jié)果婚禮上沥寥,老公的妹妹穿的比我還像新娘碍舍。我一直安慰自己,他們只是感情好营曼,可當(dāng)我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布乒验。 她就那樣靜靜地躺著愚隧,像睡著了一般蒂阱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狂塘,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天录煤,我揣著相機與錄音,去河邊找鬼荞胡。 笑死妈踊,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的泪漂。 我是一名探鬼主播廊营,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼萝勤!你這毒婦竟也來了露筒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤敌卓,失蹤者是張志新(化名)和其女友劉穎慎式,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體趟径,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡瘪吏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蜗巧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掌眠。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖幕屹,靈堂內(nèi)的尸體忽然破棺而出蓝丙,到底是詐尸還是另有隱情刑枝,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布迅腔,位于F島的核電站装畅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏沧烈。R本人自食惡果不足惜掠兄,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锌雀。 院中可真熱鬧蚂夕,春花似錦、人聲如沸腋逆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惩歉。三九已至等脂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間撑蚌,已是汗流浹背上遥。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留争涌,地道東北人粉楚。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像亮垫,于是被迫代替她去往敵國和親模软。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容

  • 1.安裝 sudo gem install sass 需要獲取權(quán)限2.sass監(jiān)視scss的變化 注意紅線部分不能加空格
    司馬捷閱讀 421評論 0 0
  • 天然御澤益清香,地處甘文甲四方害晦。茗葉芬芳涎欲滴特铝,凝神細品沁心房。偶遇詩句壹瘟,因而引我回憶起一夜平遙鲫剿。 我曾經(jīng)在夢中來...
    一庭月色閱讀 324評論 0 0
  • 風(fēng)險管理就是以最小的成本獲取最最大的安全保障,對于可能性大稻轨,損失小的風(fēng)險灵莲,要懂的降低可能性,想辦法預(yù)防殴俱。
    臨淄茂業(yè)DDM王春梅閱讀 78評論 0 0
  • 很久沒有更新追源了政冻。 這幾天枚抵,上海天氣陰陰的,心情說不上好與壞明场,就如此刻這室內(nèi)的空間汽摹,又悶又潮。 一直奮力奔跑苦锨,總...
    胖咸魚sunshine閱讀 2,090評論 1 3