我在按钮中间画了一个假想的圆圈。
圆的半径是 Height/2
如果 Height>Width
或 Width/2
如果 Width>Height
。
现在我必须计算这个圆圈中有哪些坐标(以像素为单位)。
这个想法是,如果例如。鼠标光标悬停在那个圆圈上,会发生一些事情。
最佳答案
计算每个坐标是多余的;只需比较与中心的距离。例如:
int radius = 5; // whatever
int deltaX = originX - mouseX, deltaY = originY - mouseY;
// compare the square distance, to avoid an unnecessary square-root
if((deltaX * deltaX) + (deltaY * deltaY) <= (radius * radius)) {
// inside the circle, or on the edge
}
为了避免一些数学运算,您还可以进行快速边界框检查,即检查矩形区域(只是加法/减法)。这可以组合使用,即
- 检查边界框
- 如果它不在边界框内,它肯定不在圆圈内
- 如果它在边界框内,计算一下平方距离
关于c# - 计算圆内坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12364690/