Android game development: My little game 2-connected Video Game 4-connected video algorithm 2

Source: Internet
Author: User
Tags benchmark

The following describes how to determine whether a connection can be established when there is an inflection point or two.

When there is an inflection point, there are two Connection Methods ,:

When there are two inflection points, there are the following situations:

It looks very complicated, but it is not simple. Haha, after trying many methods, I found that one method is very good. Of course, this method is not my original one,

Directly reference others' Images

In order to find the connection path between A and B, the first two points are used as four line segments. The two lines at both ends of the line segment are the map edge, and they are parallel to the horizontal axis, the other two are parallel to the ordinate axis. consider the two parallel to the x-axis.

Take a point of C and D on each of the two line segments. The two points are in a straight line parallel to the ordinate axis. Then, the ACDB path is a possible connection path between A and B.

The C and D points move on two line segments until a valid connection path is found, or the final conclusion is that such a path does not exist.

Search for two lines parallel to the ordinate axis in the same way.

Algorithm Optimization

The two-point connection path should be the shortest. Therefore, the query starts from the midline of the rectangle where A and B are located, and finds the shortest connection path from the top, bottom, and left sides.

However, I still feel that finding A to B is better, and the implementation efficiency is more efficient.

I will attach the code below

// Public Boolean moreturning () {int bignumx = 0; int smallnumx = 0; int bignumy = 0; int smallnumy = 0; int maxcolumnnum = 9; int maxrownum = 9; If (lastclick [0]> curclick [0]) {bignumx = lastclick [0]; smallnumx = curclick [0];} else if (lastclick [0] <curclick [0]) {bignumx = curclick [0]; smallnumx = lastclick [0];} if (lastclick [1]> curclick [1]) {bignumy = lastclick [1]; smallnumy = curclick [1];} else if (lastclick [1] <Curclick [1]) {bignumy = curclick [1]; smallnumy = lastclick [1];} // to the right, it is better to use a as the benchmark. If a is used as the benchmark, it should be efficient for first to the right and then to the left (INT I = smallnumx + 1; I <= maxcolumnnum; I ++) {If (pieces [I] [smallnumy] = 1) continue; For (Int J = smallnumy; j <bignumy; j ++) {If (pieces [I] [J] = 1) break;} if (I <bignumx) {for (int K = I; k <bignumx; k ++) {If (pieces [I] [bignumy] = 1) Break ;}} else if (I> bignumx) {for (int K = I; k> bignumx; I --) {If (pieces [I] [bignumy] = 1) Break ;}} els E if (I = bignumx) {// This point is recorded here. Pieces [bignumx] [smallnumy] Return true ;} // record the two points pieces [I] [smallnumy], pieces [I] [bignumy] Return true;} // redirect to the left for (INT I = smallnumx; i> = 0; I --) {if (I! = Smallnumx & pieces [I] [smallnumy] = 1) continue; For (Int J = smallnumy + 1; j <= bignumy; j ++) {If (pieces [I] [J] = 1) break;} For (int K = I; k <bignumx; k ++) {If (pieces [k] [bignumy] = 1) break;} if (I = smallnumx) // an inflection point, just record one point {// remember the point pieces [smallnumx] [bignumy];} else // two inflection points {// pieces [I] [smallnumy], pieces [I] [bignumy];} return true;} // For (INT I = smallnumy; I <= maxrownum; I ++) {If (pieces [smallnumx] [I] = 1) continu E; For (Int J = smallnumx; j <bignumx; j ++) {If (pieces [I] [J] = 1) break;} if (I <bignumy) {for (int K = I; k <bignumy; k ++) {If (pieces [bignumx] [I] = 1) break ;}} else if (I> bignumy) {for (int K = I; k> bignumy; I --) {If (pieces [bignumx] [I] = 1) break ;}} // else if (I = bignumy) // {// at this time, there is only one inflection point, recording pieces [smallnumx] [bignumy], however, the previous two points have found this point // return true; //} // indicating that there are two inflection points: Pieces [smallnumx] [I], pieces [bignumx] [I] Return true;} // go up to for (INT I = Smallnumy; I> = 0; I --) {if (I! = Smallnumy & pieces [smallnumx] [I] = 1) continue; For (Int J = smallnumx; j <bignumx; j ++) {If (pieces [I] [J] = 1) continue;} For (int K = I; k <bignumy; k ++) {If (pieces [bignumx] [I] = 1) continue;} // if (I = smallnumy) // there is only one inflection point. // {// at this time, there is only one inflection point. It records pieces [smallnumx] [bignumy], but the first two have already done this, now you don't need to do it // return true; // There are two inflection points at this time, record pieces [smallnumx] [I], pieces [bignumx] [I] Return true ;} return false ;}

Related Article

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.