The description is very simple. Given the coordinates of some vertices, return true if they are in the same line. The topic description is very simple. Given the coordinates of some vertices, return
TrueIf they are in the same line.
For example[7, 4], Which indicates a point.
onLine([[1,2], [7, 4], [22, 9]]); // returns trueonLine([[1,2], [-3, -14], [22, 9]]); // returns false
Seeing this question, I first came up with a plane in my mind, followed by a Cartesian coordinate system.
In the coordinate system, I used the methods taught by the mathematics teacher in the middle school to draw these points one by one in the coordinate system.
Then, link the vertex with a line segment.
So how can we determine that they are on the same straight line?
I quickly thought of the slope.
If any two points on a straight line are(X1, y1),(X2, y2)Then the straight line slopeK = (y2-y1)/(x2-x1).
If the slope of these line segments is the same, they must be in the same line!
In the program, I only need to judge the slope between line segments and return different results.False.
Write the Code as follows:
Function onLine (points) {var slope; for (var I = 0; I
The above are JavaScript interesting questions: point, line, and surface content. For more information, see PHP Chinese website (www.php1.cn )!