My horse Voronoi


    Great, geniuses. I used to be a genius, but now I write toys for the iPhone. The past week, Apple and Google have excited me a lot, trying to deprive apps and revenue. I had to sweat and make 7 games in 7 days on Swift 3.0. At the same time, I did not look into the old Obj-C code of past games - it is much easier to make applications in the new language.

    Before, when I was a genius, I had to re-write the sort function (as an element of the game) every time and then be proud of it. God, what an idiot I was. However, not much I have changed. But the tool has changed. See what sorting now looks like on Swift 3.0.

      let vtxSorted = vtx.sorted(by:{ $0.yPosition > $1.yPosition })
    

    And that’s all! The VTX vertex array is sorted by Y coordinate ...
    Why is this necessary? But why ...

    Sorting by coordinate is needed to build a Voronoi diagram, about which there were many articles on Habré.

    Voronoi diagram


    You all know about convex-hull and how to build it. In Russian, this is called the Voronoi diagram. Let me remind you, that this is a grid equidistant from all points sketched on a plane in a mess. Replace the plane on the iPhone screen, dots on bold white pixels and get such a picture.


    Nice and immediately want to write some kind of game. So I did back in 2009, when you, the reader, did not even know about Ruby, who now lies on the rails instead of Anna Karenina.

    Yes, and let it lie, and we are carried away - that's when I made a game on OpenGL and didn’t earn anything on it. Perhaps I got $ 50 for 5 years, but this is unlikely.

    But the game is cool. You poke a finger on the screen and are nervous that you do not have time to remove all the cells from the board. What else is needed for happiness? Well, maybe a little candy and jam. Moreover, in the prime of life.

    And so I decided to remake the game in a new way. Just on the Internet I got a vivid picture with a hot topic about elections in the USA, and I made it an application icon. He replaced the points with Clintonih and Trump-pam-pam, the grid with the States - and rushed to program on a clean Swift.

    Ambush with OpenGL


    Why OpenGL? To draw textured triangles. Pouring with pure color is to the garden. Kindergarten. We will only have hardcore!

    Ahhhhh! No! I'm not talking about the defender of Zenith. There is no Swift API to call OpenGL functions! Come on peppers! Okay, play with the nodules and try metal. This is a fashionable library that Apple made instead of OpenGL, with similar functionality and interface, but supposedly working faster on native hardware.

    Ambush with Metal


    This library does not work on the simulator! Atas! Under Jobs, this was not. Hold me 100 people! Three thousand devils. Life seems to have been lived in vain. And so sad, with a shattered dream, I wandered in the sudden Balkan rain and listened to the neighing of local guys. However, the girl neighing even louder. Suddenly, like the checkers of a green-eyed taxi, a thought dawned on me - and no libraries needed, kind man! I can draw a triangle with a texture myself, on a clean UIKit, on a sweet heart Swift. And not even a triangle, but a whole polygon, which is even cooler.

    How? but look at the code, the Russian language is powerless here.

            var mask = CAShapeLayer()
            mask.frame = groundTrump.layer.bounds
            let path = CGMutablePath()
            for t in app.trumpTriangles  {
                if t.flag == 0 {
                    path.closeSubpath()  // закрываем треугольник
                    path.move(to: t.p)  // начинаем новый
                } else {
                    path.addLine(to: t.p) // бежим по вершинам диаграммы
                }
             }
            mask.path = path
            groundTrump.layer.mask = mask1
    

    Yes, the mask steers and I know her. And now you also know. Go ahead, you need to write an algorithm for constructing a grid. I love grids, my dissertation was about meshless methods (SPH). Here P is particles.

    By the way, github has overseas Voronoi diagram grid building algorithms on Swift 3.0. As many as two. Unfortunately, the first of them does not take into account a rectangular border, the other works with error errors. I had to write the program myself, which was wonderful, the brains curled up to the side, clockwise. It is clockwise that I go around each vertex individually and the entire array of points as a whole. I did not use the Fletcher method, sorted the points by angle and fried stupidly O (N ^ 2). At 50 points, this does not matter, although on the iPhone 4S it noticeably slows down. But who has the iPhone 4S right now? Even Habré editors do not use them.

    What else? The game is not laid out in the store, maybe I'll do it tomorrow, but I suggest you take a look at the video of the gameplay and admire my amazing work in the comments.



    Ciao, see you.

    Also popular now: