更新情報
広告
PRサイト
関連サイト

原図の高さと幅をランダムに選ぶ


インスタンス変数を追加する

原図の幅と高さの最大値と最小値を表すインスタンス変数を追加します。ただしp3m1とp6の場合、他と同じサイズの原図を使うと非常に細かい紋様になってしまうので、別扱いにする事にします。末尾に2がついているインスタンス変数はp3m1とp6用です。

Randomizer.h
@class  MasterMotif;

@interface Randomizer : NSObject
{
    int     amplitudeMinX1,amplitudeMinX2,amplitudeMinX3;
    int     amplitudeMaxX1,amplitudeMaxX2,amplitudeMaxX3;
    int     amplitudeMinY1,amplitudeMinY2,amplitudeMinY3;
    int     amplitudeMaxY1,amplitudeMaxY2,amplitudeMaxY3;

    int     frequencyMinX1,frequencyMinX2,frequencyMinX3;
    int     frequencyMaxX1,frequencyMaxX2,frequencyMaxX3;
    int     frequencyMinY1,frequencyMinY2,frequencyMinY3;
    int     frequencyMaxY1,frequencyMaxY2,frequencyMaxY3;
    
    RepeatingMotif      *repeatingMotif;
    RepeatingMotifLayer *layer;

    BOOL    p1,p2,pm,pg,cm,pmm,pmg,pgg,cmm;     
    BOOL    p4,p4m,p4g,p3,p3m1,p31m,p6,p6m;
    
    int     masterWidthMin,masterWidthMax;
    int     masterHeightMin,masterHeightMax;
    int     masterWidthMin2,masterWidthMax2;
    int     masterHeightMin2,masterHeightMax2;
    
    NSArray     *colorInfoArray;
}

+ (id)sharedRandomizer;

- (void)randomizeMasterMotif:(MasterMotif *)masterMotif
             withUndoManager:(NSUndoManager *)undoManager
               lockFrequency:(BOOL)isLockFrequency;

@end

環境設定では以下の部分で設定します。

preference width height


追加したインスタンス変数をバインドする

追加したインスタンス変数は環境設定でユーザが設定するものなので、初期化メソッドでデフォルトデータベースとバインドしておきます。

Randomizer.m
- (id)init
{
    self = [super init];
    if(self)
    {
        srandom(time(NULL));

        NSString    *fileName =
            [[NSBundle mainBundle] pathForResource:@"JISColor" 
                                            ofType:@"plist"];
        colorInfoArray = 
            [[NSArray arrayWithContentsOfFile:fileName] retain];

        [self bindWithName:@"amplitudeMinX1"];
        [self bindWithName:@"amplitudeMinX2"];
        [self bindWithName:@"amplitudeMinX3"];
        [self bindWithName:@"amplitudeMaxX1"];
        [self bindWithName:@"amplitudeMaxX2"];
        [self bindWithName:@"amplitudeMaxX3"];
        
        [self bindWithName:@"amplitudeMinY1"];
        [self bindWithName:@"amplitudeMinY2"];
        [self bindWithName:@"amplitudeMinY3"];
        [self bindWithName:@"amplitudeMaxY1"];
        [self bindWithName:@"amplitudeMaxY2"];
        [self bindWithName:@"amplitudeMaxY3"];
        
        [self bindWithName:@"frequencyMinX1"];
        [self bindWithName:@"frequencyMinX2"];
        [self bindWithName:@"frequencyMinX3"];
        [self bindWithName:@"frequencyMaxX1"];
        [self bindWithName:@"frequencyMaxX2"];
        [self bindWithName:@"frequencyMaxX3"];
        
        [self bindWithName:@"frequencyMinY1"];
        [self bindWithName:@"frequencyMinY2"];
        [self bindWithName:@"frequencyMinY3"];
        [self bindWithName:@"frequencyMaxY1"];
        [self bindWithName:@"frequencyMaxY2"];
        [self bindWithName:@"frequencyMaxY3"];
        
        [self bindWithName:@"p1"];
        [self bindWithName:@"p2"];
        [self bindWithName:@"pm"];
        [self bindWithName:@"pg"];
        [self bindWithName:@"cm"];
        [self bindWithName:@"pmm"];
        [self bindWithName:@"pmg"];
        [self bindWithName:@"pgg"];
        [self bindWithName:@"cmm"];
        [self bindWithName:@"p4"];
        [self bindWithName:@"p4m"];
        [self bindWithName:@"p4g"];
        [self bindWithName:@"p3"];
        [self bindWithName:@"p3m1"];
        [self bindWithName:@"p31m"];
        [self bindWithName:@"p6"];
        [self bindWithName:@"p6m"];
        
        [self bindWithName:@"masterWidthMin"];
        [self bindWithName:@"masterWidthMax"];
        [self bindWithName:@"masterHeightMin"];
        [self bindWithName:@"masterHeightMax"];
        [self bindWithName:@"masterWidthMin2"];
        [self bindWithName:@"masterWidthMax2"];
        [self bindWithName:@"masterHeightMin2"];
        [self bindWithName:@"masterHeightMax2"];
    }
    return self;
}


原図の高さと幅をランダムに選んでセットする

環境設定では10刻みで数値が変化する様にしてあるので、追加したインスタンス変数に入る数値は必ず10の倍数になります。そこで、それを10で割った整数を乱数の変動範囲として、最後に10をかける事で、10の倍数の乱数が得られます。

symmetryTypeIndexが13の場合はp3m1、15の場合はp6です。

Randomizer.m
- (void)randomizeMasterWidthAndMasterHeight
{
    int     masterWidth,masterHeight;
    
    if(
       [repeatingMotif symmetryTypeIndex] == 13 ||
       [repeatingMotif symmetryTypeIndex] == 15
       )
    {
        masterWidth = 10*[self randomIntBetweenA:masterWidthMin2/10
                                            andB:masterWidthMax2/10];
        masterHeight = 10*[self randomIntBetweenA:masterHeightMin2/10
                                             andB:masterHeightMax2/10];
    }
    else
    {
        masterWidth = 10*[self randomIntBetweenA:masterWidthMin/10
                                            andB:masterWidthMax/10];
        masterHeight = 10*[self randomIntBetweenA:masterHeightMin/10
                                             andB:masterHeightMax/10];
    }
    [repeatingMotif setMasterWidth:masterWidth];
    [repeatingMotif setMasterHeight:masterHeight];
}


ページの先頭へ戻る