ref: a260fe0b8eb106f6c9cd8dd7c9b1c63d8cef8c1f
parent: 7aef7a1b25479f97f3003cfca573053e636043b9
author: qwx <qwx@sciops.net>
date: Sat Mar 26 05:37:06 EDT 2022
path/a∗,dijkstra: prefer cardinal directions and swap goal/start mouse button
--- a/path/a∗.c
+++ b/path/a∗.c
@@ -28,9 +28,8 @@
{
static Node *suc[8+1];
static dtab[2*(nelem(suc)-1)]={
- -1,-1, 0,-1, 1,-1,
- -1,0, 1,0,
- -1,1, 0,1, 1,1,
+ 0,-1, 1,0, 0,1, -1,0,
+ 1,-1, 1,1, -1,1, -1,-1,
};
int i;
Node *s, **np;
@@ -48,6 +47,7 @@
if(isblocked(s))
continue;
s->Δg = 1;
+ //s->Δg = dtab[i] != 0 && dtab[i+1] != 0 ? SQRT2 : 1;
*np++ = s;
}
return suc;
@@ -141,8 +141,8 @@
mouseinput(Node *n, Mouse m)
{
switch(m.buttons & 7){
- case 1: if(start != n) goal = n; return findpath();
- case 2: if(goal != n) start = n; return findpath();
+ case 1: if(goal != n) start = n; return findpath();
+ case 2: if(start != n) goal = n; return findpath();
case 4: n->blocked ^= 1; break;
}
return 0;
--- a/path/dijkstra.c
+++ b/path/dijkstra.c
@@ -27,9 +27,8 @@
{
static Node *suc[8+1];
static dtab[2*(nelem(suc)-1)]={
- -1,-1, 0,-1, 1,-1,
- -1,0, 1,0,
- -1,1, 0,1, 1,1,
+ 0,-1, 1,0, 0,1, -1,0,
+ 1,-1, 1,1, -1,1, -1,-1,
};
int i;
Node *s, **np;
@@ -139,8 +138,8 @@
mouseinput(Node *n, Mouse m)
{
switch(m.buttons & 7){
- case 1: if(start != n) goal = n; return findpath();
- case 2: if(goal != n) start = n; return findpath();
+ case 1: if(goal != n) start = n; return findpath();
+ case 2: if(start != n) goal = n; return findpath();
case 4: n->blocked ^= 1; break;
}
return 0;