That is a neat and simple solution, assuming it meets the OP's needs; though do you oversimplfy in one respect. Whilst it's perhaps unlikely to happen you don't take account of the possibility of mouseX == pmouseX. So perhaps this would be more foolproof:
- void mouseMoved() {
- ellipse(mouseX, mouseY, 5, 5);
- // find direction
- if (mouseX > pmouseX) {
- currDirection = "left to right";
- }
- else if (mouseX < pmouseX) {
- currDirection = "right to left";
- }
- else {
- currDirection = "no horizontal motion";
- }
- }
Mind you, since we're dealing with mouse motion we're dealing with user input... and users don't necessarily have frame level control over the mouse. As an example someone could be moving the mouse to the left but 'twitch' at which point this frame-level approach would register motion to the right. If you've got this hooked up to some kind of visual feedback you'd then see some unpleasant jitter; which is why smoothing is probably a good idea...
Actually my OpenProcessing example demonstrates this issue: when you bring the mouse to a stop it's not unusual for your hand to overcompensate and draw it back slightly in the opposite direction and the system registers this as a separate move event. Ideally I should have incorporated something to avoid this happening (probably a short time-delay before a new mouse move event could be registered).
Obviously this is all rather academic... though at the very least it does highlight the need for questions to be put into some kind of context ;)