110,580
社区成员
发帖
与我相关
我的任务
分享
//y=ax+b == > -ax+y-b=0;
//calculate the distance between point and line
//distance = |-a*x+y-b|/sqrt(a^2+1)
int onCount = 0;
foreach (BeamLight bl in Lights)
{
bl.IsLightOn = false;
float x = bl.Position.X;
float y = bl.Position.Y;
foreach (BeamNode bn in Enclosure.Nodes)
{
KeyValuePair<float, float> kp = dic[bn];
float minx, maxx;
minx = bn.Position.X > bn.NextNode.Position.X ? bn.NextNode.Position.X : bn.Position.X;
maxx = bn.Position.X > bn.NextNode.Position.X ? bn.Position.X : bn.NextNode.Position.X;
double distance = Math.Abs(-kp.Key * x + y - kp.Value) / Math.Sqrt(kp.Key * kp.Key + 1);
//check distance and make sure x betwen min~max
if (distance < Constant.DistanceLimit && x >= minx && x <= maxx)
{
bl.IsLightOn = true;
break;
}
}
if (bl.IsLightOn)
{
onCount++;
}
}