Challenge: Sherlock and Planes

Scor cont: 20.0 / 20

Submission status: Accepted

Submission score: 1.0

Submission ID: 464733947

Limbaj: java8

Link challenge: https://www.hackerrank.com/challenges/sherlock-and-planes/problem

Cerinta

Watson gives four 3-dimensional points to Sherlock and asks him if they all lie in the same plane. Your task here is to help Sherlock.   

**Input Format**  
First line contains _T_, the number of testcases.  
Each test case consists of four lines. Each line contains three integers, denoting _x<sub>i</sub> y<sub>i</sub> z<sub>i</sub>_.   

**Output Format**  
For each test case, print `YES` or `NO` whether all four points lie in same plane or not, respectively.  

**Constraints**  
1 ≤ T ≤ 10<sup>4</sup>  
-10<sup>3</sup> ≤ x<sub>i</sub>,y<sub>i</sub>,z<sub>i</sub> ≤ 10<sup>3</sup>  

**Sample Input**

	1
    1 2 0
    2 3 0
    4 0 0
    0 0 0
    
**Sample Output**

	YES
    
**Explanation**  
All points have _z<sub>i</sub> = 0_, hence they lie in the same plane, and output is `YES`

Cod sursa

//https://www.hackerrank.com/challenges/sherlock-and-planes
import java.io.*;

public class Solution {

    public static void main(String[] args) throws IOException {
        StringBuffer sb = new StringBuffer();
        
        //INPUT
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(short T = Short.parseShort(br.readLine()); T > 0; --T){
            Point3D a = getPoint(br.readLine());
            Point3D b = getPoint(br.readLine());
            Point3D c = getPoint(br.readLine());
            Point3D d = getPoint(br.readLine());
            
            //SOLVE
            boolean isCoplanar = 0 == dotProduct(
                normal(a, b, c), 
                vector(a, d)
            );
            
            //OUTPUT
            sb.append((isCoplanar)? "YES\n" : "NO\n");
        }
        System.out.print(sb);
    }
    
    private static Point3D getPoint(String str){
        String[] coords = str.split(" ");
        return new Point3D(
            Short.parseShort(coords[0]),
            Short.parseShort(coords[1]),
            Short.parseShort(coords[2])
        );
    }
    
    private static int dotProduct(Point3D v1, Point3D v2){
        return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
    }
    
    private static Point3D normal(Point3D p1, Point3D p2, Point3D p3){
        final Point3D a = vector(p1, p2);
        final Point3D b = vector(p1, p3);
        return new Point3D(
            a.y*b.z - a.z*b.y,
            a.z*b.x - a.x*b.z,
            a.x*b.y - a.y*b.x
        );
    }
    
    private static Point3D vector(Point3D p1, Point3D p2){
        return new Point3D(
            p2.x - p1.x,
            p2.y - p1.y,
            p2.z - p1.z
        );
    }
    
    public static class Point3D{
        int x, y, z;
        public Point3D(int x, int y, int z){
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}
HackerRank Geometry – Sherlock and Planes