-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollinearity.java
More file actions
27 lines (26 loc) · 1.28 KB
/
Collinearity.java
File metadata and controls
27 lines (26 loc) · 1.28 KB
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
//8th kyu
//You are given two vectors starting from the origin (x=0, y=0) with coordinates (x1,y1) and (x2,y2).
//Your task is to find out if these vectors are collinear.
//Collinear vectors are vectors that lie on the same straight line.
//They can be directed in the same or opposite directions. One vector can be obtained from another by multiplying it by a certain number.
//In terms of coordinates, vectors (x1, y1) and (x2, y2) are collinear if (x1, y1) = (k*x2, k*y2) , where k is any number acting as a coefficient.
//All vectors start from the origin (x=0, y=0).
//Be careful when handling cases where x1, x2, y1, or y2 are zero to avoid division by zero errors.
//A vector with coordinates (0, 0) is collinear to all vectors.
//Ex: collinearity(0, 0, 1, 1) --> true
public class Collinearity {
public static boolean collinearity(int x1, int y1, int x2, int y2) {
if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0)){
return true;
}
else if (x1 * y2 - y1 * x2 == 0){
return true;
}
else{
return false;
}
}
public static void main(String[] args){
System.out.println(collinearity(0, 3, 1, 2));
}
}