Astar pathfinding Algorithm Based on hash table data source-[as 3.0]

Source: Internet
Author: User

 

The Code posted here is intended for those who need it, rather than providing the source code for others. Therefore, we apologize. This algorithm does not use a hash table as a storage container with the traditional 2D array. The advantage of this algorithm is that the query and modification speed is faster and more convenient than the 2D array.

Package astart. Interfaces
{
Public interface iastartsourcemode
{
/**
* ID of the square corresponding to the data source
* @ Return
*
*/
Function get keypoint (): Point
/**
* String format of the ID corresponding to the square of the data source (for example, VAR keypoint = new point (x, y), the key should be "X, Y ")
* @ Return
*
*/
Function get key (): String
/**
* The Type 0 of the square corresponding to the data source is non-accessible.
* @ Return
*
*/
Function get type (): int

}
}

//////////////////////////////////////// //////////////

Package astart. Interfaces
{
Import flash. Geom. Point;
Public interface iastardata
{
Function get key (): String
Function get keypoint (): Point
Function get G (): int;
Function get F (): int;
Function get parent (): iastardata
Function get type (): int
Function set key (VAL: string): void
Function set keypoint (VAL: Point): void
Function set g (VAL: INT): void;
Function set F (VAL: INT): void;
Function set parent (VAL: iastardata): void
Function set type (VAL: INT): void
}
}

//////////////////////////////////////// ////////////////////////////////

Package astart. Interfaces
{
Import flash. Geom. Point;

Public interface iastar
{
/**
* Return result path
* @ Param source the query key of the array object that uses the VO class instance implemented by the iastartsourcemode interface as the key value must correspond to the key attribute of the VO class implemented by the * iastartsourcemode Interface
* @ Param startpoint start point, for example, new point (1, 2) data source in formation
* @ Param endpoint end point: New Point (4, 5) Corresponding data source
* @ Return
*
*/
Function getpath (Source: array, startpoint: Point, endpoint: Point): Array
}
}

 

/// // The above is the interface file ////////////// //////////////////

//// // The specific implementation is as follows ////// ////////////////////////////////////

/**
* Search for data sources in the form of Objects
* By my weak contacts/wxsr
* @ 2008
*/
Package astart. actualize
{
Import flash. Geom. Point;
Import astart. Interfaces. iastartsourcemode
Import astart. Interfaces. iastart

Public class astar implements iastar
{
Private Static const cost_straight: Int = 10;
Private Static const cost_diagonal: Int = 14;
Private Static const dir_tc: String = 'tc ';
Private Static const dir_ct: String = 'ct ';
Private Static const dir_cr: String = 'cr ';
Private Static const dir_bc: String = 'bc ';
Private var nonce: astardata;
Private var isfinish: Boolean;
Private var G: int;

Private var Source: array;
Private var startpoint: Point
Private var endpoint: Point

Private var colsepath: array;
Private var colsearray: array;
Private var openpath: array;
Private var openarray: array;
Private var patharray: Array
Private var cantl: Boolean;
Private var Cantr: Boolean;
Private var canbl: Boolean;
Private var canbr: Boolean;

Public Function astar ()
{

}
/**
* Pathfinding
* @ Param source data source
* @ Param startpoint current start
* @ Param endpoint
* @ Param Cell
* @ Return
*
*/

Public Function getpath (Source: array, startpoint: Point, endpoint: Point): Array
{
Reset ()
This. startpoint = startpoint
This. endpoint = endpoint
This. Source = Source
This. nonce = NULL? This. nonce = new astardata (0, 0, this. startpoint ):'';
This. nonce. Parent = This. nonce;
This. colsearray. Push ({f: This. nonce. F, data: This. nonce })
This. colsepath [This. nonce. Key] = This. colsearray [0]
While (this. isfinish ){
Getscale9grid (source, this. nonce, this. endpoint)
}

Return cleanarray ()
}
// Score
Private function getdis (point: Point, endpoint: Point): int
{
VaR Dix: Int = math. Abs (endpoint. x-point.x)
VaR DIY: Int = math. Abs (endpoint. y-point.y)
Return math. Abs (Dix + DIY)
}
// Obtain the vertical line Object of the optimal scoring object
Private function stratght (TAR: iastartsourcemode, endpoint: Point, type: string): void
{
If (tar! = NULL ){
If (tar. type> 0 ){
VaR cosomething: Int = getdis (tar. keypoint, endpoint) * 10;
VaR costg: Int = cost_straight + G
VaR data: astardata = new astardata (costg, (costg + cosomething), tar. keypoint );
Data. Parent = NULL? Data. Parent = This. nonce :''
If (openpath [tar. Key] = NULL & this. colsepath [tar. Key] = NULL ){

Openpath [tar. Key] = Data
This. openarray. Push ({f: Data. F, data: Data })

} Else if (openpath [tar. Key]! = NULL)
{
VaR old: astardata = openpath [tar. Key]
Data. F <old. F? Openpath [tar. Key] = data :''

}

} Else {
If (type = dir_tc)
{
This. cantl = false;
This. Cantr = false;
} Else if (type = dir_ct)
{
This. cantl = false;
This. canbl = false;
} Else if (type = dir_cr)
{
This. Cantr = false;
This. canbr = false;
} Else if (type = dir_bc)
{
This. canbl = false;
This. canbr = false;
}
}
}

}
// Obtain the diagonal object of the optimal object for the current score
Private function diagonal (TAR: iastartsourcemode, endpoint: point, can: Boolean): void
{
If (CAN & tar! = NULL)
{
If (tar. type> 0)
{
VaR cosomething: Int = getdis (tar. keypoint, endpoint) * 10;
VaR costg: Int = cost_diagonal + G
VaR data: astardata = new astardata (costg, costg + cosomething, tar. keypoint );
Data. Parent = NULL? Data. Parent = This. nonce :''
If (openpath [tar. Key] = NULL & colsepath [tar. Key] = NULL)
{
Openpath [tar. Key] = Data
This. openarray. Push ({f: Data. F, data: Data })
} Else if (openpath [tar. Key]! = NULL ){

VaR old: astardata = openpath [tar. Key]
Data. F <old. F? Openpath [tar. Key] = data :''

}
}
}

}
// Obtain the optimal score for the current score
Private function getscale9grid (Source: array, data: astardata, endpoint: Point): void
{
This. canbl = true;
This. canbr = true;
This. cantl = true;
This. Cantr = true;

VaR X: Int = data. keypoint. x
Var y: Int = data. keypoint. Y

VaR TC: iastartsourcemode = source [x + ',' + (Y-1)];
VaR CT: iastartsourcemode = source [(x-1) + ',' + Y]
VaR Cr: iastartsourcemode = source [(x + 1) + ',' + Y]
VaR BC: iastartsourcemode = source [x + ',' + (Y + 1)]

VaR TL: iastartsourcemode = source [(x-1) + ',' + (Y-1)];
VaR TR: iastartsourcemode = source [(x + 1) + ',' + (Y-1)];
VaR Bl: iastartsourcemode = source [(x-1) + ',' + (Y + 1)]
VaR BR: iastartsourcemode = source [(x + 1) + ',' + (Y + 1)]

Stratght (TC, endpoint, dir_tc)
Stratght (CT, endpoint, dir_ct)
Stratght (Cr, endpoint, dir_cr)
Stratght (BC, endpoint, dir_bc)

Diagonal (TL, endpoint, cantl)
Diagonal (TR, endpoint, Cantr)
Diagonal (BL, endpoint, canbl)
Diagonal (BR, endpoint, canbr)

Openarray. sorton ('F', array. Numeric );

If (this. openarray [0] = NULL)
{
This. isfinish = false;
Return
} Else {
This. nonce = This. openarray [0]. Data;
Delete this. openpath [This. openarray [0]. Key];
This. openarray. Shift ()

If (this. colsepath [This. nonce. Key] = NULL)
{
This. colsearray. unshift ({f: This. nonce. F, data: This. nonce })
This. colsepath [This. nonce. Key] = This. colsearray [0]
}
This. G = This. nonce. g
}
If (this. nonce. keypoint. x = endpoint. X & this. nonce. keypoint. Y = endpoint. Y)
{
This. isfinish = false;
}
Return
}
// Return the final path
Private function cleanarray (): Array
{

This. patharray = new array

Var key: String = string (this. endpoint. x + ',' + this. endpoint. y );

If (this. colsepath [Key]! = NULL ){
This. patharray. Push (this. colsepath [Key]. Data. Parent. keypoint );
This. patharray. Push (this. colsepath [Key]. Data. keypoint );

While (true ){
Key = string (this. colsepath [Key]. Data. Parent. Key );

If (Key = string (this. startpoint. x + ',' + this. startpoint. y) break;

VaR item: iastartsourcemode = This. source [Key]
This. patharray. unshift (this. colsepath [Key]. Data. Parent. keypoint)
}
}
Return this. patharray
}

// Initialize the Array
Private function reset (): void
{
This. patharray = []
This. Source = [];
This. colsepath = [];
This. colsearray = []
This. openpath = [];
This. openarray = []
This. G = 0
This. nonce = NULL
This. cantl = true
This. Cantr = true
This. canbl = true
This. canbr = true
This. isfinish = true
}
}
}

//////////////////////////////////////// //////////////////////////////////////// ////////////

/**
* Astar Pathfinder data storage unit
* By my weak contacts/wxsr
* @ 2008
*/
Package astart. astardata
{
Import astar. Interfaces. iastardata;

Public class astardata implements iastardata
{
Private VaR _ key: String
Private VaR _ keypoint: Point
Private VaR _ g: Int = 0;
Private VaR _ F: Int = 0;
Private VaR _ parent: astardata
Private VaR _ type: int
Public Function astardata (G: int, F: int, keypoint: Point)
{
This. _ g = g;
This. f = F;
This. keypoint = keypoint
}

Public Function get key (): String
{
Return this. _ key;
}

Public Function get keypoint (): Point
{
Return this. _ keypoint;
}

Public Function get G (): int
{
Return this. _ g;
}

Public Function get F (): int
{
Return this. _ F;
}

Public Function get parent (): iastardata
{
Return this. _ parent;
}

Public Function get type (): int
{
Return this. _ type;
}

Public Function set key (VAL: string): void
{
This. _ key = Val
}

Public Function set keypoint (VAL: Point): void
{
This. keypoint = Val
}

Public Function set g (VAL: INT): void
{
This. _ g = Val
}

Public Function set F (VAL: INT): void
{
This. _ f = Val
}

Public Function set parent (VAL: iastardata): void
{
This. _ parent = Val
}

Public Function set type (VAL: INT): void
{
This. _ type = Val
}

}
}

//////////////////////////////////////// /////////////////////////////////

/**
* The query key of the astar pathfinding hash table data source is the key value of this type of attribute, and the key value is the object instance.
* For example, source: array = []
* Source ["1, 2"] = new astartsourcemode (New keypoint (1, 2), 1 );
* Source ["-1,-2"] = new astartsourcemode (New keypoint (-1,-2), 1)
* Source ["2,-3"] = new astartsourcemode (New keypoint (2,-3), 0)
* The data is then transmitted to astart using the source parameter.
* By my weak contacts/wxsr
* @ 2008
*/
Package astar. actualize
{
Import flash. Geom. Point;
Import astar. Interfaces. iastartsourcemode;
Public class astartsourcemode implements iastartsourcemode
{
Private VaR _ keypoint: Point
Private VaR _ key: String
Private VaR _ type: int
Public Function astartsourcemode (keypoint: Point, type: INT)
{
This. _ keypoint = keypoint
This. _ key = string (this. _ keypoint. x + ',' + this. _ keypoint. Y)
This. _ type = Type
}

Public Function get keypoint (): Point
{
Return _ keypoint;
}

Public Function get key (): String
{
Return this. _ key;
}

Public Function get type (): int
{
Return this. _ type;
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.