Created
September 12, 2015 14:55
-
-
Save mzsima/4611b904aaa31ab31a57 to your computer and use it in GitHub Desktop.
polyline max min
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.m | |
// polylineMax | |
// | |
// Created by Mizushima Yusuke on 9/12/15. | |
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved. | |
// | |
#import "ViewController.h" | |
#include <vector> | |
#include <memory> | |
using namespace std; | |
class FindMaxMin { | |
public: | |
CGPoint maxP; | |
CGPoint minP = CGPointMake(0, 9999); | |
void find(vector<CGPoint> pts) { | |
for (CGPoint p : pts) { | |
if (maxP.y <= p.y) maxP = p; | |
if (minP.y >= p.y) minP = p; | |
} | |
} | |
}; | |
@interface ViewController () { | |
vector<CGPoint> points; | |
float dy; | |
} | |
@property (nonatomic, weak) UILabel *min; | |
@property (nonatomic, weak) UILabel *max; | |
@property (nonatomic, weak) UIView *graph; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
UIFont *font = [UIFont fontWithName:@"HiraKakuProN-W3" size:30]; | |
UIColor *colorA = [UIColor colorWithHue:0.25 saturation:0.3 brightness:1 alpha:1]; | |
UIColor *colorB = [UIColor colorWithHue:0.55 saturation:0.4 brightness:0.8 alpha:1]; | |
self.view.backgroundColor = colorA; | |
CGRect oRect = self.view.bounds; | |
float l = oRect.size.width; | |
UIView *gsheet = [[UIView alloc] initWithFrame:CGRectMake(l * 0.05, l * 0.1, l * 0.9, 450)]; | |
gsheet.backgroundColor = colorB; | |
[self.view addSubview:gsheet]; | |
self.graph = gsheet; | |
for(int i = 0; i < 2; i++) { | |
float h = i ? l * 0.4 : l * 0.7; | |
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(l * 0.1, h, l * 0.7, 2)]; | |
line.backgroundColor = colorA; | |
[self.view addSubview:line]; | |
UILabel *n = [[UILabel alloc] init]; | |
n.text = i ? @"50" : @"100"; | |
n.font = font; | |
n.textColor = colorA; | |
[n sizeToFit]; | |
n.center = CGPointMake(l * 0.88, h); | |
[self.view addSubview:n]; | |
} | |
dy = (l * 0.6) / 100.0; | |
for (int i=0; i<30; i++) { | |
float y = (arc4random() % 100) * dy; | |
CGPoint p = CGPointMake(i * 9 + 10, y); | |
points.emplace_back(p); | |
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)]; | |
dot.backgroundColor = colorA; | |
dot.transform = CGAffineTransformMakeRotation(M_PI * 0.25); | |
dot.center = p; | |
[gsheet addSubview:dot]; | |
} | |
UILabel *min = [[UILabel alloc] init]; | |
min.text = @"MIN"; | |
min.font = font; | |
min.textColor = colorA; | |
[min sizeToFit]; | |
min.frame = CGRectMake(l * 0.1, l * 0.99, min.frame.size.width, min.frame.size.height); | |
[self.view addSubview:min]; | |
self.min = min; | |
UILabel *max = [[UILabel alloc] init]; | |
max.text = @"MAX"; | |
max.font = font; | |
max.textColor = colorA; | |
[max sizeToFit]; | |
max.frame = CGRectMake(l * 0.1, l * 1.14, max.frame.size.width, max.frame.size.height); | |
[self.view addSubview:max]; | |
self.max = max; | |
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; | |
btn.frame = CGRectMake(l * 0.05, 470 + l * 0.1, l * 0.9, 100); | |
btn.backgroundColor = colorB; | |
btn.titleLabel.font = font; | |
[btn setTitle:@"Calculate" forState:UIControlStateNormal]; | |
[btn setTitleColor:colorA forState:UIControlStateNormal]; | |
[self.view addSubview:btn]; | |
[btn addTarget:self action:@selector(calc) forControlEvents:UIControlEventTouchUpInside]; | |
} | |
- (void)calc { | |
shared_ptr<FindMaxMin> cppClass(new FindMaxMin()); | |
cppClass->find(points); | |
UIView *marker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; | |
marker.layer.borderColor = [UIColor redColor].CGColor; | |
marker.layer.borderWidth = 2; | |
[self.view addSubview:marker]; | |
for (int i=0; i<points.size(); i++) { | |
CGPoint p = points[i]; | |
[UIView animateWithDuration:0.1 delay:0.2 * i options:0 animations:^{ | |
marker.center = [self.graph convertPoint:p toView:self.view]; | |
marker.transform = CGAffineTransformRotate(marker.transform, 0.3); | |
} completion:^(BOOL finished) { | |
if (p.y == cppClass->maxP.y) { | |
UIView *maxV = [self.graph hitTest:p withEvent:nil]; | |
maxV.backgroundColor = [UIColor blueColor]; | |
maxV.transform = CGAffineTransformMakeScale(3, 3); | |
UILabel *mx = [[UILabel alloc] init]; | |
mx.font = self.max.font; | |
mx.textColor = self.max.textColor; | |
mx.frame = CGRectMake(self.max.frame.origin.x + 100, self.max.frame.origin.y, 120, self.max.frame.size.height); | |
mx.text = [NSString stringWithFormat:@"%d", (int)(p.y / dy)]; | |
[self.view addSubview:mx]; | |
} | |
if (p.y == cppClass->minP.y) { | |
UIView *minV = [self.graph hitTest:p withEvent:nil]; | |
minV.backgroundColor = [UIColor orangeColor]; | |
minV.transform = CGAffineTransformMakeScale(3, 3); | |
UILabel *mn = [[UILabel alloc] init]; | |
mn.font = self.max.font; | |
mn.textColor = self.max.textColor; | |
mn.frame = CGRectMake(self.min.frame.origin.x + 100, self.min.frame.origin.y, 120, self.min.frame.size.height); | |
mn.text = [NSString stringWithFormat:@"%d", (int)(p.y / dy)]; | |
[self.view addSubview:mn]; | |
} | |
}]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4344