+ 3
[SOLVED] Can someone help me with the implementation of pmouseX and pmouseY in P5.js?
In this code I used the values of the pmouseX and pmouseY to calculate the length and midpoint of lines that you can draw onscreen. This works well for lines that are horizontal or vertical, but not for the diagonal lines. Can someone have a look to see where I am making a mistake? Or maybe thereâs just a flaw in the implementation of pmouseX and pmouseY? Thanks! https://code.sololearn.com/WNqeLbeR8AhH/?ref=app
7 Answers
+ 1
marjel101 ok there was a problem with your implementation of Pythagoras' Theorem. You accidentally used subtraction instead of adding both numbers before using their square root. You will find a fix in this code (scroll to line 87 of the JS tab):
https://code.sololearn.com/WzdT1zVo0jod/?ref=app
+ 4
Made a slight change on line 86, radius length calculation I believe. It appears to solve the diagonal problem.
https://code.sololearn.com/W96YduHmXTrA/#js
+ 4
You are welcome.
btw Don't beat yourself up too much, because it happens to all of us.đ
+ 3
Aaron Eberhardt and ODLNT
Thanks so much for responding! So it was just a mistake I made, so stupid đ Iâll try to be more careful with my math in the future đ
+ 1
is it working now?
I mean the code looks fine for me, but the question is still there :/
+ 1
Aaron Eberhardt
The issue is still there. The dots should be nicely arranged along a drawn line, and that happens only when the line is horizontal or vertical.
When you draw a diagonal line, the dots are spaced too far apart, as if the radius is not calculated properly.
Maybe I didnât make the correct calculation, or I donât quite understand how the pmouseX and pmouseY functions.
+ 1
P.S. your code is very cool, yet you could work a bit on readability, especially when using math :)