PathFinding | Windows Forms application demonstrating several common path | Learning library
kandi X-RAY | PathFinding Summary
kandi X-RAY | PathFinding Summary
C# Windows Forms application demonstrating several common path finding algorithms traversing a 2D maze. Do what you like with the code, it's not like I invented these algorithms. Uses Dijkstra, A*, Breadth First search, and Depth First search to find a path from A to B. The cells of the maze are randomly weighted so that the direct path is not always the cheapest. Traverses the maze investigating the least costly so far path until it reaches its destination. This is a generic algorithm that does not use a heuristic. A special implementation of DIJKSTRA that also applies a heuristic to guess which node are the most promising. The 'Manhattan distance' is used in this case, but other heuristics are possible. An algorithm that searches all the child nodes of a particualar starting point before then search their children. Searches all the child nodes a starting point, examining each branch as far as it will go either to the destination or a dead end. If a dead end is reaches, the branch is traced back to the nearest node with unexplored children and continues.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of PathFinding
PathFinding Key Features
PathFinding Examples and Code Snippets
Community Discussions
Trending Discussions on PathFinding
QUESTION
I'm testing if it works and a function that runs a while loop will block the code even if it is in an asynchronous function that I am awaiting. How do do something like pathfinding without freezing my webpage?
...ANSWER
Answered 2022-Apr-16 at 22:46Doing work keeps the main JS event loop busy.
You can't get around that by using promises or the async
and await
tools. They are tools to manage asynchronous code, they don't make code asynchronous.
A couple of strategies you could employ are:
- Moving the work off the main event loop and into a Web Worker.
- Doing the work in chunks with chunk 𝑛 triggering the processing of chunk 𝑛+1 using
setTimeout
to introduce a delay during which the main event loop can pick up other work that is waiting for it.
QUESTION
I am creating a pathfinding application and I want to connect every hexgon(H) to its adjacent hexagons. The grid is a rectangle but it is populated with hexagons. The issue is the code right now to connect these hexagons is lengthy and extremely finicky. An example of what i am trying to achieve is:
The issue is that the connections between say one hexagon and its neighbours (range from 2-6 depending on their placement in the grid) is not working properly. An example of the code i am using right now to connect a hexagon with 6 neighbours is:
...ANSWER
Answered 2022-Apr-16 at 15:16Interesting problem... To set a solid foundation, here's a hexagon grid class that is neither lengthy nor finicky, based on a simple data structure of a linear array. A couple of notes...
- The
HexagonGrid
constructor accepts the hexagon grid dimensions in terms of the number of hexagons wide (hexWidth
) by number of hexagons high (hexHeight
). - The
hexHeight
alternates by an additional hexagon every other column for a more pleasing appearance. Thus an odd number forhexWidth
bookends the hexagon grid with the same number of hexagons in the first and last columns. - The
length
attribute represents the total number of hexagons in the grid. - Each hexagon is referenced by a linear index from 0..
length
. - The
hexagonIndex
method which takes (x,y) coordinates returns an the linear index based on an approximation of the closest hexagon. Thus, when near the edges of a hexagon, the index returned might be a close neighbor. - Am not totally satisfied with the class structure, but is sufficient to show the key algorithms involved in a linear indexed hexagon grid.
To aid in visualizing the linear indexing scheme, the code snippet displays the linear index value in the hexagon. Such an indexing scheme offers the opportunity to have a parallel array of the same length which represents the characteristics of each specific hexagon by index.
Also exemplified is the ability to translate from mouse coordinates to the hexagon index, by clicking on any hexagon, which will redraw the hexagon with a thicker border.
QUESTION
I have built an A* Pathfinding algorithm that finds the best route from Point A to Point B, there is a timer that starts and ends post execute of the algorithm and the path is draw, this is parsed to a global variable. so it is accessable when i run the alogrithm more than once (to gain an average time).
the global variable gets added to a list, except when i run the algorithm 5 times, only 4 values get added (I can see 5 times being recorded as the algorithm prints the time after completion). when displaying the list it always misses the first time, and only has times 2,3,4,5 if i run the algorithm 5 times. here is main.py
ANSWER
Answered 2022-Apr-08 at 19:15EDIT:
the misterious 5th print is coming from this line, of course
QUESTION
class Node:
"""
A node class for A* pathfinding.
:param parent: parent of the current Node
:param position: current position of the Node in the maze
:param g: cost from start to current Node
:param h: heuristic based estimated cost for current Node to end Node
:param f: total cost of present node i.e., : f = g + h
"""
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
...ANSWER
Answered 2022-Mar-31 at 20:18This code defines a type of object Node
; each instance of the Node
object has four attributes: position
, f
, g
, and h
.
Instances of the Node
class of objects also come with the ability to be compared to other Node
s (we know this because __eq__()
has been defined for the class).
The __eq__()
method of the Node
class takes two instances of the Node
class as inputs. These are called self
and other
in the function definition. Both self
and other
have the four attributes belonging to the Node
object as mentioned above: position
, f
, g
, and h
.
The values of those attributes are accessed through Python's 'dot notation': e.g., self.position
gives the position of the object with which the method has been called. other.position
gives the position of a different Node
that you're comparing to.
The key here is that other
refers to an entirely different object, not another attribute of self
. (other
doesn't have to be a Node
; the code will work as long as other
also has an attribute named position
; otherwise, it will raise an AttributeError
.)
QUESTION
How do I make an enemy stop at a certain distance from the player rather than going right to it? I want to create a ranged unit. I can do the enemy attacks, I just don't want the enemy to go directly to the player. I have a custom AI script on my enemy using the AStar pathfinding package -
...ANSWER
Answered 2022-Mar-28 at 14:45The correct way would be to not search for a specific node, but any node within some distance from the target, or any node within some distance and with a direct line of sight.
It looks like the AStar pathfinding library should have functions for things like that. There is for example a GetNearest overload that takes a constraint that might work. Or the maxNearestNodeDistance
field. But I'm not familiar with that library, so I have difficulty providing specific suggestions.
Another alternative would be to just write your own implementation. This is not trivial, but also not overly complex, and there are plenty of resources explaining algorithms like A*. You will probably not reach feature parity with a commercial library, but you might only need fairly simple functionality. It might also be useful as a learning exercise to get better knowledge on how AI and path finding works.
QUESTION
I'm trying to adapt Sebastian Lague's A-Star path finding code (youtube) to 3 dimensions. I have it somewhat working but occasionally the path produced between my nodes is sub-optimal, and I think it's because I'm calculating distance wrong.
To move along a single dimension from one node to the next is a distance of 1. To move in 2 dimensions (diagonally) the distance is √2 (which is simplified to 1.4 in the code). These values are multiplied by 10 to keep them as integers, so 10 and 14 respectively.
To calculate the distance on a 2d plane you take the smaller of the X and Y distances and multiply it by 14, then minus the smaller distance from the larger one and multiply what remains by 10.
...ANSWER
Answered 2022-Mar-10 at 13:40In your code, changing location in two dimensions should be based on 14 (10 * square root of 2), but changing in all three dimensions at once should be based on 17 (10 * square root of 3), if my quick math is correct. I suspect that may solve your issue.
(Sure enough...here's a short explanation of why it should be based on sqrt(3).)
QUESTION
I was wondering what best approach to take when trying to implement pathfinding but without using a grid.
In this example, the player is a able to place road tiles on a grid, but I only want to pathfind the placed roads (no need to compute for the whole map).
I've tried a couple things but with no luck.
Any help would be appreciated.
Thanks in advance.
...ANSWER
Answered 2022-Jan-29 at 04:46I think you are looking for A* algorithm. There are also other ways to do this, but most of them are based on graphs, including Unity NavMesh.
There is amazing site with interactive examples and implementation guide, for sure it will be helpful, whether you want to implement this yourself or just understand how it works: https://www.redblobgames.com/pathfinding/a-star/introduction.html
QUESTION
I am trying to make a simple pathfinding app with pygame and make it a .exe using pyinstaller. Unfortunatly, the project uses images, and when I run the .exe, it comes back with telling me the images dont exist. How do I run pyinstaller to include the images?
...ANSWER
Answered 2022-Jan-17 at 04:24If you dont use the --onefile
command, you can just drag and drop the images into the folder your exe is in. Or drop in the folder of images.
If you do use onefile, you need to modify the .spec file you get when you run the code pyinstaller script.py
. Then run pyinstaller scriptname.spec
. edit the datas
variable somewhat like this
QUESTION
import javax.swing.*;
import java.awt.*;
public class Grid extends JFrame{
public Grid(){
super("Pathfinding Algorithms");
setContentPane(new drawGrid());
setSize(1920,1080);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true);
}
class drawGrid extends JPanel {
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.drawLine(0,50,1920,50);
}
}
public static void main(String[] args){
new Grid();
}
}
...ANSWER
Answered 2021-Dec-31 at 23:13Follow a tutorial to learn the basics of Swing. Oracle provides one free-of-cost.
There you will find this example code to compare to your code.
In that example code you’ll find the main
method makes this call:
QUESTION
As you see, there is a map and I want to pathfinding to identify which cities are liked each other.
The yellow tiles in the map are the Land, and blue ones are the ocean. The red font means there is a waterway, and the green font means there is a road. The correct path should be linked as road-road, waterway-waterway, road-harbor-waterway or waterway-harbor-road. Therefore,
2,6City can link to 2,4City via (2,6City)-(1,6)-(0,6)-(1,5)-(2,5)-(3,4Harbor)-(2,4City),
2,6City can link to 0,0City via (2,6City)-(1,6)-(0,6)-(1,5)-(2,5)-(3,4Harbor)-(2,4City)– (1,4)-(0,3City)-(0,2)-(0,1)-(0,0City),
2,6City can link to 3,0City via (2,6City)-(1,6)-(0,6)-(1,5)-(2,5)-(3,4Harbor)-(3,3)– (3,2)-(4,1Harbor)-(3,0City).
However, when I use GKGridGraph to create a map for Pathfinding, I don’t know how to tell the situation that waterway is not accessible to road. You can see, I DON‘T want:
2,6City can link to 2,4City via (2,6City)-(2,5)-(2.4City) or
2,4City is linked to 2,2City because (2,4City)-(3,4Harbor)-(3,3)-(3,2)-(2,2City)
So, any suggestion? Thanks a lot.
...ANSWER
Answered 2021-Dec-18 at 08:33If you are using GKGridGraph(fromStartingGridAt:width:height:diagonalsAllowed:)
to create your graph then every node has connections to each of its neighbors. So a better picture of your map would be this:
Since every node is connected it will find the shortest path which will pass through the water. To prevent that you need to remove the impossible connections. Your graph should look something like this:
To achieve that you cam simply remove the unwanted connections from your graph using GKGraphNode.removeConnections(to:bidirectional:)
This could be achieved by a function like this one:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install PathFinding
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page