diff --git a/src/simulations/drawer/random_rule_constructor.ts b/src/simulations/drawer/random_rule_constructor.ts new file mode 100644 index 00000000..8b4c41cf --- /dev/null +++ b/src/simulations/drawer/random_rule_constructor.ts @@ -0,0 +1,37 @@ +import { random } from "../../classes/utilities" +import { LSystemCondition } from "./lsystem_rule" +import { VanillaLSystemRule } from "./vanilla_lsystem_rule" + +export const RandomRuleConstructor = { + random(): VanillaLSystemRule { + const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("") + const conditions = alphabets.slice(0, random(alphabets.length, 1)) + const map = new Map() + const maxConditions = 10 + + const randomCondition = (): string => { + const index = Math.floor(random(conditions.length)) + + return conditions[index] + } + + conditions.forEach(condition => { + const nextConditions: LSystemCondition[] = [] + for (let i = 0; i < maxConditions; i += 1) { + if (random(1) > 0.5) { + break + } + const angle = Math.floor(random(360, 0)) - 180 + nextConditions.push(angle) + nextConditions.push(randomCondition()) + } + if (nextConditions.length === 0) { + nextConditions.push(VanillaLSystemRule.endOfBranch) + } + + map.set(condition, nextConditions) + }) + + return new VanillaLSystemRule(map) + } +} \ No newline at end of file diff --git a/src/simulations/drawer/source.ts b/src/simulations/drawer/source.ts index 6f524bc5..5777b840 100644 --- a/src/simulations/drawer/source.ts +++ b/src/simulations/drawer/source.ts @@ -8,6 +8,7 @@ import { VanillaLSystemRule } from "./vanilla_lsystem_rule" import { exampleRules } from "./rule_examples" import { Downloader } from "./downloader" import { TransitionColoredModel } from "./transition_colored_model" +import { RandomRuleConstructor } from "./random_rule_constructor" let t = 0 const canvasId = "canvas" @@ -99,7 +100,7 @@ function createModel(ruleString?: string): Model { for (let i = 0; i < constants.simulation.numberOfSeeds; i += 1) { const tries = 20 for (let j = 0; j < tries; j += 1) { - const rule = VanillaLSystemRule.trimUnreachableConditions(VanillaLSystemRule.random(), initialCondition) + const rule = VanillaLSystemRule.trimUnreachableConditions(RandomRuleConstructor.random(), initialCondition) if (rule.isCirculated(initialCondition)) { rules.push(rule) break diff --git a/src/simulations/drawer/vanilla_lsystem_rule.ts b/src/simulations/drawer/vanilla_lsystem_rule.ts index 96ea7636..2ed96ef0 100644 --- a/src/simulations/drawer/vanilla_lsystem_rule.ts +++ b/src/simulations/drawer/vanilla_lsystem_rule.ts @@ -1,4 +1,3 @@ -import { random } from "../../classes/utilities" import { Color } from "../../classes/color" import { LSystemRule, LSystemCondition, LSystemCoordinate } from "./lsystem_rule" @@ -50,7 +49,8 @@ export class VanillaLSystemRule implements LSystemRule { return this._encoded } - private static endOfBranch = "." + public static endOfBranch = "." + private _encoded: string private _map: Map @@ -76,38 +76,6 @@ export class VanillaLSystemRule implements LSystemRule { this.transition = this.calculateTransition() } - public static random(): VanillaLSystemRule { // FixMe: 適当に書いたので探索範囲が偏っているはず - const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("") - const conditions = alphabets.slice(0, random(alphabets.length, 1)) - const map = new Map() - const maxConditions = 10 - - const randomCondition = (): string => { - const index = Math.floor(random(conditions.length)) - - return conditions[index] - } - - conditions.forEach(condition => { - const nextConditions: LSystemCondition[] = [] - for (let i = 0; i < maxConditions; i += 1) { - if (random(1) > 0.5) { - break - } - const angle = Math.floor(random(360, 0)) - 180 - nextConditions.push(angle) - nextConditions.push(randomCondition()) - } - if (nextConditions.length === 0) { - nextConditions.push(VanillaLSystemRule.endOfBranch) - } - - map.set(condition, nextConditions) - }) - - return new VanillaLSystemRule(map) - } - public static encode(map: Map): string { const result: string[] = [] map.forEach((value, key) => {