A small libray to provide polar coordinate feature to extend CoreGraphics
CoreGraphicsExtension is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod'CoreGraphicsExtension'- File > Swift Packages > Add Package Dependency...
- Choose Project you want to add CoreGraphicExtension
- Paste repository https://github.com/chenhaiteng/CoreGraphicsExtension.git
- 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.
- Open Project Editor by tap on root of project navigator
- Choose the target you want to use CoreGraphicsExtension.
- Choose Build Phases, and expand Link Binary With Libraries
- Tap on + button, and choose CoreGraphics to add it.
dependencies:[.package(name:"CoreGraphicsExtension", url:"https://github.com/chenhaiteng/CoreGraphicsExtension.git", from:"0.3.1")],targets:[.target(
name:"MyPackage",
dependencies:["CoreGraphicsExtension"]),]The alias of CGFloat, and presume its unit as radians. It also provides functions to transform radians and degrees.
// Create CGAngle
letrightAngle=CGAngle.pi/2letrightAngle2=CGAngle.degrees(90.0)letrightAngle3=CGAngle.radians(CGFloat.pi/2)letrightAngle4=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)A structure to show point in polar coordiante. Provides convenient method to transform to rectangular coordinate(a.k.a Cartesian coordinate system)
// Create polar point
letoriginPt=CGPolarPoint.zero
letupPoint=CGPolarPoint(radius:CGFloat(1), angle:CGAngle.pi/2)letrightUpPoint=CGPolarPoint(radius:CGFloat(sqrt(2)), angle:CGAngle.pi/4)
// Get point in rectangular coordinate:
letrectPt= rightUpPoint.cgpointApply convenient offset functions and operator(>>) on CGPoint.
letbasePt=CGPoint.zero
// Offset the base point by a CGPoint.
letoffsetPt=CGPoint(x:1, y:1)XCTAssertEqual(basePt.offset(by: offsetPt), offsetPt)XCTAssertEqual(basePt >> offsetPt, offsetPt)
// Offset the base point by a CGSize.
letoffsetSize=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)Apply convenient offset operator(>>) on CGRect.
letbaseRect=CGRect(origin:.zero, size:CGSize(width:100, height:120))
// Offset the rect by CGPoint
letoffsetPt=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
letoffsetSize=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)Create a derived square rect that scale to fit its source rect with the same center point.
letoriginRect=CGRect(origin:.zero, size:CGSize(width:100, height:120))XCTAssertEqual(originRect.fitSquare(),CGRect(x:0, y:10, width:100, height:100))A computed variable that return current center of the rect.
let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 120))
let center = rect.center
CoreGraphicsExtension is released under the MIT License.
If you find this library useful, one coffee could help it going.