Git Product home page Git Product logo

metron's Introduction

Build Status GitHub Release CocoaPods Release Carthage Compatible SwiftPM Compatible Supported Platforms

Metron

Geometry, simplified.

Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives provided by CoreGraphics. Completely written in Swift, Metron allows you to express complex geometric calculations in very intuitive statements:

Example 1.

Circle(in: viewFrame).contains(touchPoint)

↳ creates a Circle that fits (centered) inside viewFrame, and checks if the touchPoint is inside that circle.

Example 2.

(Angle(.pi) + Angle(270, unit: .degrees)).normalized // Angle of 0.5pi (90°)

↳ adds two Angles, one expressed in radians (default) and one in degrees. The sum is normalized to within 0 and 2𝛑 radians (or 0 and 360 degrees).

Example 3.

LineSegment(a: startPoint, b: currentPoint).intersection(with: viewFrame.lineSegment(for: minYEdge))

↳ creates a LineSegment between a (touch) start point and current point, and returns the intersection with the minYEdge of a view's frame (if these line segments indeed intersect).

Example 4.

let rotatedPoints = viewFrame.points.map { $0.applying(rotationTransform, anchorPoint: rotationPoint) }
let path: CGPath = Polygon(points: rotatedPoints).path

↳ maps each corner point of a view's frame (a CGRect) to a point to which a rotationTransform is applied, taking rotationPoint as the anchor point for the transform. With these points, a Polygon is initialized, representing the rotated rectangular frame of the view. From that polygon, we derive a CGPath that can then be drawn.

And there's much more...

Extensions

For CGPoint

  • Distance to other CGPoint
  • Clipping to CGRect
  • Relative position in CGRect
  • Normalized position in CGRect
  • Convert to CGVector
  • Round to non-decimal components
  • Addition, subtraction, multiplication...
  • Convex hull for an array of CGPoints (returns a Polygon)

For CGVector

  • Magnitude
  • Angle
  • Convenience initializer with magnitude and angle
  • Convert to CGPoint
  • Inverse
  • Dominant edge
  • Dominant corner
  • Derive line through point
  • Derive line segment from point
  • CGAffineTransform extensions
  • Addition, subtraction, multiplication...

For CGSize

  • Area
  • Swap width and height
  • Clip to other CGSize
  • Scaling using multiplication and division

For CGRect

  • Many convenience initializers, including AspectFit / AspectFill for size
  • Scaling
  • Corner points
  • Edges as line segments
  • Area
  • Center
  • Perimeter
  • CGPath

For CGRectEdge

  • Axis (x or y)
  • Adjacent corners
  • Opposite edge

For CGAffineTransform

  • Create translation transform using CGVector
  • Apply transform with a specified anchor point

New Types

Line

  • Slope
  • Y-intercept
  • X-intercept
  • Horizontal? / vertical? / parallel(to: ...)?
  • Get a perpendicular line
  • Determine intersection with other Line or LineSegment

LineSegment

  • Length
  • Derive a Line
  • Rotate
  • Determine intersection with other Line or LineSegment
  • CGPath

Circle

  • Radius
  • Diameter
  • Circumference
  • Area
  • Center
  • Width / Height
  • Bounding rect
  • Contains point?
  • CGPath
  • Points along the perimeter (divide the circle into steps, rotating in a specific direction...)

Triangle

  • Vertices (as CGPoint)
  • Sides (as LineSegment)
  • Angles (as Angle, see further on)
  • Angle bisectors (as LineSegment)
  • Altitudes (as LineSegment)
  • Equilateral? / isosceles? / scalene? / right? / oblique? / acute? / obtuse?
  • Centroid
  • Circumcenter
  • Incenter
  • Orthocenter
  • Area
  • Bounding rect
  • Contains point?
  • CGPath

Square

  • Edge length
  • Area
  • Perimeter
  • Center
  • CGPath
  • CGRect

Polygon

  • Init with points or with line segments
  • Edge count
  • Self-intersecting?
  • Convex? / concave?
  • Area
  • Perimeter
  • Center
  • Bounding rect
  • Contains point?
  • CGPath

Corner

  • Adjacent edges (CGRectEdges)
  • Opposite corner

Angle

  • Init with radians or degrees
  • Convert unit
  • Normalize
  • Invert
  • Compare
  • Addition, subtraction, multiplication...
  • Basic trigonometric functions also accept Angle
  • Create rotation CGAffineTransform with Angle

Installation

CocoaPods

Metron is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Metron"

Carthage

Metron is also available through Carthage. To install it, simply add the following line to your Cartfile:

github "toineheuvelmans/Metron"

Swift Package Manager

Metron can also be used with the Swift Package Manager. Add Metron to the dependencies value of your Package.swift:

dependencies: [
    .Package(url: "https://github.com/toineheuvelmans/metron.git", majorVersion: 1)
]

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

License

Metron is available under the MIT license. See the LICENSE file for more info.

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.