samplePointSegments method
Sample the function into contiguous finite runs within xMin..xMax.
Invalid samples split runs so graph renderers do not join across
discontinuities such as vertical asymptotes.
Implementation
List<List<Map<String, double>>> samplePointSegments({
double xMin = -10,
double xMax = 10,
int numPoints = 200,
}) {
if (!isValid ||
!xMin.isFinite ||
!xMax.isFinite ||
xMax <= xMin ||
!(xMax - xMin).isFinite ||
numPoints < 2) {
return [];
}
final step = (xMax - xMin) / (numPoints - 1);
final segments = <List<Map<String, double>>>[];
var currentSegment = <Map<String, double>>[];
void flushSegment() {
if (currentSegment.isNotEmpty) segments.add(currentSegment);
currentSegment = <Map<String, double>>[];
}
for (int i = 0; i < numPoints; i++) {
final x = xMin + i * step;
try {
final y = evaluator(x);
if (y.isFinite) {
if (currentSegment.isNotEmpty) {
final previous = currentSegment.last;
final previousX = previous['x']!;
final previousY = previous['y']!;
try {
final midpointY = evaluator((previousX + x) / 2);
final expectedMidpointY = (previousY + y) / 2;
final scale = [
1.0,
previousY.abs(),
y.abs(),
midpointY.abs(),
].reduce((a, b) => a > b ? a : b);
if (!midpointY.isFinite ||
(midpointY - expectedMidpointY).abs() > 0.5 * scale) {
flushSegment();
}
} catch (_) {
flushSegment();
}
}
currentSegment.add({'x': x, 'y': y});
} else {
flushSegment();
}
} catch (_) {
flushSegment();
}
}
flushSegment();
return segments;
}