Skip to content

Commit

Permalink
[shape] fix circle
Browse files Browse the repository at this point in the history
  • Loading branch information
honghaoz committed Oct 7, 2024
1 parent a81e91c commit 15c3790
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/honghaoz/ChouTi",
"state" : {
"branch" : "develop",
"revision" : "43251518cd40350ffa0ac60c3a14c1e5cf0b9858"
"revision" : "1c03bcbb24e853d2e1b8270d4afc136ecf906394"
}
}
],
Expand Down
17 changes: 16 additions & 1 deletion Sources/ChouTiUI/Universal/Shape/Shapes/Circle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ public struct Circle: Shape, OffsetableShape {
// MARK: - Shape

public func path(in rect: CGRect) -> CGPath {
CGPath(ellipseIn: rect, transform: nil)
guard rect.size.area > 0 else {
return CGPath(ellipseIn: .zero, transform: nil)
}
return CGPath(ellipseIn: rect.squareRect(), transform: nil)

// 1000 times performance:
// 0.000466375s for CGPath.init(ellipseIn:transform:)
//
// 0.000886791 for the below code:
// BezierPath(
// arcCenter: rect.center,
// radius: min(rect.width, rect.height) / 2,
// startAngle: 0,
// endAngle: 2 * CGFloat.pi,
// clockwise: true
// ).cgPath
}

// MARK: - OffsetableShape
Expand Down
5 changes: 3 additions & 2 deletions Tests/ChouTiUITests/Universal/Shape/Shapes/CircleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import CoreGraphics

import ChouTiTest

import ChouTi
import ChouTiUI

class CircleTests: XCTestCase {
Expand Down Expand Up @@ -72,15 +73,15 @@ class CircleTests: XCTestCase {
let shape = Circle()
let rect = CGRect(x: 0, y: 0, width: -100, height: -100)
let path = shape.path(in: rect)
expect(path) == CGPath(ellipseIn: rect, transform: nil)
expect(path) == CGPath(ellipseIn: .zero, transform: nil)
}

// rect with negative width
do {
let shape = Circle()
let rect = CGRect(x: 0, y: 0, width: -100, height: 100)
let path = shape.path(in: rect)
expect(path) == CGPath(ellipseIn: rect, transform: nil)
expect(path) == CGPath(ellipseIn: .zero, transform: nil)
}
}

Expand Down

0 comments on commit 15c3790

Please sign in to comment.