We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am doing one visualization task, where I am drawing graph inside canvas through connecting edges as line function. Now I want to check whether my mouse is over the line or not. If it is then I would like to show the distance between this two vertices. Can anyone tell me here how can I do it with P5.js ?
Answers
http://www.JeffreyThompson.org/collision-detection/line-point.php
https://GitHub.com/processing/p5.js/wiki/Processing-transition
If you have a list of points, then you need to have them sort. The task is as easy as checking the two points in the array that the mouse straddles.
Kf
@GoToLoop The issue is I have an array of lines. Can I check parallelly on all lines that whether my mouse pointer is on them or not? Is p5 so fast to check on all lines?
Dunno. Are all of those lines form a polygon area? :-/
http://www.JeffreyThompson.org/collision-detection/poly-point.php
If they're random lines everywhere, you've gotta iterate over them 1 by 1.
Once found a match, quit the loop prematurely. That's all I know. ^#(^
They are lines of a connected graph. Actually, I want to show the distance of this line which I have already calculated and having an each edge as an object.
Sorry, not good at all on 3D here. :(
Is this a 3D model or are you working in 2D?
If it is 3D, you can project all points to a common plane. Then you need to find the point closest to your mouse pointer. You will then get all the lines associated to this point and you should find which line is closer to your mouse pointer. When you find that line, you can get the second point associated to that line.
If this is a 3D problem, there is a small issue if you change the perspective of your camera. There are perspectives where, for example, two points will overlap. Then the algorithm above will only detect one point.
Now, if this is a 2D, then GoToLoop's link is the best bet. You test your mouse position against all lines and find the one the mouse pointer is close to.
Kf
@darshansonagara --
Here is a related example in Processing (Java mode) rather than p5.js.
lines
is an array ofPShape
with vertices -- but it could all be done with a custom line class instead. It can't be easily done with an array of PShapes using LINE because then there is no access to the vertex data after the shape has been created.