Skip to content

Commit

Permalink
グラフ構造のランダムルール生成処理を作成
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuyoshi-yamazaki committed Aug 31, 2023
1 parent 45244ea commit 1958786
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
59 changes: 54 additions & 5 deletions src/simulations/drawer/random_rule_constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,70 @@ export const RandomRuleConstructor = {
},

graph(): VanillaLSystemRule {
const conditions = alphabets.slice(0, random(alphabets.length, 1))
const allPossibleConditions = [...alphabets]
const unusedConditions = new Set(allPossibleConditions)

const loopConditions = alphabets.slice(0, random(12, 2))
loopConditions.forEach(condition => {
unusedConditions.delete(condition)
})

const branchConditions: string[] = []
const map = new Map<string, LSystemCondition[]>()

conditions.forEach((condition, index) => {
const randomCondition = (conditions: string[]): string => {
const index = Math.floor(random(conditions.length))

return conditions[index]
}

loopConditions.forEach((condition, index) => {
const nextConditions: LSystemCondition[] = []
const angle = Math.floor(random(360, 0)) - 180
nextConditions.push(angle)

nextConditions.push(angle)
const destinationCondition = conditions[(index + 1) % conditions.length]
const destinationCondition = loopConditions[(index + 1) % loopConditions.length]
nextConditions.push(destinationCondition)

for (let i = 0; i < 10; i += 1) {
if (random(1) > 0.7) {
break
}
const branchAngle = Math.floor(random(360, 0)) - 180
nextConditions.push(branchAngle)

const branchCondition = randomCondition(allPossibleConditions)
if (unusedConditions.has(branchCondition) === true) {
unusedConditions.delete(branchCondition)
branchConditions.push(branchCondition)
}
nextConditions.push(branchCondition)
}

map.set(condition, nextConditions)
})

const usedConditions = [
...loopConditions,
...branchConditions,
]
branchConditions.forEach(condition => { // 枝分かれ中にのみ登場する条件がない
const nextConditions: LSystemCondition[] = []
for (let i = 0; i < 10; i += 1) {
if (random(1) > 0.5) {
break
}
const angle = Math.floor(random(360, 0)) - 180
nextConditions.push(angle)
nextConditions.push(randomCondition(usedConditions))
}
if (nextConditions.length === 0) {
nextConditions.push(VanillaLSystemRule.endOfBranch)
}

map.set(condition, nextConditions)
})

return new VanillaLSystemRule(map)
},
}
}
1 change: 0 additions & 1 deletion src/simulations/drawer/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function createModel(ruleString?: string): Model {
const tries = 20
for (let j = 0; j < tries; j += 1) {
const rule = VanillaLSystemRule.trimUnreachableConditions(RandomRuleConstructor.graph(), initialCondition)
console.log("graph")
if (rule.isCirculated(initialCondition)) {
rules.push(rule)
break
Expand Down

0 comments on commit 1958786

Please sign in to comment.