mercoledì 29 gennaio 2014

Mandelbrot su IPhone/IOS

Per disegnare Mandelbrot su un Iphone per prima cosa si deve trovare una superficie su cui disegnare.
Scartata l'ipotesi di OpenGL (troppo complicata), la cosa piu' semplice e' utilizzare la libreria Quartz 2D facendo un subclass di UIView (per il dettaglio seguire questo link)




Questo puo' essere effettuato con i seguenti comandi (partendo da un progetto vuoto con View-Based Application)
File -> Add Files to 
si seleziona Objective-C Class e in basso si seleziona SubClass of UIView
Si seleziona un nome per la nuova classe e si conclude

In seguito dall'Interface Builder (il creatore di interfacce visuali) si seleziona tutta la finestra (UIView), si preme Command+4 e si modifica la classe da UIView con il nome precedentemente selezionato

Fatto cio' si trova che nel file miaclasse.m un scheletro vuoto di una funzione drawRect.Qui si puo' inserire il codice per la generazione dell'insieme di Mandelbrot

------------------------------------------------------------
//
//  Draw2D.m
//  Mand
//
//  Created by Luca Innocenti on 27/01/14.
//  Copyright (c) 2014 Luca Innocenti. All rights reserved.
//

#import "Draw2D.h"

@implementation Draw2D

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    
    int SCREEN_WIDTH = 480;
    int SCREEN_HEIGHT = 320;
    
    float re_min = -2.0;
    float im_min = -1.2;
    float re_max = 1.0;
    float im_max = 1.2;
    int iterazioni = 60;
    float a,b;
    float x,y,x_new,y_new;
    int test;
    int k,j,i;
    
    float re_factor = (re_max-re_min);
    float im_factor = (im_max-im_min);
    for (i=0;i<SCREEN_HEIGHT;i++)
    {
        for (j=0;j<SCREEN_WIDTH;j++)
        {
            a = re_min+(j*re_factor/SCREEN_WIDTH);
            b = im_min+(i*im_factor/SCREEN_HEIGHT);
            x = 0;
            y = 0;
            test = 0;
            for (k=0;k<iterazioni;k++)
            {
                x_new = (x*x)-(y*y)+a;
                y_new = (2*x*y)+b;
                if (((x_new*x_new)+(y_new*y_new))>4)
                {
                    test = k;
                    if (k%2)
                    {
                        CGContextMoveToPoint(context, i, j);
                        CGContextAddLineToPoint(context, i+1,j);
                        CGContextStrokePath(context);
                    }
                    break;
                }
                x = x_new;
                y = y_new;
            }
        }
    }
}

@end
--------------------------------------------------------------


Nessun commento:

Posta un commento

Frane con rete DeepLabV3

Aggiornamento Per usare la GPU T4 sono migrato su Google Colab Per avere Keras 3 sono state fatte le seguenti modifiche ! pip install keras ...