Tiled layout of random triangles (implemented by iOS) and tiled by ios
Have you read the regular block and regular display provided by default in iOS?
This article describes the design and implementation of a random triangle tile Layout Algorithm. This layout makes a compromise between the rule and the random, so that it looks fresh and not messy.
This implementation focuses on the design and implementation of the layout algorithm, which can change the color or add images.
Latest source code: https://github.com/duzixi/Varied-Layouts (continuous maintenance, welcome to powder)
Blog address: http://blog.csdn.net/duzixi
The layout is generated as follows:
The core algorithm design and code implementation are as follows:
//// TriangleViewController. m // TriangleLayout /// Created by duzixi on 14-8-24. // Copyright (c) 2014 lanou3g.com 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 @ im Plementation 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 * SIZE: j * SIZE-0.5 * SIZE; ox-= SIZE/4; // step 1: Draw a grid 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 a random vertex int x = arc4random () % (SIZE-PADDING * 2) + view in the grid. frame. origin. x + PADDING; int y = arc4random () % (SIZE-PADDING * 2) + v Iew. 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 stringWithFo Rmat: @ "% 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: divide points into 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", k, k + 1, k + 1 + col ); [_ triangles addObject: t]; // step 4: generate the rectangular area where the triangle is located. 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: generate a mask based on the 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 that can be recreated .} @ end
/// Triangle. h // TriangleLayout /// Created by 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 to let everyone see the layout process, the Code retains some intermediate processes (commented out ).
Open comments to view the lattice, random points, and other content.
The next goal is to rewrite it to the Layout of the UICollectionView. Please stay tuned.
Tell me how to solve the question of positive polygon tiled. For example, what can be a positive triangle or a tile?
The question of positive polygon tiled is, in fact, whether an inner corner of the positive polygon can be divisible by 360 degrees.
Because an inner angle of a triangle is 60 degrees, 360° degrees 60 ° = 6, the triangle can be tiled.
Another example is that an inner angle of a square is 90 °, and 360° is 90 ° = 4, so the square can be tiled.
For example, if the positive hexagonal, the inner angle is 120 ° and 120 °, ° = 3, the positive hexagonal can also be tiled.
Design an algorithm to achieve the following functions: input the three sides of the triangle a, B, c on the keyboard, if the triangle can be formed
# Include <iostream>
# Include <cmath>
Using namespace std;
Int main ()
{
Double a, B, c, s;
Cin> a> B> c;
If (a + B <= c | a + c <= B | B + c <=)
Cout <"cannot constitute a triangle! \ N ";
Else {
S = (a + B + c)/2;
Cout <sqrt (s * (s-a) * (s-B) * (s-c) <endl;
}
Return 0;
}
// Ask if you have any questions