refineIntersection function
Refines the intersection point between a line segment and a sphere.
The start
and end
parameters represent the endpoints of the line segment.
The center
parameter represents the center of the sphere.
The radius
parameter specifies the radius of the sphere.
Returns the refined intersection point as an Offset.
Implementation
Offset refineIntersection(
Offset start, Offset end, Offset center, double radius) {
double startDistance = (start - center).distance - radius;
// double endDistance = (end - center).distance - radius;
for (int i = 0; i < 10; i++) {
Offset midpoint = (start + end) / 2;
double midDistance = (midpoint - center).distance - radius;
if (midDistance.abs() < 1) {
return midpoint;
}
if (startDistance.sign == midDistance.sign) {
start = midpoint;
startDistance = midDistance;
} else {
end = midpoint;
// endDistance = midDistance;
}
}
return (start + end) / 2;
}