头一个幼稚的算法,我们以“推”与另一图标重叠图标:
FOR iconToPlace in icons do:
isPlaced = false
WHILE(not isPlaced) DO:
isPlaced = true
FOR icon in icons DO:
IF overlap(iconToPlace, icon) AND iconToPlace != icon THEN:
isPlaced = false
push(iconToPlace) // same angle but the icon is now further
BREAK
ENDIF
ENDFOR
ENDWHILE
ENDFOR
有了这个第一算法一些图标会futher从中心以外。但它没有通过改变角度来利用可能的地方。通过将其应用于第二个设计(具有较小的值),显然该解决方案将远离理想的解决方案。
第二少幼稚算法中,首先我们分配一个新的角度(大于DeltaAngleMax差更小)的每个图标,然后我们应用第一算法中:
icons = SORT(icons)
iconsRef = icons
isFinished = false
WHILE(not isFinished) DO:
isFinished = true
FOR i = 0 TO i = NUM_ICONS-1 DO:
IF overlap(icons(i), icons(i+1 % NUM_ICONS))
AND not overlap(icons(i), icons(i-1 % NUM_ICONS)) //seems useless
AND not overlap(icons(i)-DeltaAngle % 360, icons(i-1 % NUM_ICONS))
AND ABS(icons(i)-iconsRef(i)) <= DeltaAngleMax THEN:
//overlap with next icon but not with previous,
//if we decrease angle we still not overlap with previous icon and
//the futur delta angle is less than DeltaAngleMax
//then we can move the icon :
icons(i) = icons(i)-DeltaAngle
isFinished = false
ELSE IF overlap(icons(i), icons(i-1 % NUM_ICONS))
AND not overlap(icons(i), icons(i+1 % NUM_ICONS)) //seems useless
AND not overlap(icons(i)+DeltaAngle % 360, icons(i+1 % NUM_ICONS))
AND ABS(icons(i)-iconsRef(i)) <= DeltaAngleMax THEN:
//vice et versa:
icons(i) = icons(i)+DeltaAngle
isFinished = false
ENDFOR
ENDWHILE
APPLY_FIRST_ALGO
明智地选择deltaAngle和DeltaAngleMax。太少的deltaAngle会导致很大的运行时间。
要走得更远,你应该看看the force-directed graph drawing算法,这是更健壮的方法来实现你的目标,其中一个难点是找到节点的正确力量(你的图标,你没有边缘)。
这不是最好的解决方案,但它足够好并且很容易实现。对于阅读此内容的人,请阅读其他答案。 – Sulthan 2013-05-23 10:51:39
苏丹 - 你好,你在乎分享完整的解决方案吗?我会非常感谢代码本身,我确实需要它的Android,但iOS的例子也将作为一个很好的参考。 – sahar 2017-05-16 14:10:49