11
11
12
12
@implementation USKFractalView {
13
13
size_t width, height;
14
+ CGRect currentCRect;
14
15
}
15
16
16
17
- (id )initWithFrame : (CGRect )frame
@@ -20,13 +21,21 @@ - (id)initWithFrame:(CGRect)frame
20
21
// Initialization code
21
22
self.contentMode = UIViewContentModeScaleAspectFit;
22
23
self.userInteractionEnabled = YES ;
23
- [self drawMandelbrotSetInComplexRect: CGRectMake (- 2.0 , - 2.0 , 4.0 , 4.0 ) ];
24
+ [self drawMandelbrotSet ];
24
25
}
25
26
return self;
26
27
}
27
28
29
+ - (void )drawMandelbrotSet
30
+ {
31
+ CGRect complexRect = CGRectMake (-2.0 , -2.0 , 4.0 , 4.0 );
32
+ [self drawMandelbrotSetInComplexRect: complexRect];
33
+ }
34
+
28
35
- (void )drawMandelbrotSetInComplexRect : (CGRect )cRect
29
36
{
37
+ currentCRect = cRect;
38
+
30
39
// Draw Mandelbrot Set
31
40
NSUInteger maxIteration = 100 ;
32
41
NSUInteger iteration;
@@ -41,7 +50,7 @@ - (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
41
50
42
51
for (NSUInteger i = 0 ; i < height; i++) {
43
52
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;
45
54
double _Complex z = z0;
46
55
for (iteration = 0 ; iteration < maxIteration; iteration++) {
47
56
z = z * z + c;
@@ -99,10 +108,19 @@ - (void)drawMandelbrotSetInComplexRect:(CGRect)cRect
99
108
- (void )touchesBegan : (NSSet *)touches withEvent : (UIEvent *)event
100
109
{
101
110
CGPoint p = [[touches anyObject ] locationInView: self ];
102
- printf (" p = (%f , %f )\t c = (%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)];
104
120
}
105
121
122
+
123
+
106
124
/*
107
125
// Only override drawRect: if you perform custom drawing.
108
126
// An empty implementation adversely affects performance during animation.
0 commit comments