How can I align multiple objects around a circle?

2

I'm trying in various ways and I'm not getting success, I have managed in a way that will not be viable, does anyone know how I could do more or less in this idea to get the center position of the circle and calculate points (x, y) outside the radius.

func pressedAddBubble(sender : UIButton!){
    let direction = self.mainBubble!.getDirection()

    let width = CGFloat(self.mainBubble!.getWidth() * 0.8)
    let height = CGFloat(self.mainBubble!.getHeight() * 0.8)

    if direction == 0 {
        //print("leste")
        //adiciona as bolha ao Leste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + self.mainBubble!.getWidth() + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 1 {
        //print("nordeste")
        //adiciona a 1 bolha ao Nordeste da bolha pricipal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.maxX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 2 {
        //print("norte")
        //adiciona as bolhas ao Norte da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()

    } else if direction == 3 {
        //print("noroeste")
        //adiciona as bolhas ao Noroeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY - self.mainBubble!.getHeight() + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 4 {
        //print("oeste")
        //adiciona as bolhas ao Oeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.minY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 5 {
        //print("sudoeste")
        //adiciona as bolhas ao Sudoeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX - self.mainBubble!.getWidth() + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 6 {
        //print("sul")
        //adiciona as bolhas ao Sul da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.minX + (self.mainBubble!.getWidth()) * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
    } else if direction == 7 {
        //print("sudeste")
        //adiciona as bolhas ao Sudeste da bolha principal
        let x = CGFloat(self.mainBubble!.MainBubble.frame.maxX + self.mainBubble!.getWidth() * 0.1)
        let y = CGFloat(self.mainBubble!.MainBubble.frame.maxY + self.mainBubble!.getHeight() * 0.1)

        let bubble_aux = Bubble(v: self.view,
                                src: randomMoldura(),
                                image: imageTeste,
                                x: x,
                                y: y,
                                w: width,
                                h: height)

        bubbles.append(bubble_aux)
        self.mainBubble!.setDirection()
        //self.mainBubble!.restoreDirection()
        //self.multiplier += 1
    } else {
        print("só cheguei até aqui")
    }
}

The variable direction would be responsible for controlling the direction in relation to the main bubble to create the others (it would use a multiplier to create the bubbles in an outer layer the first layer, but this would form a star-like drawing) and not to mention that the second layer would need twice as many objects in relation to the first layer :(.

I'm trying to position the objects in a circular form of a main object.

    
asked by anonymous 26.07.2016 / 16:31

1 answer

1

To calculate coordinates of points in the radius of a circle you should use the sine and cosine functions.

Let's say your circle has radius 1, and you wanted an angle of 45 degrees (for example).

How the code works with radians: 45 degrees equals pi / 4.

Then x = cos (pi / 4) * 1 and y = sin (pi / 4) * 1

Leaving in a more general way:

x = cos(angulo)*altura
y = sin(angulo)*altura

Some examples of what your code looks like:

//adiciona as bolha ao Leste da bolha principal
let x = CGFloat(cos(0)* height) + xCenterMainCircle
let y = CGFloat(sin(0)* height) + yCenterMainCircle

//adiciona as bolha ao Oeste da bolha principal
let x = CGFloat(cos(CGFloat.pi)* height) + xCenterMainCircle
let y = CGFloat(sin(CGFloat.pi)* height) + yCenterMainCircle

Just a few values for you to reference:

0              - Leste
CGFloat.pi/2   - Norte
3*CGFloat.pi/2 - Sul
CGFloat.pi     - Oeste
CGFloat.pi/4   - Nordeste (45º)
    
20.12.2016 / 16:49