"IOS" random triangle Tile layout algorithm

Source: Internet
Author: User

Have you seen enough of iOS in view of these default square blocks, neatly displayed?

This paper presents a random algorithm for designing a triangular layout of tiles and implementations.

Such rules, and propose compromises between random permutations. It looks messy and doesn't have a new thing.

The focus is on design and implementation to implement layout algorithms that can change colors or add images.


Latest Source: https://github.com/duzixi/Varied-Layouts (Continuous maintenance. Welcome to the mutual powder)

The starting address of the blog: Http://blog.csdn.net/duzixi


The layout generates effects such as the following:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzhv6axhp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast ">


Core algorithm design and code implementation such as the following:

triangleviewcontroller.m//trianglelayout////Created by Chieh XI (duzixi) on 14-8-24.//Copyright (c) 2014 lanou3g.co M All rights reserved.//#import "TriangleViewController.h" #import "Triangle.h" #import <quartzcore/quartzcore.h > #define PADDING 10#define SIZE 100#define COL (self.view.frame.size.width/size) #define ROW ( self.view.frame.size.height/size) @interface Triangleviewcontroller () @end @implementation triangleviewcontroller-(    void) viewdidload{[Super viewdidload];//do any additional setup after loading the view, typically from a nib.    _randompoints = [Nsmutablearray array];        _triangles = [Nsmutablearray array];        int oy =-SIZE/2; for (int i = 0, i < ROW + 1; i++) {for (int j = 0; J < COL; J + +) {int ox = (i% 2 = = 1)? J * SI            ZE:J * SIZE-0.5 * SIZE;            Ox-= SIZE/4;            Step 1: Draw lattice UIView *view = [[UIView alloc] Initwithframe:cgrectmake (OX, I * SIZE + oy, size, size)]; if ((j + i)% 2 = = 0) {View.backgroundcolor = [uicolor Graycolor];                        }//[Self.view Addsubview:view];            Step 2: Generate random points in the lattice int x = arc4random ()% (size-padding * 2) + view.frame.origin.x + PADDING;            int y = arc4random ()% (size-padding * 2) + VIEW.FRAME.ORIGIN.Y + PADDING;            Cgpoint p = cgpointmake (x, y);            Nsvalue *v = [Nsvalue valuewithcgpoint:p];            [_randompoints Addobject:v];            UIView *pview = [[UIView alloc] Initwithframe:cgrectmake (p.x, P.Y, 2, 2)];            Pview.backgroundcolor = [Uicolor Bluecolor];                        [Self.view Addsubview:pview];            UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (p.x, P.Y, 50, 20)];            Label.text = [NSString stringwithformat:@ "%lu", (unsigned long) [_randompoints Count]];        [Self.view Addsubview:label];    }} int k = 0; for (int i = 0; i < ROW; i++) {NSLog(@"-----------------");            for (int j = 0; J < COL-1, J + +) {//Step 3: I triangle will point classification k = i * COL + j + i;            Triangle *t = [[Triangle alloc] init];            T.P1 = [_randompoints[k] cgpointvalue];            T.P2 = [_randompoints[k + 1] cgpointvalue];  int col = i% 2 = = 0?            Col:col + 1;                        T.P3 = [_randompoints[k + 1 + col] Cgpointvalue];            NSLog (@ "%d%d%d", K, K + 1, K + 1 + col);                        [_triangles Addobject:t]; Step 4: The rectangular region where the triangle is generated int minX = t.p1.x < t.p2.x?

t.p1.x:t.p2.x; MinX = MinX < t.p3.x? minx:t.p3.x; int miny = T.p1.y < t.p2.y? T.P1.Y:T.P2.Y; Miny = Miny < t.p3.y?

MINY:T.P3.Y; int MaxX = t.p1.x > t.p2.x?

t.p1.x:t.p2.x; MaxX = MaxX > t.p3.x? maxx:t.p3.x; int maxy = T.P1.Y > t.p2.y? T.P1.Y:T.P2.Y; Maxy = Maxy > t.p3.y? MAXY:T.P3.Y; k++; Uiimageview *view = [[Uiimageview alloc] Initwithframe:cgrectmake (MinX, Miny, Maxx-minx, Maxy-miny)]; View.backgroundcolor = [Uicolor Orangecolor]; Step 5: Create a mask based on a triangle uibezierpath *path = [Uibezierpath Bezierpath]; [Path Movetopoint:cgpointmake (T.p1.x-minx, T.p1.y-miny)]; [Path Addlinetopoint:cgpointmake (T.p2.x-minx, T.p2.y-miny)]; [Path Addlinetopoint:cgpointmake (T.p3.x-minx, T.p3.y-miny)]; [Path Closepath]; Cashapelayer *masklayer = [Cashapelayer layer]; Masklayer.path = [path Cgpath]; View.layer.mask = Masklayer; [Self.view Addsubview:view]; }}}-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end


  triangle.h//  trianglelayout////  Created by Chieh XI (duzixi) on 14-8-24.//  Copyright (c) 2014 Lanou3g.com All rights reserved.//#import <Foundation/Foundation.h> @interface Triangle:nsobject@property ( Nonatomic, assign) Cgpoint P1, @property (nonatomic, assign) cgpoint p2; @property (nonatomic, assign) Cgpoint P3; @end


In order for everyone to see the layout process, the code retains some intermediate processes (staring out).

Open the gaze to see the lattice, random points and other content.


The next goal is to rewrite it into the Uicollectionview layout, so please look forward to


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

"IOS" random triangle Tile layout algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.