Skip to content

Commit eb342f1

Browse files
authored
Merge pull request #28707 from jsspencer/fix_sketch_divide_by_zero
Avoid division-by-zero in Sketch::Sketch
2 parents 1e98377 + 41afa7d commit eb342f1

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/path_converters.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <cmath>
77
#include <cstdint>
8+
#include <limits>
89

910
#include "agg_clip_liang_barsky.h"
1011
#include "mplutils.h"
@@ -1019,8 +1020,18 @@ class Sketch
10191020
{
10201021
rewind(0);
10211022
const double d_M_PI = 3.14159265358979323846;
1022-
m_p_scale = (2.0 * d_M_PI) / (m_length * m_randomness);
1023-
m_log_randomness = 2.0 * log(m_randomness);
1023+
// Set derived values to zero if m_length or m_randomness are zero to
1024+
// avoid divide-by-zero errors when a sketch is created but not used.
1025+
if (m_length <= std::numeric_limits<double>::epsilon() || m_randomness <= std::numeric_limits<double>::epsilon()) {
1026+
m_p_scale = 0.0;
1027+
} else {
1028+
m_p_scale = (2.0 * d_M_PI) / (m_length * m_randomness);
1029+
}
1030+
if (m_randomness <= std::numeric_limits<double>::epsilon()) {
1031+
m_log_randomness = 0.0;
1032+
} else {
1033+
m_log_randomness = 2.0 * log(m_randomness);
1034+
}
10241035
}
10251036

10261037
unsigned vertex(double *x, double *y)

0 commit comments

Comments
 (0)