epezent / implot

Immediate Mode Plotting
MIT License
4.55k stars 503 forks source link

Division by zero in Intersection of parallel segments #471

Closed iassenev closed 11 months ago

iassenev commented 1 year ago

The Intersection function is called from the RendererShaded::Render for parallel segments, which results in division by zero:

static inline ImVec2 Intersection(const ImVec2& a1, const ImVec2& a2, const ImVec2& b1, const ImVec2& b2) {
    float v1 = (a1.x * a2.y - a1.y * a2.x);  float v2 = (b1.x * b2.y - b1.y * b2.x);
    float v3 = ((a1.x - a2.x) * (b1.y - b2.y) - (a1.y - a2.y) * (b1.x - b2.x));
    return ImVec2((v1 * (b1.x - b2.x) - v2 * (a1.x - a2.x)) / v3, (v1 * (b1.y - b2.y) - v2 * (a1.y - a2.y)) / v3);
}

v3 is zero for parallel segments.

Easy hotfix in RendererShaded::Render seems to fix the issue: ImVec2 intersection = intersect ? Intersection(P11,P21,P12,P22) : ImVec2{ 0, 0 };

epezent commented 11 months ago

https://github.com/epezent/implot/commit/433022038bdf498741147cba277fd283dc0ac68d