The first naive algorithm, we βclickβ icons that overlap with another icon:
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
With this first algorithm, some icons will be farther from the center than others. But he does not use the possible place by changing the angle. Applying this to the second project (with small values), it is clear that the solution will be far from ideal.
The second less naive algorithm, first we select a new angle (the difference is less than DeltaAngleMax) for each icon, then we apply the first algorithm:
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
Choose wisely deltaAngle and DeltaAngleMax. Too little deltaAngle will lead to a long run time.
To go further, you need to look at the drawing algorithm using a directional graph , which is a much more reliable method to achieve your goal, one difficulty is finding the correct node strengths (your icons, you have no boundaries).
source share