Achieve the continuous view algorithm-achieve the separation, and achieve the separation of the continuous view Algorithm

Source: Internet
Author: User

Achieve the continuous view algorithm-achieve the separation, and achieve the separation of the continuous view Algorithm

Someone asked me, "How can I implement the serialization algorithm ?"

I said, "Save it to a two-dimensional array and determine whether two points can be connected"

"How can I determine whether to connect ?"

I said, "judge whether there are no more than two corner connections between them"

"How can we determine that there are no more than two corners ?"

Well, I don't know the specific implementation, So I went online and found it. Most algorithms are easy to understand, but the implementation is not simple, so I thought about how to implement a simple concatenation algorithm. Hey! I really came up with it. As for the title of the article, there is still a sub-Governance implementation, because of my own ideas and sub-governance (my understanding of sub-Governance: Big, small, small, and easy to small) it is consistent, so you don't have to worry about what the sub-governance is. Next, let's start with the question:

The Core algorithms of continuous view are as mentioned above: judge that the corner of a link between two points cannot exceed two. There are no more than two situations: 1. There is no corner. 2. a corner. 3. Two corners. No corner is the easiest. Let's start with it first.

No corner:

Since there is no corner, there are only two possibilities:On the same horizontal lineOrIn the same vertical lineAnd the center is blank. Then we first determine whether the two points X or Y are equal to each other to determine whether the two points are in the same line. Then, we will check the two points to see if they are all empty, if all the values are null, the two values are connected. Otherwise, the values are not empty.

// Private boolean zore (int x1, int y1, int x2, int y2) {if (x1! = X2 & y1! = Y2) {// if two points are not in the same horizontal or vertical line, return false cannot be connected without any corner;} if (x1 = x2) {// determines whether the two points are completely blank. int minY = Math. min (y1, y2); int maxY = Math. max (y1, y2); for (int I = minY + 1; I <maxY; I ++) {if (checkArr [x1] [I]! = 0) {return false ;}} else if (y1 = y2) {// determines whether the two vertices are completely blank. int minX = Math. min (x1, x2); int maxX = Math. max (x1, x2); for (int I = minX + 1; I <maxX; I ++) {if (checkArr [I] [y1]! = 0) {return false ;}}// if it can be executed, it indicates that the two points are all blank. Therefore, true return true is returned ;}Code implementation without corner

One Corner:

 

Draw a right angle with a fixed start point and end point, as shown in:

In the figure, "a B" is the two points selected in our game. The two points can meet the requirements of A corner.A-> D-> BAndA-> C-> BFor the two cases, first look at A-> D-> B:

We already know the coordinates of A (x1, y1) and B (x2, y2). At this time, the coordinates of the D point naturally come out: (x2, y1), knowing the coordinates of the D point, then we can look at A corner of the A-D-B to see whether A to D is not connected with A corner, and D to B is not connected with A corner. The judgment of connecting a corner instantly becomes the judgment of two sections without connecting a corner. It's easy and easy !! However, we still need to pay attention to a small point. If the D point is not blank, all our efforts will be wasted. Therefore, we can add a judgment: The D point is not blank and Pass directly.

Private boolean one (int x1, int y1, int x2, int y2) {if (x1 = x2 | y1 = y2) {// two coordinates are required for a corner. return false;} return (data [x1] [y2] = 0 & zore (x1, y1, x1, y2) & zore (x1, y2, x2, y2) | (data [x2] [y1] = 0 & zore (x1, y1, x2, y1) & zore (x2, y1, x2, y2 ));}Code implementation for one corner

Two corners:

The previous method of turning a corner into no corner is simplified. Now let's consider how to simplify the two corners into one:

For the two corners from A to B, we can say that we know at least the first corner, "what? What do we know about the first corner? There are so many situations between two corners. Don't tease me !" Don't worry. Here I am talking about the position of the first corner because there are only four situations in the first corner: A'sHorizontal left, A'sHorizontal right, A'sVertical top, A'sVertical bottom,To find the first corner point, we only need to find it from the left, right, up, and down loops of A respectively. "How do we know which of the four directions is the first corner point ?" I don't know about this problem, but I know that if point C is the first corner, then there must be a corner between point C and B, the problem with two corners has become another problem. It's easy and easy! Yes! Yes! The important thing is said three times!

As for the realization, the first thing to do is to find the blank point from the four directions of the loop. If it is found, determine whether the blank point and the B point are connected to A corner, if a non-blank point is found or the boundary is reached, the loop can END!

Public boolean two (int x1, int y1, int x2, int y2) {// left for (int I = x1-1; I>-1; I --) {if (checkArr [I] [y1]! = 0) {break;} if (one (I, y1, x2, y2) {return true ;}// right for (int I = x1 + 1; I <checkArr. length; I ++) {if (checkArr [I] [y1]! = 0) {break;} if (one (I, y1, x2, y2) {return true ;}// up for (int I = y1-1; i>-1; I --) {if (checkArr [x1] [I]! = 0) {break;} if (one (x1, I, x2, y2) {return true ;}// for (int I = y1 + 1; I <checkArr. length; I ++) {if (checkArr [x1] [I]! = 0) {break;} if (one (x1, I, x2, y2) {return true ;}} return false ;}Code implementation for two corners

Conclusion: I have to say that this algorithm is not efficient, but I think it is easy to understand. In the end, I will attach my complete code.

Source code

 

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.