Skip to content

Commit

Permalink
ランダムルール生成器を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuyoshi-yamazaki committed Aug 15, 2023
1 parent 781979c commit c2fe78b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
37 changes: 37 additions & 0 deletions src/simulations/drawer/random_rule_constructor.ts
Original file line number Diff line number Diff line change
@@ -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<string, LSystemCondition[]>()
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)
}
}
3 changes: 2 additions & 1 deletion src/simulations/drawer/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
36 changes: 2 additions & 34 deletions src/simulations/drawer/vanilla_lsystem_rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { random } from "../../classes/utilities"
import { Color } from "../../classes/color"
import { LSystemRule, LSystemCondition, LSystemCoordinate } from "./lsystem_rule"

Expand Down Expand Up @@ -50,7 +49,8 @@ export class VanillaLSystemRule implements LSystemRule {
return this._encoded
}

private static endOfBranch = "."
public static endOfBranch = "."

private _encoded: string
private _map: Map<string, LSystemCondition[]>

Expand All @@ -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<string, LSystemCondition[]>()
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, LSystemCondition[]>): string {
const result: string[] = []
map.forEach((value, key) => {
Expand Down

0 comments on commit c2fe78b

Please sign in to comment.