Created
August 26, 2015 13:45
-
-
Save mzsima/292ba80cff3ad2baeb06 to your computer and use it in GitHub Desktop.
Euclidean Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.mm | |
// EuclideanAlgorithm | |
// | |
// Created by MizushimaYusuke on 8/26/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
#import "ViewController.h" | |
#include <vector> | |
#include <memory> | |
using namespace::std; | |
class EuclideanGCD { | |
public: | |
vector<CGSize> divisors; | |
int gcd(int a, int b) { | |
divisors.emplace_back(CGSizeMake(b, b)); | |
if (b==0) return a; | |
return gcd(b, a % b); | |
} | |
}; | |
@interface ViewController () | |
@property (nonatomic, weak) UIView *originalRect; | |
@property (nonatomic, strong) NSMutableArray *anims; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.view.backgroundColor = [UIColor colorWithHue:0.75 saturation:0.8 brightness:0.3 alpha:1]; | |
int w = 300; | |
int h = 165; | |
UILabel *title = [[UILabel alloc] init]; | |
title.text = [NSString stringWithFormat:@"greatest common divisor \n %d and %d", w, h]; | |
title.textColor = [UIColor whiteColor]; | |
title.font = [UIFont systemFontOfSize:30]; | |
title.textAlignment = NSTextAlignmentCenter; | |
title.numberOfLines = 0; | |
[title sizeToFit]; | |
title.center = CGPointMake(CGRectGetMidX(self.view.bounds), 130); | |
[self.view addSubview:title]; | |
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)]; | |
v.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); | |
v.backgroundColor = [UIColor clearColor]; | |
v.layer.borderWidth = 2; | |
v.layer.borderColor = [UIColor whiteColor].CGColor; | |
[self.view addSubview:v]; | |
self.originalRect = v; | |
} | |
typedef void (^DelayView)(void); | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
shared_ptr<EuclideanGCD> cppClass(new EuclideanGCD()); | |
int result = cppClass->gcd(self.originalRect.frame.size.width, self.originalRect.frame.size.height); | |
self.anims = [NSMutableArray array]; | |
float x = 0; | |
float y = 0; | |
int cnt = 0; | |
for (auto s : cppClass->divisors) { | |
if (s.width == 0 || s.height == 0) continue; | |
int w = cnt % 2 ? self.originalRect.frame.size.height : self.originalRect.frame.size.width; | |
int l = cnt % 2 ? s.height : s.width; | |
int p = cnt % 2 ? y : x; | |
for (int i=0; i< (w - p)/l; i++) { | |
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, s.width, s.height)]; | |
v.backgroundColor = [UIColor colorWithHue:cnt * 0.1 saturation:0.4 brightness:1 alpha:1]; | |
v.layer.borderWidth = 1; | |
v.layer.borderColor = [UIColor grayColor].CGColor; | |
if (cnt % 2 == 0) x += s.width; | |
else y += s.height; | |
DelayView delayView = ^{ [self.originalRect addSubview:v]; }; | |
[self.anims addObject:delayView]; | |
} | |
cnt++; | |
} | |
UILabel *ans = [[UILabel alloc] init]; | |
ans.text = [NSString stringWithFormat:@"%d", result]; | |
ans.textColor = [UIColor whiteColor]; | |
ans.font = [UIFont systemFontOfSize:20]; | |
[ans sizeToFit]; | |
ans.center = CGPointMake(CGRectGetMaxX(self.originalRect.bounds) - 5, CGRectGetMaxY(self.originalRect.bounds) + 15); | |
[self.originalRect addSubview:ans]; | |
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(showMyView:) userInfo:nil repeats:YES]; | |
} | |
- (void)showMyView:(NSTimer *)timer { | |
if (self.anims.count == 0) { | |
[timer invalidate]; | |
return; | |
} | |
DelayView b = ((DelayView)self.anims[0]); | |
b(); | |
[self.anims removeObjectAtIndex:0]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment