【发布时间】:2012-11-28 08:51:23
【问题描述】:
我必须根据其与同心圆的交点来绘制具有不同填充颜色的矩形。显示的图片将使您对场景有更好的了解, (仅代表目的)
目前我正在通过应用毕达哥拉斯定理检查每个点的状态
伪代码:
SquareOf Point 到中心的距离 (sqrOfDistance) = square(point X - 圆心 X) + 正方形(点 Y- 圆心 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 是用来确定点的状态的枚举。
【问题讨论】:
-
只是出于好奇,你介意告诉我这是干什么用的吗?
-
您可以通过首先检查矩形是否在包围圆的正方形((-r,-r)到(r,r))内进行优化(半径= r,中心=(0,0 ))。
-
“Square() 是获取平方根的自定义函数” - 你真的是指“平方根”,还是仅仅指“平方”?如果是前者,那么请注意,您可以重写算法以使用事物的平方来进行命中测试,而不是平方根事物,这会快得多。此外,对于实际计算平方根的函数来说,Square() 是一个可怕的名称……(实际上,我很确定您的意思只是“平方根”。)
-
与其对交集检查进行微优化,不如考虑在一些空间划分结构中组织方格——四叉树、k-d树...
-
@Tharwen 是半导体晶圆,矩形是裸片