From 108c4fcd56363f77b9ab13892b876479d246958d Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Fri, 13 Nov 2015 22:15:23 -0500 Subject: [PATCH] Simplify code for capping bezier control points. Cap the X direction as well. --- src/controllers/controller.line.js | 24 ++++-------------------- src/controllers/controller.radar.js | 24 ++++-------------------- 2 files changed, 8 insertions(+), 40 deletions(-) diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index 954686e06..c22df9f97 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -251,28 +251,12 @@ point._model.tension ); - point._model.controlPointPreviousX = controlPoints.previous.x; - point._model.controlPointNextX = controlPoints.next.x; - // Prevent the bezier going outside of the bounds of the graph + point._model.controlPointPreviousX = Math.max(Math.min(controlPoints.previous.x, this.chart.chartArea.right), this.chart.chartArea.left); + point._model.controlPointPreviousY = Math.max(Math.min(controlPoints.previous.y, this.chart.chartArea.bottom), this.chart.chartArea.bottom); - // Cap outer bezier handles to the upper/lower scale bounds - if (controlPoints.next.y > this.chart.chartArea.bottom) { - point._model.controlPointNextY = this.chart.chartArea.bottom; - } else if (controlPoints.next.y < this.chart.chartArea.top) { - point._model.controlPointNextY = this.chart.chartArea.top; - } else { - point._model.controlPointNextY = controlPoints.next.y; - } - - // Cap inner bezier handles to the upper/lower scale bounds - if (controlPoints.previous.y > this.chart.chartArea.bottom) { - point._model.controlPointPreviousY = this.chart.chartArea.bottom; - } else if (controlPoints.previous.y < this.chart.chartArea.top) { - point._model.controlPointPreviousY = this.chart.chartArea.top; - } else { - point._model.controlPointPreviousY = controlPoints.previous.y; - } + point._model.controlPointNextX = Math.max(Math.min(controlPoints.next.x, this.chart.chartArea.right), this.chart.chartArea.left); + point._model.controlPointNextY = Math.max(Math.min(controlPoints.next.y, this.chart.chartArea.bottom), this.chart.chartArea.top); // Now pivot the point for animation point.pivot(); diff --git a/src/controllers/controller.radar.js b/src/controllers/controller.radar.js index b8bcf1441..5e452820d 100644 --- a/src/controllers/controller.radar.js +++ b/src/controllers/controller.radar.js @@ -197,28 +197,12 @@ point._model.tension ); - point._model.controlPointPreviousX = controlPoints.previous.x; - point._model.controlPointNextX = controlPoints.next.x; - // Prevent the bezier going outside of the bounds of the graph + point._model.controlPointPreviousX = Math.max(Math.min(controlPoints.previous.x, this.chart.chartArea.right), this.chart.chartArea.left); + point._model.controlPointPreviousY = Math.max(Math.min(controlPoints.previous.y, this.chart.chartArea.bottom), this.chart.chartArea.bottom); - // Cap outer bezier handles to the upper/lower scale bounds - if (controlPoints.next.y > this.chart.chartArea.bottom) { - point._model.controlPointNextY = this.chart.chartArea.bottom; - } else if (controlPoints.next.y < this.chart.chartArea.top) { - point._model.controlPointNextY = this.chart.chartArea.top; - } else { - point._model.controlPointNextY = controlPoints.next.y; - } - - // Cap inner bezier handles to the upper/lower scale bounds - if (controlPoints.previous.y > this.chart.chartArea.bottom) { - point._model.controlPointPreviousY = this.chart.chartArea.bottom; - } else if (controlPoints.previous.y < this.chart.chartArea.top) { - point._model.controlPointPreviousY = this.chart.chartArea.top; - } else { - point._model.controlPointPreviousY = controlPoints.previous.y; - } + point._model.controlPointNextX = Math.max(Math.min(controlPoints.next.x, this.chart.chartArea.right), this.chart.chartArea.left); + point._model.controlPointNextY = Math.max(Math.min(controlPoints.next.y, this.chart.chartArea.bottom), this.chart.chartArea.top); // Now pivot the point for animation point.pivot();