请考虑以下事项:
lesDisques={{14.2065, 10.609, 0.974938}, {19.5653, 6.92721, 0.974938},
{30.4607,17.4802, 0.974938}, {27.4621, 10.0393, 0.974938},
{15.915, 20.4278,0.974938}, {28.6921, 5.2132, 1.53205},
{27.0317, 24.8346,1.53205}, {20.8853, 18.8588, 1.53205}}
where lesDisques[[#]] is {X,Y,R}
frmCorner = {{6.5946, 1.5946`}, {6.5946, 28.4054`},
{60.2162`,28.4054`}, {33.4054`, 28.4054`}}
cog = {23.91871026577044`, 15.010499627383863`}
scrCenter = {20, 15}
frmXY={{6.5946, 1.5946}, {33.4054, 28.4054}}
Graphics[{
White, EdgeForm[Thick],
Rectangle @@ frmXY,
Red, PointSize[.04],
Point@cog,
Black, Disk @@@ (lesDisques /. {a_, b_, c_} :> {{a, b}, c})},
ImageSize -> 600]
对于8个磁盘中的每一个,
我想计算其边缘与之间的最小距离:
- 每个其他磁盘的边缘(7个值) - 每个框架角{4个值},
然后我将获得8个11个值的列表。
以下内容使我能够“指出”磁盘周边:
pointize[{{x_,y_},r_},size_:12]:=Table[{x+r Cos[i ((2\[Pi])/size)],
y+r Sin[i ((2\[Pi])/size)]},{i,0,size}]
有了这个,我可以找到2个最近的2个磁盘点和计算距离,但我觉得这可能不是正确的方法。
答案 0 :(得分:10)
试试这个,
Outer[ Norm[#1[[;;2]] - #2[[;;2]]] - #1[[3]] - #2[[3]]&, #, #, 1]& @ lesDisques
它的工作原理是计算光盘中心之间的距离Norm[#1[[;;2]] - #2[[;;2]]]
,然后减去所有光盘对的半径。但是,对于大型列表,这可能不是最快的,因为它计算两次所有值,但它很简单。
对于加速,首先我们需要确定我们想要计算的对是什么。一种直接的方法是使用
确定所有唯一对Subsets[Range[Length@lesDisques], {2}]
返回
{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {2, 3},
{2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {3, 4}, {3, 5}, {3, 6},
{3, 7}, {3, 8}, {4, 5}, {4, 6}, {4, 7}, {4, 8}, {5, 6}, {5, 7},
{5, 8}, {6, 7}, {6, 8}, {7, 8}}
此外,与Outer
不同,它不会将光盘与自身配对。我会像这样使用它
With[{ps = lesDisques[[ # ]]},
Norm[#1[[;;2]] - #2[[;;2]]] - #1[[3]] - #2[[3]]& @@ ps ]& /@
Subsets[Range[Length@lesDisques], {2}]
编辑:我不喜欢在多个地方使用lesDisques
这样的变量,因为这会使以后更改变得更加困难。所以,这是一个重写:
With[{ps = #},
Norm[Subtract @@ ps[[#1,;;2]]] - Plus @@ ps[[#1,3]]& /@
Subsets[Range[Length@ps], {2}]
]& @ lesDisques
编辑:Subsets
版本有一个Outer
版本没有的缺陷,正如所写,您无法分辨哪一对磁盘正在进行比较。这是一个重写版本,
With[{ps = #},
Rule[#1,Norm[Subtract @@ ps[[#1,;;2]]] - Plus @@ ps[[#1,3]]]& /@
Subsets[Range[Length@ps], {2}]
]& @ lesDisques
返回
{{1, 2} -> 4.55184, {1, 3} -> 15.697, {1, 4} -> 11.318,
{1, 5} -> 8.01646, {1, 6} -> 12.9509, {1, 7} -> 16.6464,
{1, 8} -> 8.10742, {2, 3} -> 13.2184, {2, 4} -> 6.53803,
{2, 5} -> 12.0355, {2, 6} -> 6.77936, {2, 7} -> 16.8946,
{2, 8} -> 9.4974, {3, 4} -> 6.0725, {3, 5} -> 12.8915,
{3, 6} -> 9.88685, {3, 7} -> 5.60752, {3, 8} -> 7.16714,
{4, 5} -> 13.5826, {4, 6} -> 2.47339, {4, 7} -> 12.2946,
{4, 8} -> 8.49473, {5, 6} -> 17.361, {5, 7} -> 9.45131,
{5, 8} -> 2.70508, {6, 7} -> 16.6274, {6, 8} -> 12.6569,
{7, 8} -> 5.50844}
我觉得我从来没有回答你问题的第二部分,找到光盘和框架角落之间的最小距离。此任务最好使用Outer
完成,因为没有任何冗余计算。所以,这就是我要做的事情
Outer[ Norm[#1 - #2[[;;2]]]- #2[[3]]&, #1, #2, 1]& @@ {frmCorner, lesDisques}
只是对原始代码的略微修改。注意,在Outer
生成的矩阵中,行对应于第一个输入(在本例中为frmCorner
)和第二个输入的列,如下所示
{{10.8234, 13.0492, 27.6946, 21.5365, 20.0384, 20.8598, 29.4159, 20.8795},
{18.381, 24.1159, 25.2729, 26.8237, 11.2934, 30.502, 19.2147, 15.654},
{48.3566, 45.0012, 30.7229, 36.577, 44.0388, 37.6042, 31.844, 38.9409},
{25.2035, 24.5762, 10.3402, 18.3289, 18.2489, 22.1342, 5.77375, 14.2125}}
答案 1 :(得分:5)
dist[{d1_, d2_}] :=EuclideanDistance[d1[[1 ;; 2]], d2[[1 ;; 2]]]-d1[[3]]-d2[[3]];
l = Subsets[lesDisques, {2}];
(*Nearest disks*)
nD = l[[Ordering[dist /@ l]]][[1]]
(*minmum distance*)
minD = dist[nD]
Graphics[{White, EdgeForm[Thick], Rectangle @@ frmXY, Red,
PointSize[.04], Point@cog, Black,
Disk @@@ (lesDisques /. {a_, b_, c_} :> {{a, b}, c}),
Line[{nD[[1, 1 ;; 2]], nD[[2, 1 ;; 2]]}]}, ImageSize -> 600]