I have an array of possible positions and another array of filled positions which is a subarray of the possiblePositionsArray
. possiblePositionsArray
is fixed in number and already determined. I would like to find the farthest 3 consecutive points to the right and the left of a selected array element's x position in filledPositions
. Let me explain further with this example.
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 arrays of CGPoint
s and all have the same y positions and are arranged in ascending order.
If I select p11.x
the following will be the 3 consecutive points to the right and the 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 to the left and right would be:
farthest to left of p11 is [p7, p8, p9]
farthest to right of p11 is [p13, p14, p15]
How can I achieve this?
First start from the beginning of filledPositions
. Find the first item from filledPositions
in possiblePositionsArray
. Check if the next two items from both arrays match each other. The first consecutive group is the furthest to the left of your selected element. This works even if the x
values in possiblePositionsArray
elements are not equally spaced apart.
After that you do this in reversed order to find the furthest to the right.
The code for that 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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With