我必须根据与同心圆的交点绘制具有不同填充颜色的矩形。显示的图片将使您对场景有更好的了解, (仅代表目的)
目前我正在通过应用毕达哥拉斯定理检查每个点的状态
伪代码:
SquareOf Point 距中心的距离 (sqrOfDistance) = square(point X - Circle center X) + square(point Y- Circle center Y)
将这些值与半径平方 (sqrOfInnerR) 进行比较
if sqrOfDistance == sqrOfInnerR
Inline
else if sqrOfDistance > sqrOfInnerR
Out
else
In
即使当前的逻辑有效;它需要对每个点执行这些检查(4 或 8 次),最后一起确定状态。在我的真实世界应用程序中,将出现大约 3,000,000 个矩形。
private RectState CheckTheRectangleState(Rect rect, double radius, bool firstCall = true)
{
double SquareOfRadius = Square(radius);
var _x = rect.X - ControlCenter.X;
var _y = rect.Y - ControlCenter.Y;
var squareOfDistanceToTopLeftPoint = Square(_x) + Square(_y);
var squareOfDistanceToTopRight = Square(_x + rect.Width) + Square(_y);
var squareOfDistanceToBottonLeft = Square(_x) + Square(_y + rect.Height);
var squareOfDistanceToBottonRight = Square(_x + rect.Width) + Square(_y + rect.Height);
var topLeftStatus = squareOfDistanceToTopLeftPoint == SquareOfRadius ? PointStatus.Inline : (squareOfDistanceToTopLeftPoint > SquareOfRadius ? PointStatus.Out : PointStatus.In);
var topRightStatus = squareOfDistanceToTopRight == SquareOfRadius ? PointStatus.Inline : (squareOfDistanceToTopRight > SquareOfRadius ? PointStatus.Out : PointStatus.In);
var bottonLeftStatus = squareOfDistanceToBottonLeft == SquareOfRadius ? PointStatus.Inline : (squareOfDistanceToBottonLeft > SquareOfRadius ? PointStatus.Out : PointStatus.In);
var bottonRightStatus = squareOfDistanceToBottonRight == SquareOfRadius ? PointStatus.Inline : (squareOfDistanceToBottonRight > SquareOfRadius ? PointStatus.Out : PointStatus.In);
if ((topLeftStatus == PointStatus.In || topLeftStatus == PointStatus.Inline) &&
(topRightStatus == PointStatus.In || topRightStatus == PointStatus.Inline) &&
(bottonLeftStatus == PointStatus.In || bottonLeftStatus == PointStatus.Inline) &&
(bottonRightStatus == PointStatus.In || bottonRightStatus == PointStatus.Inline))
{
return firstCall ? RectState.In : RectState.Partial;
}
else
{
if (firstCall)
CheckTheRectangleState(rect, outCircleRadius, false);
}
return RectState.Out;
}
}
其中 Square() 是获取平方的自定义函数。 Square(x){ return x*x;}
PointStatus 和 RectState 是用来确定点的状态的枚举。