I have n number of files on a ScrollView. Every time a file is added, x/y coordinates are stored in an array. The user is able to drag and drop the files anywhere they like. Overlapping files (e.g. distance between center less than 100 points) will form a stack, see screenshot.

At the moment, my implementation is not good. I don't come from a computer science background, so I was wondering which algorithm I should take a look at to improve the performance of this function.

var stacksSet: Set<Controller.File.View> = .init()

for fileView in fileViews {
    for compareTo in fileViews {
        comparisons += 1
        if fileView == compareTo { continue }
        let pointA = fileView.file.relativePosition
        let pointB = compareTo.file.relativePosition
        let distance = hypotf(Float(pointA.x - pointB.x), Float(pointA.y - pointB.y)) * Float(zoomViewSize.width)
        if distance < 100 {

enter image description here

  • $\begingroup$ I encourage you to articulate the problem specification. Code is off-topic here, and you should not specify your problem by providing a code implementation. (And, asking how to speed up your code is likely off-topic here.) Instead, I recommend that you specify the algorithmic problem by listing the inputs to the algorithm and the desired output, and what relationship they need to have. I think you'll be more likely to get good answers, and it will be a better fit for our site format. $\endgroup$
    – D.W.
    Apr 1, 2022 at 8:02
  • $\begingroup$ I'm looking to only cluster points (x,y coordinate / 2D) that are close to each other, I have no idea how to state the issue in a more simplistic manner. $\endgroup$
    – mmackh
    Apr 1, 2022 at 8:26
  • $\begingroup$ @mmackh While "Overlapping files form a stack", you use a set in your code. Does the order of insertion matter in this question? If you want to "improve the performance of this function" since your "implementation is not good", do you mean the number of files can be too many for your implementation to be responsive enough? Can a user drag and drop many files at the same time? $\endgroup$
    – John L.
    Apr 2, 2022 at 2:18
  • $\begingroup$ Order does not matter, performance is bad since currently the number of compares grows exponentially with each extra file added. I can help perceived performance by offloading the code to a different thread, but ultimately I was wondering what the optimal solution here is. $\endgroup$
    – mmackh
    Apr 2, 2022 at 5:40
  • $\begingroup$ Is your code correct? What happens if viewA overlapps viewB, viewC overlaps viewD but viewA and viewB are far from viewC and viewD? Your code will put all of them in the same set. Please specify what will be put a stack clearly. For example, if viewA is in a stack, must every view that overlaps viewA be put in the same stack? $\endgroup$
    – John L.
    Apr 4, 2022 at 17:58


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.