How can I align numerous objects around a circle?

Asked

Viewed 115 times

2

I am trying in many ways and I am not succeeding, I have managed in a way that will not be viable, someone knows how I could do more in this idea take the central position of the circle and calculate points(x, y) that are 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 relative to the main bubble to create the others (would use a multiplier to create the bubbles in an outer layer the first layer, but with this would form a drawing similar to a star) and not to mention that the second layer would need twice as many objects as the first layer :(.

I’m trying to position the circular objects of a main object.

  • yes but Voce has to harvest parts of what you’ve tried, and what you’re thinking and do.

1 answer

1

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

Let’s say your circle has radius 1, and you wanted an angle of 45 degrees ( e.g. ).

How the code works with radians: 45 degrees is equal to pi/4.

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

Leaving in a more generic way:

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

circulo cos e seno

A few examples of what it would look like in your code:

//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º)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.