Skip to content

Conversation

@carlo-
Copy link
Contributor

@carlo- carlo- commented Dec 21, 2024

I noticed that when using a custom symbol radius / size, the onNodeTapped was sometimes hitting the wrong nodes. After investigating a bit more, I found that there was an error in the calculation that finds the node at a specific location.

The method findNode uses a size value for the nodes that is not affected by the viewport scale, while the note symbols are scaled in practice. Additionally, I found that iRadius2 is not actually the symbol radius squared (as the name would suggest), but double of the square of double of the radius 😄 I'm not sure if that should be the case, but I didn't change anything there, as I'm not sure what might be affected.

This PR addresses specifically the issue with findNode by talking into account the viewport scale, essentially performing this calculation, and then using scaledRadius2 as maximum distance:

let actualRadius = pow((iRadius2 * 0.5), 0.5) * 0.5
let scaledRadius = actualRadius / max(.ulpOfOne, viewportScale)
let scaledRadius2 = pow(scaledRadius, 2.0)

In practice, this can be simplified to:

let scaledRadius2 = iRadius2 / max(.ulpOfOne, (8.0 * viewportScale * viewportScale))

which is more efficient.

It might very well be that the problem should really be solved at the root, i.e. when calculating iRadius in the first place, or a combination of the two.

⚠️ I should also mention that I'm mainly working with macOS, so I'm not sure if this problem also exists on iOS.

@li3zhen1
Copy link
Collaborator

li3zhen1 commented Dec 21, 2024

Hi,
If I remember correctly, the node size will not be changed when the graph is scaled (therefore doing hit test with iRadius2 should be sufficient). Do you have some example where onNodeTapped hits the wrong nodes?

@carlo-
Copy link
Contributor Author

carlo- commented Dec 22, 2024

@li3zhen1 I can reproduce this on macOS with the example project included in the repo, for instance with "Les Misérables" and the mermaid visualizations. As soon as you zoom in (it becomes more and more prominent the more you zoom) and try to click and drag nodes, you will notice you often end up "catching" the wrong node, even if the pointer is directly on top of another one.

The node size does change when zooming in so that visually it looks constant. I've tried to illustrate the issue:
CleanShot 2024-12-22 at 14 47 07@2x
CleanShot 2024-12-22 at 14 47 20@2x

@li3zhen1 li3zhen1 merged commit d57627c into SwiftGraphs:main Dec 29, 2024
@li3zhen1
Copy link
Collaborator

Thanks for catching this! I fixed some other issues with this PR. Let me know if there're still problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants