Skip to content

Commit

Permalink
address SethFalco review
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow authored and SethFalco committed Dec 27, 2023
1 parent 842d5bf commit 5c4b425
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/03-plugins/convert-path-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ svgo:
description: Number of decimal places to round to, using conventional rounding rules.
default: 5
smartArcRounding:
description: Round the radius of circular arcs more when the effective change (measured with the sagitta) is under the error.
description: Round the radius of circular arcs when the effective change is under the error. The effective change is determined using the <a href="https://wikipedia.org/wiki/Sagitta_(geometry)" target="_blank">sagitta</a> of the arc.
default: true
removeUseless:
description: Remove redundant path commands that don't draw anything.
Expand Down
25 changes: 10 additions & 15 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ function filters(
var sdata = data,
circle;

const sagitta = command === 'a' ? calculateSagitta(data) : undefined;

if (command === 's') {
sdata = [0, 0].concat(data);

Expand Down Expand Up @@ -645,16 +643,12 @@ function filters(

// round arc radius more accurately
// eg m 0 0 a 1234.567 1234.567 0 0 1 10 0 -> m 0 0 a 1235 1235 0 0 1 10 0
if (
params.smartArcRounding &&
command === 'a' &&
sagitta !== undefined &&
precision
) {
const sagitta = command === 'a' ? calculateSagitta(data) : undefined;
if (params.smartArcRounding && sagitta !== undefined && precision) {
for (let precisionNew = precision; precisionNew >= 0; precisionNew--) {
const radius = toFixed(data[0], precisionNew);
const sagittaNew = /** @type {number} */ (
calculateSagitta([radius, radius].concat(data.slice(2)))
calculateSagitta([radius, radius, ...data.slice(2)])
);
if (Math.abs(sagitta - sagittaNew) < error) {
data[0] = radius;
Expand Down Expand Up @@ -1100,15 +1094,16 @@ function isCurveStraightLine(data) {
* Calculates the sagitta of an arc if possible.
*
* @type {(data: number[]) => number | undefined}
* @see https://wikipedia.org/wiki/Sagitta_(geometry)#Formulas
*/
function calculateSagitta(data) {
const rx = data[0],
ry = data[1],
distance = Math.sqrt(Math.pow(data[5], 2) + Math.pow(data[6], 2));
if (data[3] === 1) return undefined;

const [rx, ry] = data;
if (Math.abs(rx - ry) > error) return undefined;
if (distance / 2 > rx) return undefined;
if (data[3] == 1) return undefined;
return rx - Math.sqrt(Math.pow(rx, 2) - 0.25 * Math.pow(distance, 2));
const chord = Math.sqrt(data[5] ** 2 + data[6] ** 2);
if (chord > rx * 2) return undefined;
return rx - Math.sqrt(rx ** 2 - 0.25 * chord ** 2);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/plugins/convertPathData.32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/plugins/convertPathData.33.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c4b425

Please sign in to comment.