processing - How would I snap a position to a rectangular grid?
问题描述
I'm trying to make a midi-editor in processing and I need to have rectangular cells that you can click on to add a note there.
So far I've done googling but haven't found a result. What happens is when you click the further down the less accurate it gets.
I don't know what is going wrong and I've had this problem in the past but couldn't fix it.
Note: The cells are not square.
解决方案
Example processing code to snap the mouse coordinates to the center of the nearest grid cell (where cell dimensions are defined by rectangle width and height).
int w = 48; // rectangle width
int h = 24; // rectangle height
int snapX = round(mouseX / w) * w + w/2; // '+ w/2' is offset to center of cell
int snapY = round(mouseY / h) * h + h/2; // '+ h/2' is offset to center of cell
推荐阅读
- spring - 将 @Autowired 添加到 DataSourceFactory
- javascript - 单击第一个按钮时也自动单击第二个按钮
- vbscript - 如何更改文本突出显示颜色?
- python - 如何解决:UnboundLocalError:赋值前引用了局部变量't'?在蟒蛇?
- amazon-ec2 - 使用 ec2_metric_alarm 时获取“AlarmName 不得包含前导或尾随空格”
- flutter - Flutter 实现方法:点击拍照长按录制
- ios - 如何停止在导航上旋转地图?在这里 iOS SDK
- python - 如何使用 python 3.x 而不是 python 2.7.9 安装 praw
- c# - 如何根据 asp.net core 中 AutoMapper 中的用户角色有条件地映射/忽略属性
- javascript - 当 redux 存储更新时 mapStateToProps 不更新