Git Product home page Git Product logo

coregraphicsextension's Introduction

CoreGraphicsExtension GitHub

A small libray to provide polar coordinate feature to extend CoreGraphics

Installation:

CocoaPods

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

pod 'CoreGraphicsExtension'

Swift Package Manager

- Add to Xcode:

  1. File > Swift Packages > Add Package Dependency...
  2. Choose Project you want to add CoreGraphicExtension
  3. Paste repository https://github.com/chenhaiteng/CoreGraphicsExtension.git
  4. Rules > Version: Up to Next Major 0.3.1 It's can also apply Rules > Branch : Main to access latest code.

Note: It might need to link CoreGraphicsExtension to your target maunally.

  1. Open Project Editor by tap on root of project navigator
  2. Choose the target you want to use CoreGraphicsExtension.
  3. Choose Build Phases, and expand Link Binary With Libraries
  4. Tap on + button, and choose CoreGraphics to add it.

- Add to SPM package:

dependencies: [
    .package(name: "CoreGraphicsExtension", url: "https://github.com/chenhaiteng/CoreGraphicsExtension.git", from: "0.3.1")
],
targets: [
    .target(
        name: "MyPackage",
        dependencies: ["CoreGraphicsExtension"]),
]

Features:

CGAngle

The alias of CGFloat, and presume its unit as radians. It also provides functions to transform radians and degrees.

Usage:

// Create CGAngle
let rightAngle = CGAngle.pi/2
let rightAngle2 = CGAngle.degrees(90.0)
let rightAngle3 = CGAngle.radians(CGFloat.pi/2)
let rightAngle4 = CGAngle.radians(Double.pi/2)

// Compare CGAngle with floating numbers in different unit. 
XCTAssertEqual(rightAngle, CGFloat.pi/2)
XCTAssertEqual(rightAngle.degrees, 90)
XCTAssertEqual(rightAngle.radians, CGFloat.pi/2)

CGPolarPoint

A structure to show point in polar coordiante. Provides convenient method to transform to rectangular coordinate(a.k.a Cartesian coordinate system)

Usage:

// Create polar point
let originPt = CGPolarPoint.zero
let upPoint = CGPolarPoint(radius: CGFloat(1), angle: CGAngle.pi/2)
let rightUpPoint = CGPolarPoint(radius: CGFloat(sqrt(2)), angle: CGAngle.pi/4)

// Get point in rectangular coordinate:
let rectPt = rightUpPoint.cgpoint

CGPoint(Offset)

Apply convenient offset functions and operator(>>) on CGPoint.

Usage:

let basePt = CGPoint.zero

// Offset the base point by a CGPoint.
let offsetPt = CGPoint(x: 1, y: 1)
XCTAssertEqual(basePt.offset(by: offsetPt), offsetPt)
XCTAssertEqual(basePt >> offsetPt, offsetPt)

// Offset the base point by a CGSize.
let offsetSize = CGSize(width: 1, height: 1)
XCTAssertEqual(basePt.offset(by: offsetSize), offsetPt)
XCTAssertEqual(basePt >> offsetSize, offsetPt)

// Offset the base point by specified dx and dy
XCTAssertEqual(basePt.offset(dx: 1, dy: 1), offsetPt)
XCTAssertEqual(basePt >> (1, 1), offsetPt)

CGRect(Offset)

Apply convenient offset operator(>>) on CGRect.

Usage:

let baseRect = CGRect(origin: .zero, size: CGSize(width: 100, height: 120))

// Offset the rect by CGPoint
let offsetPt = CGPoint(x: 1, y: 1)
XCTAssertNotEqual((baseRect >> offsetPt).origin, baseRect.origin)
XCTAssertEqual((baseRect >> offsetPt).origin, offsetPt)
XCTAssertEqual((baseRect >> offsetPt).size, baseRect.size)

// Offset the rect by CGSize
let offsetSize = CGSize(width: 1, height: 1)
XCTAssertEqual((baseRect >> offsetSize).origin, offsetPt)
XCTAssertEqual((baseRect >> offsetSize).size, baseRect.size)

// Offset the rect by specified dx, dy.
XCTAssertEqual((baseRect >> (1, 1)).origin, offsetPt)
XCTAssertEqual((baseRect >> (1, 1)).size, baseRect.size)

CGRect(FitSquare)

Create a derived square rect that scale to fit its source rect with the same center point.

Usage:

let originRect = CGRect(origin: .zero, size: CGSize(width: 100, height: 120))
XCTAssertEqual(originRect.fitSquare(), CGRect(x: 0, y: 10, width: 100, height: 100))

CGRect(Center)

A computed variable that return current center of the rect.

Usage:

let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 120))
let center = rect.center

License

CoreGraphicsExtension is released under the MIT License.

If you find this library useful, one coffee could help it going.

coregraphicsextension's People

Contributors

chenhaiteng avatar

Watchers

 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.