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

その他のパラメータをランダムに選ぶ


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

その他のパラメータをまとめて追加します。

  • strokeWidthMin:線の太さの最小値
  • strokeWidthMax:線の太さの最大値
  • scaleWidthMin:横倍率の最小値
  • scaleWidthMax:横倍率の最大値
  • scaleHeightMin:縦倍率の最小値
  • scaleHeightMax:縦倍率の最大値
  • lineJoinStyle:線の接続方法
  • windingRule:内側と外側の判定方法
  • strokeOption:線の有無
  • fillOption:塗りつぶしの有無
  • opacityMin:不透明度の最小値
  • opacityMax:不透明度の最大値
  • moreOpaque:下のレイヤーほど不透明
  • zeroLineWidth:線なしの場合、太さをゼロにする
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;
    
    int     strokeWidthMin,strokeWidthMax;
    int     scaleWidthMin,scaleWidthMax,scaleHeightMin,scaleHeightMax;
    int     lineJoinStyle,windingRule,strokeOption,fillOption;
    float   opacityMin,opacityMax;
    BOOL    moreOpaque,zeroLineWidth;
    
    NSArray     *colorInfoArray;
}

+ (id)sharedRandomizer;

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

@end

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

preference other


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

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

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"];
        
        [self bindWithName:@"strokeWidthMin"];
        [self bindWithName:@"strokeWidthMax"];
        [self bindWithName:@"scaleWidthMin"];
        [self bindWithName:@"scaleWidthMax"];
        [self bindWithName:@"scaleHeightMin"];
        [self bindWithName:@"scaleHeightMax"];
        
        [self bindWithName:@"lineJoinStyle"];
        [self bindWithName:@"windingRule"];
        [self bindWithName:@"strokeOption"];
        [self bindWithName:@"fillOption"];
        
        [self bindWithName:@"opacityMin"];
        [self bindWithName:@"opacityMax"];
        
        [self bindWithName:@"moreOpaque"];
        [self bindWithName:@"zeroLineWidth"];
    }
    return self;
}

倍率をランダムに選んでセットする

倍率の上限、下限に応じた乱数を発生させて、設定しています。

Randomizer.m
- (void)randomizeScale
{
    [layer setScaleWidth:[self randomIntBetweenA:scaleWidthMin
                                            andB:scaleWidthMax]];
    [layer setScaleHeight:[self randomIntBetweenA:scaleHeightMin
                                             andB:scaleHeightMax]];
}

線の接続方法をランダムに選んでセットする

lineJoinStyleが3という事は「ランダム」が選ばれたという事なので、乱数で接続方法を決定します。それ以外は選ばれたものをそのまま設定します。

Randomizer.m
- (void)randomizeLineJoinStyle
{
    if(lineJoinStyle == 3)
        [layer setLineJoinStyle:[self randomIntBetweenA:0 andB:2]];
    else
        [layer setLineJoinStyle:lineJoinStyle];
}

内側と外側の判定方法をランダムに選んでセットする

windingRuleが2という事は「ランダム」が選ばれたという事なので、乱数で内側と外側の判定方法を決定します。それ以外は選ばれたものをそのまま設定します。

Randomizer.m
- (void)randomizeWindingRule
{
    if(windingRule == 2)
        [layer setWindingRule:[self randomIntBetweenA:0 andB:1]];
    else
        [layer setWindingRule:windingRule];
}

線の有無をランダムに選んでセットする

strokeOptionが2という事は「ランダム」が選ばれたという事なので、乱数でYES,NOを決定します。それ以外は選ばれたものをそのまま設定します。

Randomizer.m
- (void)randomizeStroke
{
    switch(strokeOption)
    {
        case 0:[layer setStroke:YES];   break;
        case 1:[layer setStroke:NO];    break;
        case 2:[layer setStroke:[self randomBool]];
    }
}


塗りつぶしの有無をランダムに選んでセットする

fillOptionが2という事は「ランダム」が選ばれたという事なので、乱数でYES,NOを決定します。それ以外は選ばれたものをそのまま設定します。ただし線がなしの時に塗りつぶしもなしにすると何も描画しなくなってしまうので、線がなしの時は強制的に塗りつぶしありにしています。

Randomizer.m
- (void)randomizeFill
{
    if([layer stroke])
    {
        switch(fillOption)
        {
            case 0:[layer setFill:YES]; break;
            case 1:[layer setFill:NO];  break;
            case 2:[layer setFill:[self randomBool]];
        }
    }
    else
        [layer setFill:YES];
}

線の太さをランダムに選んでセットする

線の太さの上限、下限に応じた乱数を発生させて、設定しています。ただし「線なしの場合、太さをゼロにする」がYESで、かつ線なしの場合は太さを強制的にゼロにしています。

Randomizer.m
- (void)randomizeStrokeWidth
{
    if(zeroLineWidth && ![layer stroke])
    {
        [layer setStrokeWidth:0];
    }
    else
    {
        [layer setStrokeWidth:
            [self randomIntBetweenA:strokeWidthMin
                               andB:strokeWidthMax]];
    }
}

不透明度をランダムに選んでセットする

不透明度の上限、下限に応じた乱数を発生させて、設定しています。ただし「下のレイヤーほど不透明」がYESの場合は何番目のレイヤーなのかに応じて上限、下限の値を変化させています。

Randomizer.m
- (void)randomizeOpacity
{
    if(moreOpaque)
    {
        NSArray *layers = [repeatingMotif repeatingMotifLayers];
        int     index = [layers indexOfObject:layer];
        int     count = [layers count];
        [layer setOpacity:
            [self randomFloatBetweenA:opacityMin +
                (count-index-1)*(opacityMax-opacityMin)/(float)count
                                 andB:opacityMin +
                (count-index)*(opacityMax-opacityMin)/(float)count]];
    }
    else
        [layer setOpacity:
            [self randomFloatBetweenA:opacityMin
                                 andB:opacityMax]];
}


ページの先頭へ戻る