xxxxxxxxxx
128
let panel_size_px = 512
let panel_size = 5
let cell_size
let path = []
let tracing = true
const Directions = {
'UP': 0,
'RIGHT': 1,
'DOWN': 2,
'LEFT': 3
}
function PathNode(row, col, entry = '', exit = '') {
this.row = row
this.col = col
this.entry = entry
this.exit = exit
}
function setup() {
createCanvas(600, 600);
cell_size = panel_size_px / panel_size
path.push(new PathNode(0, 0))
}
function drawPanel(x_offset, y_offset) {
drawNodes(x_offset, y_offset)
drawPath(path, x_offset, y_offset)
}
function drawNodes(x_offset, y_offset) {
forCellInPanel(panel_size, (row, col) => {
let x = col * cell_size + x_offset
let y = row * cell_size + y_offset
ellipse(x, y, cell_size/4)
})
}
function drawPath(path, x_offset, y_offset) {
updatePath(path, x_offset, y_offset)
for (let path_index = 0; path_index < path.length - 1; path_index++) {
let start_row = path[path_index + 0].row
let start_col = path[path_index + 0].col
let end_row = path[path_index + 1].row
let end_col = path[path_index + 1].col
let s_y = start_row * cell_size + y_offset
let s_x = start_col * cell_size + x_offset
let e_y = end_row * cell_size + y_offset
let e_x = end_col * cell_size + x_offset
line(s_x, s_y, e_x, e_y)
}
}
function updatePath(path, x_offset, y_offset) {
if (tracing) {
const last_node = path[path.length -1]
if (last_node) {
let x = last_node.col * cell_size + x_offset
let y = last_node.row * cell_size + y_offset
let dx = mouseX - x
let dy = mouseY - y
let new_dir
// More up
if (dy < 0) {
if (Math.abs(dx) > Math.abs(dy)) {
if (dx < 0) {
new_dir = Directions.LEFT
}
else {
new_dir = Directions.RIGHT
}
}
else
new_dir = Directions.UP
}
// More down
else {
if (Math.abs(dx) > Math.abs(dy)) {
if (dx < 0) {
new_dir = Directions.LEFT
}
else {
new_dir = Directions.RIGHT
}
}
else
new_dir = Directions.DOWN
}
console.log(new_dir)
}
}
}
function draw() {
background(220);
drawPanel(cell_size/2, cell_size/2)
}
function forCellInPanel(panel_size, callback) {
for (let row = 0; row < panel_size; row++) {
for (let col = 0; col < panel_size; col++) {
callback(row, col)
}
}
}