Code Monkey home page Code Monkey logo

mondrian's Introduction

Mondrian

A Binary Space Partitioning Framework for iOS.

Build Status Carthage compatible


Introduction

Mondrain is a lightweight framework consisting of a handful of protocols and extensions.

Any type that implements Partitionable may pass an instance into the partitioned function to return a collection of elements that compose the partitioned instance.

Installation with Carthage

Mondrian is compatible with Carthage. To install it, simply add the following line to your Cartfile:

github "NiceThings/Mondrian"

Usage

partitioned takes a root value to partitition, a minimum value which the returned elements should not fall below, and a tranformation closure that defines how the partitioning should take place.

For example, partitioned could return a CGRect vertically bisected in in chunks no smaller than a rectangle of area 100:

        let viewRect = view.frame

        CGRect.partitioned(withRootValue: viewRect,
                                          minValue: CGRect(x: 0.0,
                                                           y: 0.0,
                                                           width: 50.0,
                                                           height: 50.0)) { (parent) -> (CGRect, CGRect) in

            let bisectPt = parent.height / 2.0
            let c1 = CGRect(origin: parent.origin, size: CGSize(width: parent.width, height: parent.height - bisectPt))
            let c2 = CGRect(origin: CGPoint(x: c1.minX, y: c1.maxY), size: CGSize(width: parent.width, height: bisectPt))


            return (c1, c2)
        }

In the example below, the CGRect bisected at a random index, resulting in a slick, Mondrian-esque image.

CGRect.partitioned(withRootValue: viewRect, minValue: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0)) { (parent) -> (CGRect, CGRect) in
            
            var c1 = CGRect.zero
            var c2 = CGRect.zero
            var bisectPt: CGFloat = 0.0
            
            if arc4random() % 2 == 0 {
                bisectPt = parent.height / CGFloat(arc4random_uniform(4) + 2)
                c1 = CGRect(origin: parent.origin, size: CGSize(width: parent.width, height: parent.height - bisectPt))
                c2 = CGRect(origin: CGPoint(x: c1.minX, y: c1.maxY), size: CGSize(width: parent.width, height: bisectPt))
            } else {
                bisectPt = parent.width / CGFloat(arc4random_uniform(4) + 2)
                c1 = CGRect(origin: parent.origin, size: CGSize(width: parent.width - bisectPt, height: parent.height))
                c2 = CGRect(origin: CGPoint(x: c1.maxX, y: c1.minY), size: CGSize(width: bisectPt, height: parent.height))
            }
            
            
            return (c1, c2)
        }


Overview

License

Mondrian is released under the MIT license. See LICENSE.md for details.

mondrian's People

Contributors

mattthousand avatar

Watchers

Zev Eisenberg avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.