May 15, 2015

Snippet #1. Overlapping circles positions

Let our snippet section begin with some fresh Java (Android focused) code here! If you want to get the positions of n-overlapping circles like so:

Check this:

public int[] getCirclePositions(int radius, int viewWidth, int items){
	int[] positions = new int[items];
	int portionSize = viewWidth / (items + 1);
 
	for (int i = 0; i < positions.length; i++){
		positions[i] = (portionSize*(i+1)) - radius;
	}
 
	return positions;
}
  • radius: Radius of each circle.
  • viewWidth: Total width of the view where circles will be placed.
  • items: Total of circles.

What does it returns?

An int array containing the starting position where each circle should be placed.