Consideration on Cocos2dx CrazyTetris dual-line pseudo-crop area calculation for elimination of judgment (2)
As previously mentioned, in this game, the basic blocks are composed of four small blocks, each of which is 25*25. Therefore, the game area can contain 10 blocks in width and 20 blocks in height. That is, 250*500. The spacing of each line is 25.
Therefore, the question now is how to determine the area occupied by the square in the area with a width of 250 and a height of 25. If the area is calculated and the total area is 250*25 = 6250, you can determine whether the condition for elimination is met. For example, an area of more than 6000 square meters.
Therefore, we will mainly discuss how to calculate the set area.
Directly connect to the previous article. In the previous article, the cropping line was used to cut the image set up and down. Here, we obviously need to use two lines to cut the image up, down, and down, and then calculate the central area based on the cutting result.
The red area is the area to be calculated.
At this time, the algorithm IDEA is similar to single-line cropping. Because it is divided into three layers, two layers of encoding are required: upper layer (01), middle layer (00), and lower layer (10 ).
Then, crop based on the encoding, save only the central cropping result, and then create a PhysicsShapePolygon object using the cropping result (Point Set), and use its getArea () method to obtain the area.
In this case, although the cropping algorithm is implemented, it is not actually applied. Therefore, it is called a pseudo-cropping algorithm.
The implementation code is as follows:
// Calculated area algorithm float BaseBlock: calculaArea (float y1, float y2) {// defines the upper and lower polygon set float area = 0; // for (int I = 0; I
* MiddleShape; middleShape = new std: vector
(); // Crop side by side for (int j = 0; j
At (I); j ++) {Vec2 startPoint = this-> coordinateSpin (shapeVecs-> at (I) [j]); vec2 endPoint = this-> coordinateSpin (shapeVecs-> at (I) [(j + 1) % shapeVecAmount-> at (I)]); int cStart = 0; int cEnd = 0; // if (fabs (startPoint. y-y1) <1e-6) & (fabs (endPoint. y-y1) <1e-6) {cStart = cEnd = 0;} else if (fabs (startPoint. y-y1) <1e-6) {if (endPoint. y-y1 <1e-6) {cStart | = 2; cEnd | = 2 ;}} else if (fabs (endPoint. y-y1) <1e-6) {if (startPoint. y-y1 <1e-6) {cStart | = 2; cEnd | = 2 ;}} else {if (startPoint. y-y1 <1e-6) cStart | = 2; if (endPoint. y-y1 <1e-6) cEnd | = 2;} // if (fabs (startPoint. y-y2) <1e-6) & (fabs (endPoint. y-y2) <1e-6) {cStart = cEnd = 0;} else if (fabs (startPoint. y-y2) <1e-6) {if (endPoint. y-y2> 1e-6) {cStart | = 1; cEnd | = 1 ;}} else if (fabs (endPoint. y-y2) <1e-6) {if (startPoint. y-y2> 1e-6) {cStart | = 1; cEnd | = 1 ;}} else {if (startPoint. y-y2> 1e-6) cStart | = 1; if (endPoint. y-y2> 1e-6) cEnd | = 1;} if (cStart = cEnd) {// The two vertices are on the same side and do not need to crop if (cStart = 0) {// Vertex on top, record to the top vertex set middleShape-> push_back (coordinateGoBack (startPoint);} else {// The two vertices are on different edges, crop if (cStart = 0) {float cutting_x; float cutting_y; if (cEnd = 1) {cutting_x = startPoint. x + (endPoint. x-startPoint. x) * (y2-startPoint. y)/(endPoint. y-startPoint. y); cutting_y = y2;} else {cutting_x = startPoint. x + (endPoint. x-startPoint. x) * (y1-startPoint. y)/(endPoint. y-startPoint. y); cutting_y = y1;} middleShape-> push_back (coordinateGoBack (startPoint); middleShape-> push_back (Vec2 (cutting_x, cutting_y )));} else {float cutting_x; float cutting_y; if (cStart = 1) {cutting_x = startPoint. x + (endPoint. x-startPoint. x) * (y2-startPoint. y)/(endPoint. y-startPoint. y); cutting_y = y2;} else {cutting_x = startPoint. x + (endPoint. x-startPoint. x) * (y1-startPoint. y)/(endPoint. y-startPoint. y); cutting_y = y1;} middleShape-> push_back (coordinateGoBack (Vec2 (cutting_x, cutting_y )));}}} vec2 * middleTempShape = new Vec2 [middleShape-> size ()]; for (int index = 0; index <middleShape-> size (); index ++) {middleTempShape [index] = middleShape-> at (index);} area + = PhysicsShapePolygon: create (middleTempShape, middleShape-> size ()-> getArea ();} return area ;}