Two-point connection path Finding Algorithm

Source: Internet
Author: User
This is a two-point connection algorithm used in the last release of the link. After the source code was released last time, many friends sent emails or asked questions in the source code via MSN. The algorithm accounts for a majority, at that time, I promised to publish an article to explain this algorithm in detail. However, as I was busy with my work recently, I would like to talk about Sorry here.
I hope you can give us some comments after reading this algorithm. Thank you. Also, the last time I got the final release version of my continuous view to my C # development group, I did not expect a BUG to be found at once. It was depressing and I did not perform tests at ordinary times, when I was playing for my girlfriend, I had clearly told her that it was useless and useful. I decided to release the final version in a day or two. I hope you can help me with the test (just play a little game after work ). The basic algorithm is as follows:
1: The two goals are the same.
2: The discount of the connection line between two targets cannot exceed two. (The connection line consists of parallel lines on the X and Y axes.) You can analyze the connection conditions and see that there are three possible conditions.
1: Linear connection 2: One Fold Point 3: two fold points. It can be found that if there is a discount point, each discount point must have at least one coordinate (x or y) that is the same as one of the target points, that is, the discount point must be in the x or y direction of the two target points.
The design logic is as follows:
Assume that the target point p1 and p2 has two discount points, z1 and z2, respectively.
1: If the p1 and p2 linear connections are verified, the connection is established.
2: Search for the finite points on the four straight lines in the direction of x and y of p1, p2, that is, the cross path described below (two straight lines may overlap). Take two points each time as z1, z2: Verify that p1 to z1/z1 to z2/z2 to p2 can be connected in a straight line. If yes, the connection is established. (If z1 = z2, there is only one discount point, it will not affect the judgment) see the following matrix 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 9 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 the two points to be connected (9) take the cross path of point 1 (3, 2) and point 2 (12, 5) respectively (the path line of point 1 is represented by number 1, and the path of point 2 is represented by number 2, intersection number), the result is also 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 01 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 0 0 1 0 0 0 0 0 0 0 2 0 0 0 00 0 0 1 0 0 0 0 0 0 0 2 0 02 2 2 1 2 2 2 2 2 2 2 2 2 2 20 0 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 above is the specific idea, next we will explain the ideas from the code. To determine whether two points can be connected, we must first obtain the cross paths of the two points, /// <summary> /// obtain the horizontal reachable path of the point specified by the vertex // </summary> private int [,] GetTempPointX (Common. position p, int [,] buffer) {// horizontal detection. The base point is 1 and the left int nBufX = 1; buffer [0, 0] = p. x; buffer [0, 1] = p. y; for (int TmpX = p. x-1; TmpX> = 0; -- TmpX) {if (map [p. y, TmpX] = 0) {buffer [nBufX, 0] = TmpX; buffer [nBufX, 1] = p. y; ++ nBufX;} else {break;} // horizontal detection. The base point is 1 to right for (short TmpX = (short) (p. x + 1); TmpX <Common. MAP_WIDTH; ++ TmpX) {if (map [p. y, TmpX] = 0) {buffer [nBufX, 0] = TmpX; buffer [nBufX, 1] = p. y; ++ nBufX;} else {break;} buffer [nBufX, 0] =-1; buffer [nBufX, 1] =-1; return buffer;} // <summary> // obtain the vertical reachable path of the point specified by the vertex /// </summary> private int [,] getTempPointY (Common. position p, int [,] buffer) {int nBufY = 1; buffer [0, 0] = p. x; buffer [0, 1] = p. y; // Vertical Detection, above point 1 for (int TmpY = p. y-1; TmpY> = 0; -- TmpY) {if (map [TmpY, p. x] = 0) {buffer [nBufY, 0] = p. x; buffer [nBufY, 1] = TmpY; ++ nBufY;} else {break;} // longitudinal detection, starting point 1 or lower for (int TmpY = p. y + 1; TmpY <Common. MAP_HEIGHT; ++ TmpY) {if (map [TmpY, p. x] = 0) {buffer [nBufY, 0] = p. x; buffer [nBufY, 1] = TmpY; ++ nBufY;} else {break;} buffer [nBufY, 0] =-1; buffer [nBufY, 1] =-1; return buffer;} all methods for obtaining the horizontal and vertical reachable paths are available. Now we can determine the private bool CheckInLineX (int [,] buffer1, int [,] buffer2, bool isInternal) {// determines whether it is in the same straight line and can be accelerated to detect if (CheckInLine (buffer1, buffer2 )) {// check method CheckInLine returns false slightly;} // horizontal Detection: longitudinal path detection // connection Sign bool isInLine = false; for (int nChkLink1 = 0 ;! (Buffer1 [nChkLink1, 0] <0 & buffer1 [nChkLink1, 1] <0); ++ nChkLink1) {// set the coaxial acceleration check flag bool bShortPass = false; for (int nChkLink2 = 0 ;! (Buffer2 [nChkLink2, 0] <0 & buffer2 [nChkLink2, 1] <0); ++ nChkLink2) {// check whether the data is in the same vertical axis if (buffer1 [nChkLink1, 0] = buffer2 [nChkLink2, 0]) {// because the coaxial point has been detected, set the coaxial contrast acceleration check flag to true bw.pass = true; // calculate the comparison point distance int nStep = buffer1 [nChkLink1, 1]-buffer2 [nChkLink2, 1]; // offset int OpNum; if (nStep> 0) {OpNum =-1; -- nStep;} else {OpNum = 1; ++ nStep;} isInLine = true; int tmpvx = buffer1 [nChkLink1, 0]; (Int mStep = nStep; mStep! = 0; mStep + = OpNum) // Math. abs (mStep-0) <1 {if (map [buffer1 [nChkLink1, 1]-mStep, tmpvx] = 0) {// continue to check next point continue ;} else {// path is blocked, and isInLine = false; break ;}}// if it is connected to if (isInLine = true) {# if DEBUG System. windows. forms. messageBox. show ("Linked \ n"); # endif return true ;}} else {// determines the coaxial acceleration check flag to confirm the acceleration if (bShortPass = true) {break ;}}}return false;} the basic idea of the judgment is as follows: from the two cross paths obtained, take out the horizontal axis (the horizontal line of two points) Point), and then take the first connection on the horizontal and vertical of point 1, find the point on the horizontal and vertical of point 2, and then determine whether the two points can be connected, if it can be connected, it indicates that points 1 and 2 are connectable. If it cannot be connected, take the next Vertex on the horizontal axis of point 1, then judge the corresponding points on the horizontal axis of point 2 until all the connected points or points on Point 1 are determined. If the horizontal axes of two points cannot be connected, then compare the vertical axes of the two points. The coaxial acceleration check mark is used to end the cycle early. When two horizontal axes are compared to whether the two axes are connected or not, if one of them is compared to the same vertical axis, whether or not they can be connected, you do not need to compare the following points. In fact, you can modify it in this place so that it can be directly located in the corresponding position in the comparison axis (Oh, because it is busy, there is no time to do this thing, it has not been met for several months now.) the scalability of this algorithm on the node is not good. If the requirement is changed to or, it will be difficult to expand. In contrast, the common pathtracing algorithm is much better, but I find that it is better to expand dimensions. If there is a 3D continuous view in the future, it needs to be connected in a three-dimensional space, the scalability of this algorithm may be better. I don't know whether you understand this algorithm. In fact, I thought it was easy to understand and use it. In terms of efficiency, I don't know how it works, most people may still find a good path. I hope you will evaluate this algorithm and give some comments. Thank you.

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.