Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatalin Mititiuc <Catalin.Mititiuc@gmail.com>2024-03-22 17:00:17 -0700
committerCatalin Mititiuc <Catalin.Mititiuc@gmail.com>2024-03-22 17:07:00 -0700
commit9f8c563c9844c3f6cc1a14614b1debdad6ec2051 (patch)
tree5606208fefdab9b5b47e6c2c2bd12edf612f0430
parent7d9073eebf14e9dfb74799fa6c6658b904051ed2 (diff)
Refactor large function
-rw-r--r--index.js36
1 files changed, 8 insertions, 28 deletions
diff --git a/index.js b/index.js
index 5d68314..ef7cdef 100644
--- a/index.js
+++ b/index.js
@@ -18,41 +18,21 @@ function edgePoint(x1, y1, x2, y2, maxX, maxY) {
yIntercept = x => (x - x1) * yDiff / xDiff + y1;
if (xDiff > 0 && yDiff > 0) {
- let yWhenXisMax = yIntercept(maxX);
- let xWhenYisMax = xIntercept(maxY);
+ let x = xIntercept(maxY);
- if (xWhenYisMax <= maxX) {
- pointCoords = [xWhenYisMax, maxY];
- } else {
- pointCoords = [maxX, yWhenXisMax];
- }
+ pointCoords = x <= maxX ? [x, maxY] : [maxX, yIntercept(maxX)];
} else if (xDiff > 0 && yDiff < 0) {
- let yWhenXisMax = yIntercept(maxX);
- let xWhenYisZero = xIntercept(0);
+ let y = yIntercept(maxX);
- if (yWhenXisMax >= 0) {
- pointCoords = [maxX, yWhenXisMax];
- } else {
- pointCoords = [xWhenYisZero, 0];
- }
+ pointCoords = y >= 0 ? [maxX, y] : [xIntercept(0), 0];
} else if (xDiff < 0 && yDiff < 0) {
- let yWhenXisZero = yIntercept(0);
- let xWhenYisZero = xIntercept(0);
+ let x = xIntercept(0);
- if (xWhenYisZero >= 0) {
- pointCoords = [xWhenYisZero, 0];
- } else {
- pointCoords = [0, yWhenXisZero];
- }
+ pointCoords = x >= 0 ? [x, 0] : [0, yIntercept(0)];
} else {
- let yWhenXisZero = yIntercept(0);
- let xWhenYisMax = xIntercept(maxY);
+ let y = yIntercept(0);
- if (yWhenXisZero <= maxY) {
- pointCoords = [0, yWhenXisZero];
- } else {
- pointCoords = [xWhenYisMax, maxY];
- }
+ pointCoords = y <= maxY ? [0, y] : [xIntercept(maxY), maxY];
}
return pointCoords;