How to find the farthest 3 consecutive elements from an array

I have an array of possible positions and another array of filled positions, which is a subarray of possiblePositionsArray . possiblePositionsArray fixed in number and already defined. I would like to find the farthest 3 consecutive points to the right and left of the selected element x of the element of the element in filledPositions . Let me explain this example further. Let's say

 possiblePositionsArray = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15] filledPositions = [p1, p2, p4, p7, p8, p9, p10, p12, p13, p14, p15] 

Both are CGPoint arrays, and they all have the same y positions and are arranged in ascending order. If I choose p11.x , then there will be the following three consecutive points on the right and left.

 [p7, p8, p9] and [p8, p9, p10] To the Left of p11 [p12, p13, p14] and [p13, p14, p15] to the right of p11 

But the farthest left and right will be:

 farthest to left of p11 is [p7, p8, p9] farthest to right of p11 is [p13, p14, p15] 

How can i achieve this?

+5
source share
1 answer

Start by starting filledPositions . Find the first item from filledPositions in possiblePositionsArray . Check if the following two elements from both arrays match each other. The first group in a row is the farthest to the left of the item you selected. This works even if the x values ​​in the possiblePositionsArray elements are not evenly spaced.

After that, you do this in reverse order to find the farthest to the right.

The code for this would be something like this:

 let selectedElement = yourSelectedElement //left consecutive group var consLeft = [CGPoint]() //right consecutive group var consRight = [CGPoint]() if filledPositions.count >= 3 { for i in 0..<filledPositions.count-2 { // find the index of the element from filledPositions in possiblePositionsArray let indexInPossiblePostionArray = possiblePositionsArray.indexOf(filledPositions[i])! if indexInPossiblePostionArray < possiblePositionsArray.count-2 && // safety check filledPositions[i+2].x < selectedElement.x && // Only check left of selected element //check equality of second items filledPositions[i+1].x == possiblePositionsArray[indexInPossiblePostionArray+1].x && //check equality of third items filledPositions[i+2].x == possiblePositionsArray[indexInPossiblePostionArray+2].x { //3 consecutive elements to left selected element was found for j in i...i+2 { //add to left consecutive group consLeft.append(filledPositions[j]) } //break out of the for loop break } } //The same thing in reversed order for i in (2..<filledPositions.count).reverse() { let indexInPossiblePostionArray = possiblePositionsArray.indexOf(filledPositions[i])! if indexInPossiblePostionArray-2 >= 0 && filledPositions[i-2].x > selectedElement.x && filledPositions[i-1].x == possiblePositionsArray[indexInPossiblePostionArray-1].x && filledPositions[i-2].x == possiblePositionsArray[indexInPossiblePostionArray-2].x { for j in i-2...i { consRight.append(filledPositions[j]) } break } } } 
+1
source

All Articles