惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

CodeBlocQ

Jest - Mock Local Storage Have Mobx and React work with TypeScript Loose assertions on arguments passed to function with Jest TypeScript Abstract Class Check if a Docker image exists locally My Free and Open Source Expense Tracker App is on the App Store Pass artifacts around in between stages in gitlab CI How to start a tech company as a non technical individual Setup gitment on your Hexo blog
A-Star Pathfinding React Demo
Jonathan Klughertz · 2020-01-12 · via CodeBlocQ

Check out the demo here.

Introduction

The A-Star Pathfinding algorithm finds the shortest path in between 2 points while avoiding obstacles.

Resources

To understand how the algorithm works, I highly recommend the following video by Sebastian Lague:

Explanations

The basic logic is pretty straightforward:

For each cell around the start node, calculate 3 properties:

  • The distance from the start node - commonly called G Cost
  • The distance from to the end node - commonly called H Cost
  • The sum of the two - commonly called F Cost = G Cost + H Cost

Then pick the node with the lowest F Cost amongst all the nodes and start the process again. When finding the next node to use as a pivot, consider the newly calculated nodes and the previous ones as well.

If there is a path, it should eventually be reached. Otherwise eject !

The part that is a bit trickier:

The distance from the start for a new Node A is equal to the distance in between Node A and its parent + the distance in between that parent and the start node and not the absolute distance from the start node.

This is important as otherwise, obstacles would not be taken into account.

This has 2 consequences:

  • When computing a new node, the parent node needs to be saved as well. This will form a linked list of sorts that will save the whole path information from the start.
  • When looking at the neighbooring nodes, always check the already computed nodes as well and see if a smaller G Cost can be found through the current node. If that is the case, the G Cost, F Cost and parent need to be updated for that neighbooring node with the new best path information.

When using the app I have created, you can hover over the cells to checkout the different properties that were computed.

Pseudo Code based on my implementation

create an OPEN list that will hold the computed nodes
create a GRID of cells with properties
including (fCost, gCost, hCost, parentNode, isClosed, xCoordinate, yCoordinate)

insert the start node into the OPEN list

while there are nodes inside OPEN // Main loop
take the node inside OPEN with the smallest fCost -> CURRENT

if CURRENT is the END node
go back through the parents to compute the path and return. It's done

remove CURRENT from the OPEN list
set the GRID cell with the CURRENT coordinates to isClosed = true

for each NEIGHBOR of CURRENT
if NEIGHBOR is blocked by an obstacle, a closed cell or out of bounds
move on to the next NEIGHBOR

calculate the gCost for NEIGHBOR which is gCost of CURRENT + distance to NEIGHBOR
(distance 1 if in a straight line or sqrt(2) ~ 1.4 if in a diagonal)

if NEIGHBOR is not in OPEN
add it to OPEN and set the rest of its properties (hCost, fCost and parent)
else if NEIGHBOR is in OPEN but the new gCost is smaller than the existing node
update the gCost, fCost and parent of that node

The TypeScript implementation is available here and the full code is available on GitHub

Thanks for reading :)