Faster algorithm for the length of a polyline(-segment)

Please use this forum to post feedback and suggestions related to QCAD.

Moderator: andrew

Post Reply
CVH
Premier Member
Posts: 5082
Joined: Wed Sep 27, 2017 4:17 pm

Faster algorithm for the length of a polyline(-segment)

Post by CVH » Fri Jan 16, 2026 1:26 pm

Andrew,

Please consider a faster algorithm for the length of a polyline(-segment), example code in this forum topic.

It is totally not required to explode a polyline to its individual segments first.
And discard all these shapes afterwards.

The required data can be extracted directly from the vertex data as explained.
Chord length and sweep angle for bulging segments is readily available.
The segment length is then nothing more than:
  • =A * sin(B) / B
OR
  • =A when it is straight.
Tested the algorithm on a large sample size of various polylines at hand without a hick-up
Results are systematically the same apart form the last or the last few floating point digits.

With such an accuracy error, it is impossible to determine which of the routes is more accurate.
In the end it is a running sum and that suffers from accuracy loss in floating point.

Except with 3D data ... But then QCAD can not render a 3D polyline with bulging segments correctly in the first place.
And it will not save such data.
For that we have to agree that a bulging segment ... a so-called 'Arc' in top view with 3D data is part of a Helix (G2/G3).
With a pitch of zero, it is again a flat, arc-shaped segment. :wink:

Regards,
CVH

Post Reply

Return to “QCAD Suggestions and Feedback”