astar

package module
v0.0.0-...-a3eb58e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 3 Imported by: 0

README

A* 알고리즘

A* 길찾기 알고리즘을 Go로 구현하였습니다.

패키지 설치

$ go get github.com/swkwon/astar@latest

시작하기

	if m1, err := astar.MakeDefaultMap("map-1", 10, 10, astar.Coordinate{X: 3, Y: 2}, astar.Coordinate{X: 6, Y: 9},
		[]astar.Coordinate{
			{X: 2, Y: 4},
			{X: 3, Y: 4},
			{X: 4, Y: 4},
			{X: 5, Y: 4},
			{X: 5, Y: 8},
			{X: 6, Y: 8},
			{X: 7, Y: 8},
			{X: 8, Y: 8},
		}); err == nil {
		m1.Find()
		m1.Print()
	} else {
		panic(err)
	}
  • MakeDefaultMap 메서드를 이용하여 2차원 맵 정보를 생성합니다.
  • Find 메서드를 이용하여 길을 찾습니다.
  • Print 메서드를 이용하여 결과를 출력합니다.

예제 실행방법

$ go install github.com/swkwon/astar/cmd/example@latest

$ example.exe

예제는 GOBIN 폴더에 설치됩니다. GOBIN 환경변수 설정이 안되어 있을 경우 설치되지 않습니다. 예제를 실행하면 두가지 예제에 대해 출력됩니다.

start a* algorithm

map-1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 X 0 0 0 0 0
0 0 S 0 X 0 0 0 0 0
0 0 0 . X 0 0 0 . 0
0 0 0 . X 0 0 . X .
0 0 0 0 . . . 0 X E
0 0 0 0 0 0 0 0 X 0
0 0 0 0 0 0 0 0 X 0
0 0 0 0 0 0 0 0 0 0

map-2
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 X 0 0 0 0 0
0 0 S 0 X 0 0 0 0 0
0 0 0 . X 0 0 0 0 0
0 0 0 . X 0 0 0 X X
0 0 0 0 . . . 0 X E
0 0 0 0 0 0 0 . X .
0 0 0 0 0 0 0 . X .
0 0 0 0 0 0 0 0 . 0
end a* algorithm

Key

map의 key 의미는 아래와 같습니다.

  • 0 - 갈 수 있는 길
  • X - 막힌길
  • S - 시작점
  • E - 종착점
  • . - 이동한 길

비용 계산

상하좌우 이동할 때는 10, 대각선 이동할 경우 14의 이동 비용이 발생합니다.

Documentation

Index

Constants

View Source
const (
	Available = '0' // Available
	Block     = 'X' // Block
	Start     = 'S' // Start
	End       = 'E' // End
	Dot       = '.' // Path
)
View Source
const (
	DefaultCost  = 10
	DiagonalCost = 14
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Coordinate

type Coordinate struct {
	X int
	Y int
}

func (*Coordinate) String

func (c *Coordinate) String() string

type MapInfo

type MapInfo struct {
	Tag    string
	Start  *Node
	End    *Node
	Width  int
	Height int
	Map    map[int]map[int]*Node

	OpenSet   *NodeSet
	ClosedSet *NodeSet
}

func MakeDefaultMap

func MakeDefaultMap(tag string, width, height int, start, end Coordinate, block []Coordinate) (*MapInfo, error)

func (*MapInfo) Find

func (m *MapInfo) Find() []*Node

func (*MapInfo) GetNearNodes

func (m *MapInfo) GetNearNodes(node *Node) []*Node

func (*MapInfo) Print

func (m *MapInfo) Print()

type Node

type Node struct {
	Coord   Coordinate
	Display rune
	G       int
	H       int
	Parent  *Node
}

func (*Node) CalcGValue

func (n *Node) CalcGValue(parent *Node)

func (*Node) CalcHValue

func (n *Node) CalcHValue(end *Node)

func (*Node) F

func (n *Node) F() int

func (*Node) IsBlocked

func (n *Node) IsBlocked() bool

type NodeSet

type NodeSet struct {
	Type         SetType
	Nodes        []*Node
	NodeIndexMap map[string]int
}

func MakeNodeSet

func MakeNodeSet(setType SetType) (*NodeSet, error)

func (*NodeSet) GetRoot

func (ns *NodeSet) GetRoot() *Node

func (*NodeSet) IsInSet

func (ns *NodeSet) IsInSet(coord *Coordinate) bool

func (*NodeSet) Len

func (ns *NodeSet) Len() int

func (*NodeSet) Pop

func (ns *NodeSet) Pop()

func (*NodeSet) Push

func (ns *NodeSet) Push(node *Node)

type SetType

type SetType int
const (
	Open   SetType = 1
	Closed SetType = 2
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL