Swift playground Challenge "Symmetric Stars" Answers

Asked 2 months ago, Updated 2 months ago, 1 views

I am re-learning from Swift playground.

I'm stuck with the title
Let's Learn Code 3
I found out the answer online, but I didn't know why it was correct.
In particular, I do not understand the last for loop and the included if statement.

Execute the following code to create a quadrant of
where you touch the screen and divide the screen into four parts. An image of an animal appears in a position symmetrical to the x-axis and the y-axis.
(For example, if you touch the upper right corner of the screen, the panda's picture is
in the lower right corner, upper left corner, and lower left corner. If you slide your fingers, different animals will appear one after another, which will have a kaleidoscope effect.

For the last for loop and the included if statement,
Does For loop mean that characters are placed in the upper right, lower right, upper left, and lower left?
I don't understand the meaning of the if statement in particular, but
Does i of Graphics[i] in Scene.place() refer to the same thing as for in or i of ifi?If so, display the 0th to 3rd animal character sequence four times
I wonder if it's just...
Also, if you set the graphics[i] to [2] or [1] in all graphics[i], no matter where you touch the screen,
Animal characters gather in the lower left corner.Why is this happening?

let animals = [ imageLiteral(resourceName: "[email protected]"),  imageLiteral(resourceName: "[email protected]"),  imageLiteral(resourceName: "[email protected]"),  imageLiteral(resourceName: "[email protected]"),  imageLiteral(resourceName: "[email protected]"),  imageLiteral(resourceName: "[email protected]")]

var lastPlacePosition = Point(x: 0, y: 0)

func addImage(touch: Touch) {

    // Space out the graphics.
    let placeDistance = touch.position.distance(from: lastPlacePosition)
    if placeDistance < /*#-editable-code*/80/*#-end-editable-code*/ { return }
    lastPlacePosition = touch.position

    //   making color sequence graph " Ericsson
    var graphics: [Graphic] = []

    Pick a random animals from the sequence of numbers / /   Kinectimals
    let chosenImage = animals.randomItem

    / and acquire three sequence graph " animals that Ericsson pickup
    for i in 0 ..< 4 {
        let graphic = Graphic(image: chosenImage)
        graphics.append(graphic)
    }

    absolute coordinates are found the touch screen /
    let x = abs (touch.position.x)
    letty=abs(touch.position.y)

    // determine the coordinates of the upper right, lower right, upper left, and lower left
    let position 1 = Point (x:x, y:y)
    let position 2 = Point (x:-x, y:y)
    let position 3 = Point (x:x, y:-y)
    let position 4 = Point (x:-x, y:-y)

    // I don't understand what this for loop means
    for in 0..<4{
        if i == 0 {
            scene.place(graphics[i], at:position1)
        } else if i == 1 {
            scene.place(graphics[i], at:position2)
        } else if i == 2 {
            scene.place(graphics[i], at:position3)
        } else if i == 3 {
            scene.place(graphics[i], at:position4)
        }
    }
}

swift

2022-09-30 14:18

1 Answers

If , display the 0th to 3rd animal character sequence four times.
I wonder if it's just...

That's right."(However, if you say ""display from 0th to 3rd four times"", it may mean ""four times each,"" so you should use the expression ""display from 0th to 3rd one time each"" without any misunderstanding."Also, it's not a sequence, it's an array.)

There is no point in using the for statement at the end of the posted code (if you want to reprint the code you found online, it would be better to include the link from which it was reprinted).

If I were you, I would write the following parts to determine the coordinates of the upper right, lower right, upper left, and lower left.

scene.place(graphics[0], at:Point(x:x,y:y))
scene.place(graphics[1], at:Point(x:-x, y:y))
scene.place(graphics[2], at:Point(x:x,y:-y))
scene.place(graphics[3], at:Point(x:-x, y:-y))

However,

Does For loop mean that characters are placed in the upper right, lower right, upper left, and lower left? I don't understand the meaning of the if statement in particular, but
Does i in Graphics[i] in Scene.place() refer to the same i in "for i in" or "if i"?

I don't think you understand the basics properly yet, if you wonder what it is.I found a strange code with a vague understanding, so that area was exposed.If you understood enough, you would have immediately recognized that "this code is doing something strange."

As an author, I practiced using for statements just before, so I thought I had to use for statements again, but I feel like I'm putting it in a complicated code by using if statements.

If you really want to use the for statement, for example, you might want to do this.

let positions=[
    Point (x:x, y:y),
    Point (x:-x, y:y),
    Point (x:x, y:-y),
    Point (x:-x, y:-y)
]

for i in 0 ..< 4 {
    scene.place(graphics[i], at:positions[i])
}

Try [2] or [1] in graphics[i] to see where you touch the screen. Animal characters gather in the lower left corner.Why is this happening?

"Gathering" means moving your fingers and addImage(touch:) is called many times, but it's a good idea to move your fingers away and addImage(touch:) is called only once.

scene.place(graphics[2], at:position1) // `graphics[2]` is placed in `position1`(top right)
scene.place(graphics[2], at:position2) // `graphics[2]` moves to `position2` (upper left)
scene.place(graphics[2], at:position3) // `graphics[2]` moves to `position3`(bottom right)
scene.place(graphics[2], at:position4) // `graphics[2]` moves to `position4` (lower left)

This depends on how the Graphic class is designed to work, but the place(_:at:) method does not place a copy of the image on the screen like a stamp, but does not exist in two places at a time like a video game character.In other words, if you continue to call place(_:at:) to the exact same instance, you will move to the last position without having time to see it on the screen.

addImage(touch:) is called many times while you move your fingers, creating a new instance of Graphic every time you call it, so what you placed in the previous addImage(touch:) will not move.

This area may be difficult to understand if you don't fully understand the relationship between classes and instances.

"When I ran the relevant task on Swift Playgrounds, it said ""This page is a creation page, so there is no answer."""The code you found is not the only correct answer.

It would have been a good opportunity to re-acknowledge that some of the information you find online is wrong or not necessarily the best, and that some of these things can be ranked high in your search.


2022-09-30 14:18

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.