From 554c6efda6cb0e30b6e74fe42267265dcf839e84 Mon Sep 17 00:00:00 2001 From: nik0la-vr <37928424+nik0la-vr@users.noreply.github.com> Date: Sun, 21 Apr 2019 19:47:24 +0200 Subject: [PATCH] Properly handling vertical lines --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 628bb32..d51fc46 100644 --- a/index.js +++ b/index.js @@ -332,10 +332,14 @@ function clip (startPoint, endPoint) { return; if (startPoint.x === endPoint.x) { // to prevent division by zero error - if (startPoint.y < endPoint.y) // intersects with top window, the clipped line is (startPoint.x, startPoint.y) to (startPoint.x, clippingWindow.topRight.y) - drawLine(startPoint.x, startPoint.y, endPoint.x, clippingWindow.topRight.y, '#f00'); - else // intersects with bottom window, the clipped line is (startPoint.x, startPoint.y) to (startPoint.x, clippingWindow.bottomLeft.y) - drawLine(startPoint.x, startPoint.y, endPoint.x, clippingWindow.bottomLeft.y, '#f00'); + if (startPoint.y < endPoint.y) { + startPoint.y = Math.max(startPoint.y, clippingWindow.topRight.y); + endPoint.y = Math.min(endPoint.y, clippingWindow.bottomLeft.y); + } else { + startPoint.y = Math.min(startPoint.y, clippingWindow.topRight.y); + endPoint.y = Math.max(endPoint.y, clippingWindow.bottomLeft.y); + } + drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, '#f00'); return; }