-
Notifications
You must be signed in to change notification settings - Fork 0
/
1232. Check If It Is a Straight Line.js
80 lines (62 loc) · 1.98 KB
/
1232. Check If It Is a Straight Line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point.
Check if these points make a straight line in the XY plane.
TIP:
the [slope] value is the smae for each points (if they all are on the same line)
Readings:
https://planetcalc.com/8110/
https://content.byui.edu/file/b8b83119-9acc-4a7b-bc84-efacf9043998/1/Math-2-11-2.html#WS2
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates contains no duplicate point.
*/
/**
* @param {number[][]} coordinates
* @return {boolean}
*/
var checkStraightLine = function(coordinates) {
let n = coordinates.length;
let [ x1, y1 ] = coordinates[0];
let [ xn, yn ] = coordinates.at(-1);
// fast check
// single dot
if (x1 === xn && y1 === yn) { return true; }
// just line of two points
if (coordinates.length === 2) { return true; }
// is vertival line
const isVertical = xn - x1 === 0;
const isHorizontal = yn - y1 === 0;
// slope
const m = isVertical ? 0 : (yn - y1) / (xn - x1);
// y-intercept
// b = y - m * x
const b = y1 - m * x1;
// y = m * x + b
const fy = (x) => m * x + b;
console.log('linear f(x) params', 'm=', m, 'b=', b, 'isVertical',isVertical,'isHorizontal',isHorizontal);
// check points wth the linear function
for (let i = 0; i < n; i++) {
let [ pX, pY ] = coordinates[i];
// vertical line has te same X
if (isVertical && pX !== x1) {
return false;
}
// however, horizontal line has the same Y
if (isHorizontal && pY !== y1) {
return false;
}
// the others, with slope
if (!isVertical && !isHorizontal && pY !== fy(pX)) {
return false;
}
}
return true;
};