Skip to content

Commit f1a9b7f

Browse files
committed
add simple magnification and back button
1 parent 9879444 commit f1a9b7f

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

iMandelbrot/USKFractalView.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
@interface USKFractalView : UIImageView
1212

13+
- (void)drawMandelbrotSet;
1314
- (void)drawMandelbrotSetInComplexRect:(CGRect)cRect;
1415

1516
@end

iMandelbrot/USKFractalView.m

+22-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@implementation USKFractalView {
1313
size_t width, height;
14+
CGRect currentCRect;
1415
}
1516

1617
- (id)initWithFrame:(CGRect)frame
@@ -20,13 +21,21 @@ - (id)initWithFrame:(CGRect)frame
2021
// Initialization code
2122
self.contentMode = UIViewContentModeScaleAspectFit;
2223
self.userInteractionEnabled = YES;
23-
[self drawMandelbrotSetInComplexRect:CGRectMake(-2.0, -2.0, 4.0, 4.0)];
24+
[self drawMandelbrotSet];
2425
}
2526
return self;
2627
}
2728

29+
- (void)drawMandelbrotSet
30+
{
31+
CGRect complexRect = CGRectMake(-2.0, -2.0, 4.0, 4.0);
32+
[self drawMandelbrotSetInComplexRect:complexRect];
33+
}
34+
2835
- (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
2936
{
37+
currentCRect = cRect;
38+
3039
// Draw Mandelbrot Set
3140
NSUInteger maxIteration = 100;
3241
NSUInteger iteration;
@@ -41,7 +50,7 @@ - (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
4150

4251
for (NSUInteger i = 0; i < height; i++) {
4352
for (NSUInteger j = 0; j < width; j++) {
44-
double _Complex c = cRect.size.width * j / width + cRect.origin.x + (cRect.size.height * i / height + cRect.origin.y) * I;
53+
double _Complex c = currentCRect.size.width * j / width + currentCRect.origin.x + (currentCRect.size.height * i / height + currentCRect.origin.y) * I;
4554
double _Complex z = z0;
4655
for (iteration = 0; iteration < maxIteration; iteration++) {
4756
z = z * z + c;
@@ -99,10 +108,19 @@ - (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
99108
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
100109
{
101110
CGPoint p = [[touches anyObject] locationInView:self];
102-
printf("p = (%f, %f)\tc = (%f, %f)\n", p.x, p.y, p.x / width - 0.5, p.y / height - 0.5);
103-
[self drawMandelbrotSetInComplexRect:CGRectMake(p.x / width - 0.5, p.y / height - 0.5, 1.0, 1.0)];
111+
CGPoint relativeP = CGPointMake(p.x / self.frame.size.width, p.y / self.frame.size.height);
112+
double magnification = 3.0;
113+
CGPoint complexP = CGPointMake(currentCRect.origin.x + currentCRect.size.width * relativeP.x,
114+
currentCRect.origin.y + currentCRect.size.height * relativeP.y);
115+
CGSize newSize = CGSizeMake(currentCRect.size.width / magnification, currentCRect.size.height / magnification);
116+
[self drawMandelbrotSetInComplexRect:CGRectMake(complexP.x - newSize.width / 2.0,
117+
complexP.y - newSize.height / 2.0,
118+
newSize.width,
119+
newSize.height)];
104120
}
105121

122+
123+
106124
/*
107125
// Only override drawRect: if you perform custom drawing.
108126
// An empty implementation adversely affects performance during animation.

iMandelbrot/USKViewController.m

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ - (void)viewDidLoad
2424
iv.center = self.view.center;
2525
[self.view addSubview:iv];
2626
self.view.backgroundColor = [UIColor whiteColor];
27+
28+
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
29+
[backButton addTarget:iv action:@selector(drawMandelbrotSet) forControlEvents:UIControlEventTouchUpInside];
30+
backButton.frame = CGRectMake(20, [[UIScreen mainScreen] bounds].size.height - 108, [[UIScreen mainScreen] bounds].size.width - 40, 88);
31+
[backButton setTitle:@"Back" forState:UIControlStateNormal];
32+
[self.view addSubview:backButton];
2733
}
2834

2935
- (void)didReceiveMemoryWarning

0 commit comments

Comments
 (0)