Skip to content

Add length approximation from sampled points.#10

Closed
leehericks wants to merge 3 commits into
nicklockwood:mainfrom
leehericks:svg-path-length
Closed

Add length approximation from sampled points.#10
leehericks wants to merge 3 commits into
nicklockwood:mainfrom
leehericks:svg-path-length

Conversation

@leehericks

Copy link
Copy Markdown
Contributor

Another small extension to the project. It simply adds an extension on Collection where Element == SVGPoint to calculate the length between all the points, and then uses that extension to calculate the length from the existing point sampling function.

let points = svgPath.points(withDetail: 32)
let length = points.length

let length = svgPath.length(withDetail: 32)

@codecov

codecov Bot commented Dec 21, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.19%. Comparing base (381fd60) to head (069f7d9).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
Sources/SVGPath.swift 86.04% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main      #10       +/-   ##
===========================================
+ Coverage   51.46%   76.19%   +24.73%     
===========================================
  Files           2        2               
  Lines         581      605       +24     
===========================================
+ Hits          299      461      +162     
+ Misses        282      144      -138     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nicklockwood nicklockwood force-pushed the main branch 6 times, most recently from 61b692d to 6f9ccdf Compare December 25, 2025 10:34
@nicklockwood

Copy link
Copy Markdown
Owner

Sorry, I missed this until now.

I'm curious what your use-case is for this? My gut feeling is that since it's not related to the purpose of parsing or rendering SVG paths, and because it's relatively simple to implement it yourself by calling the existing points(withDetail:) API, that it doesn't belong in the SVGPath library.

@nicklockwood nicklockwood force-pushed the main branch 3 times, most recently from 4e64d63 to dc424c3 Compare December 25, 2025 11:32
@leehericks

Copy link
Copy Markdown
Contributor Author

Yes, I just have the same extension in multiple projects/packages. It's why I guess I will stay with my own fork of this project. I animate the strokes of kanji and I use this extension to generate the length of the paths for animation timing

@leehericks

leehericks commented Dec 25, 2025

Copy link
Copy Markdown
Contributor Author

If you want to pass on it, that's fine. I guess I could make a small svgpath-length swift package to augment it.

@nicklockwood

nicklockwood commented Dec 25, 2025

Copy link
Copy Markdown
Owner

Perhaps if I could see a worked example of how you are using it? I guess where I'm struggling is how this would be useful on its own.

For your Kanji animation example, wouldn't you also need a bunch of other functions for slicing up and interpolating paths? and those would all need to be repeated in every project that used that animation as well, so the path length calculation seems like the most trivial part.

Also, it seems quite inefficient to calculate the length by generating points and then throwing them away, only to generate them again later when you need the actual point positions. It seems like it would make more sense to have a method like:

extension [SVGPoint] {
    var length: Double { ... }
}

So that you don't need to calculate the points twice.

But then again it illustrates why it seems odd for this to be part of the SVGPath library, because this could equally be an extension of [CGPoint] since it's a simple vector math operation and not SVG-specific.

@leehericks

Copy link
Copy Markdown
Contributor Author

I guess I don't completely understand your reasoning, but it's totally ok, you are the author and steward of the project. I can understand if you want to keep the project as single responsibility as possible.

I am not a mathematics and graphics programming whiz. CGPath does not provide a built-in method to return sampled or interpolated points along the path. My sources are SVG path data from the start and to even get to a CGPath I am going through the parsing of SVGPath. The fact that you have an implementation for sampling points is wonderful. I was able to use that walk through the points and approximate the path length and store it. The trigonometry is dead simple and easily reproducible. I just thought other people might also benefit from the method. It's just as much a utility method as the points sampling function.

Thank you for also exposing the aspect fill transform directly in the initializer. Another small, not exactly necessary thing that I also have code for that I can delete now.

Probably my implementation can be cleaned up more anyway by adding something like

extension SVGPoint {
    func distance(to other: SVGPoint) -> Double {
        hypot(x - other.x, y - other.y)
    }
}

and using that in the length. The point (in my mind) are that if these functions are generic and useful, why not include them for convenience.

So shall we close this or clean it up?

@nicklockwood

Copy link
Copy Markdown
Owner

Let's close it for now. Thanks

@leehericks leehericks deleted the svg-path-length branch December 27, 2025 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants