Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tweetable Mathematical Art challenge - ObjC/iOS Implementation
#import "MCViewController.h"
#define DIM 1024
#define DM1 (DIM-1)
#define _sq(x) ((x)*(x)) // square
#define _cb(x) abs((x)*(x)*(x)) // absolute value of cube
#define _cr(x) (unsigned short)(pow((x),1.0/3.0)) // cube root
@interface MCViewController ()
@end
@implementation MCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(DIM*2,DIM*2), NO, 0.0);
for (int i = 0; i <= DIM*2; i++)
{
for (int j = 0; j <= DIM*2; j++)
{
UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(i, j, 1, 1)];
[[UIColor colorWithRed:red_fn(i, j)/255.0
green:green_fn(i, j)/255.0
blue:blue_fn(i, j)/255.0
alpha:1] setFill];
[rect fill];
}
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *v = [[UIImageView alloc] initWithImage:newImage];
v.contentMode = UIViewContentModeScaleAspectFit;
[v setFrame:CGRectMake(0, 0, 768, 768)];
[v setCenter:self.view.center];
[self.view setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:v];
}
unsigned short red_fn(int i,int j)
{
return j^j-i^i;
}
unsigned short green_fn(int i,int j)
{
return (i-DIM)^2+(j-DIM)^2;
}
unsigned short blue_fn(int i,int j)
{
return i^i-j^j;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment