[SOLVED] Can someone help me with the implementation of pmouseX and pmouseY in P5.js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

31st Aug 2019, 10:27 AM
marjel101
marjel101 - avatar
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
31st Aug 2019, 6:27 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 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
31st Aug 2019, 6:24 PM
ODLNT
ODLNT - avatar
+ 4
You are welcome. btw Don't beat yourself up too much, because it happens to all of us.😉
31st Aug 2019, 6:43 PM
ODLNT
ODLNT - avatar
+ 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 😊
31st Aug 2019, 6:40 PM
marjel101
marjel101 - avatar
+ 1
is it working now? I mean the code looks fine for me, but the question is still there :/
31st Aug 2019, 4:33 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 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.
31st Aug 2019, 6:02 PM
marjel101
marjel101 - avatar
+ 1
P.S. your code is very cool, yet you could work a bit on readability, especially when using math :)
31st Aug 2019, 6:28 PM
Aaron Eberhardt
Aaron Eberhardt - avatar