Skip to content

Commit 81c3b68

Browse files
committed
Problem 1232
1 parent 399fd83 commit 81c3b68

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Problem 1232: Check if it is a Straight Line
3+
* Prompt: You are given an array coordinates, coordinates[i] = [x, y],
4+
* where [x, y] represents the coordinate of a point. Check if these
5+
* points make a straight line in the XY plane.
6+
* Date: 05/08/2020
7+
*/
8+
class Solution {
9+
public boolean checkStraightLine(int[][] coordinates) {
10+
int x_ini = coordinates[0][0];
11+
int y_ini = coordinates[0][1];
12+
//<v1, v2> is the vector created by the first two points
13+
int v1 = coordinates[1][0] - x_ini;
14+
int v2 = coordinates[1][1] - y_ini;
15+
for (int i = 2; i < coordinates.length; ++i)
16+
{
17+
//<u1, u2> is the vector created by the
18+
//current point and the first point
19+
int u1 = coordinates[i][0] - x_ini;
20+
int u2 = coordinates[i][1] - y_ini;
21+
if (v1 * u2 - u1 * v2 != 0)
22+
return false;
23+
}
24+
return true;
25+
}
26+
}
27+
28+
/**
29+
* Notes: record each points' relationship with the starting
30+
* point as a vector, and check if two vectors are parallel
31+
* using the pseudo cross product.
32+
*/

0 commit comments

Comments
 (0)