
道を節電しつつ明るく照らせるような計算を試してみるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#include <memory>
#include <algorithm>
using namespace std;
class IntervalPartitioningSolver {
public:
void solve(NSArray*);
private:
int n;
int *x;
void greedy();
void notify(int i);
};
void IntervalPartitioningSolver::solve(NSArray *arr) {
int index=0;
n = (int)[arr count];
x = (int *) malloc(sizeof(int) * n);
NSEnumerator *enumerator = [arr objectEnumerator];
id obj;
while(obj = [enumerator nextObject])
{
x[index++] = [obj floatValue];
}
greedy();
}
void IntervalPartitioningSolver::greedy() {
int r = 50;
sort(x, x + n);
int i=0;
while (i < n) {
int s = x[i++];
NSLog(@”— %d %d”, i ,s);
while (i < n && x[i] <= s + r) {
i++;
NSLog(@”— — %d”, s + r);
}
int p = x[i – 1];
NSLog(@”— %d %d”, i, p);
while (i < n && x[i] <= p + r) i++;
if (i != n) i–;
notify(p);
}
}
void IntervalPartitioningSolver::notify(int i) {
[[NSNotificationCenter defaultCenter] postNotificationName:@”light up” object:@(i)];
}
@interface ViewController ()
@property (nonatomic) int counter;
@property (nonatomic, weak) UIView *mycar;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// random torch
float x = 25;
float max = CGRectGetMaxX(self.view.bounds) – 100;
while (x < max) {
float dx = arc4random() % 30 + 20;
x += dx;
UIView *torch = [[UIView alloc] initWithFrame:CGRectMake(x, 100, 10, 10)];
torch.tag = x;
torch.backgroundColor = [UIColor clearColor];
torch.layer.borderColor = [UIColor whiteColor].CGColor;
torch.layer.borderWidth = 1;
torch.layer.cornerRadius = 5;
[self.view addSubview:torch];
}
// road
UIView *road = [[UIView alloc] initWithFrame:CGRectMake(0, 200, CGRectGetMaxX(self.view.bounds), 10)];
road.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];
[self.view addSubview:road];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lightup:) name:@”light up” object:nil];
// car
UIView *car = [[UIView alloc] initWithFrame:CGRectMake(50, 180, 30, 15)];
car.backgroundColor = [UIColor redColor];
[self.view addSubview:car];
for (int i=0; i<2; i++) {
UIView *tire = [[UIView alloc] initWithFrame:CGRectMake(i ? 0 : 20, 10, 10, 10)];
tire.backgroundColor = [UIColor lightGrayColor];
tire.layer.cornerRadius = 5;
[car addSubview:tire];
}
self.mycar = car;
}
– (void)createLight:(CGPoint)pt {
float r = 50;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(-r, 100)];
[path addQuadCurveToPoint:CGPointMake(r, 100) controlPoint:CGPointMake(0, 140)];
[path closePath];
CAShapeLayer *light = [CAShapeLayer layer];
light.path = path.CGPath;
light.fillColor = [[UIColor yellowColor] colorWithAlphaComponent:0.3].CGColor;
light.position = pt;
[self.view.layer addSublayer:light];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *arr = [NSMutableArray array];
for (UIView *v in self.view.subviews) {
if (v.tag != 0) {
[arr addObject:@(v.tag)];
}
}
shared_ptr<IntervalPartitioningSolver> cppClass(new IntervalPartitioningSolver());
cppClass->solve(arr);
}
– (void)lightup:(NSNotification *)note {
int tag = [note.object intValue];
UIView *v = [self.view viewWithTag:tag];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.counter++ * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self createLight:v.center];
[UIView animateWithDuration:1.0 animations:^{
self.mycar.center = CGPointMake(v.center.x + 40, self.mycar.center.y);
}];
});
}
@end