Commit 60780150 authored by Pietro Saccardi's avatar Pietro Saccardi

Randomizing background generation

parent 8efba452
......@@ -21,13 +21,17 @@ function paintTree() {
const TRUNK_COL = '#444' //'#7fc0c2'
const RAY_COL = '#fff' //'#fff'
const ROOT_COL = '#444' //'#222'
const LBLS_PER_FRAME = 1
const graph = $('#graph')
const W = graph.width()
const H = graph.height()
const N_X = Math.max(10, Math.floor(W / GRID_UNIT)) - 1
const N_Y = Math.max(10, Math.floor(H / GRID_UNIT)) - 1
console.log(`Grid graph: ${N_X}×${N_Y}`)
const LBLS_PER_FRAME = N_X * N_Y / 250
const embed = pos => [(W - N_X * GRID_UNIT) / 2 + pos[0] * GRID_UNIT, (H - N_Y * GRID_UNIT) / 2 + pos[1] * GRID_UNIT]
let edges = {}
......@@ -39,7 +43,22 @@ function paintTree() {
const grid = new GridGraph(N_X, N_Y)
function node_feasibility(node) {
return 1.;
let pos = grid.getNodeXY(node)
pos[0] /= N_X
pos[1] /= N_Y
const center = [0.5, 0.3]
const radius = 0.2
const falloff = 0.3
const exp = 0.8
const rand_fraction = 0.3
// Actual math:
const dx = center[0] - pos[0]
const dy = center[1] - pos[1]
const l2 = Math.sqrt(dx * dx + dy * dy)
const unit_l2 = Math.max(Math.min((l2 - radius) / falloff, 1.), 0.)
const l2_pow_law = Math.pow(unit_l2, exp)
const rand_lerp = l2_pow_law * (1. - rand_fraction) + rand_fraction * Math.random()
return rand_lerp
}
// Generate nodes and arcs
......@@ -62,18 +81,15 @@ function paintTree() {
grid.directions.forEach(dir => dir_l2_len[dir2idx(dir)] = Math.sqrt(dir[0] * dir[0] + dir[1] * dir[1]))
const RANDOM_EDGE_COST_WEIGHT = 0.5
const NODE_WEIGHT = 10.
function edge_cost(dijkstra, node, edge) {
const dir = grid.getEdgeDirection(edge)
// base l2 cost
let cost = dir_l2_len[dir2idx(dir)]
const l2 = dir_l2_len[dir2idx(dir)]
const node_weight = 5.
const rand_fraction = 0.6
// Add a random component
cost += RANDOM_EDGE_COST_WEIGHT * Math.random()
// Node component. This is really not symmetric, but ok
cost /= NODE_WEIGHT * node_feasibility(node)
return cost
const artificial_cost = (1. + (Math.random() - 1.) * rand_fraction) * node_weight * (1. - node_feasibility(node))
return l2 + artificial_cost
}
let algo = new Dijkstra(grid, edge_cost, {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment