You are on page 1of 4

package UnitTest;

import java.util.Scanner;
public class Test {
static Scanner s = new Scanner(System.in);
//double x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, px = 0, py = 0;
public static void main(String[] args) {

double x1 = readValue();
double y1 = readValue();
double x2 = readValue();
double y2 = readValue();
double x3 = readValue();
double y3 = readValue();
double px = readValue();
double py = readValue();
double [] lines = getLines(x1, y1, x2, y2, x3, y3);
isInsidePolygon(lines, x1,y1,x2,y2,x3,y3,px,py);
}
private static double readValue(){
return s.nextDouble();
};
private static void isInsidePolygon(
double[] lines,
double x1, double y1,
double x2, double y2,
double x3, double y3,
double px, double py){
//double[] pxSolution = solvePx(lines, px);
//returns 3 x coordinates for cuts with X1X2, X1X3, X2X3. If coo
rdinate at [n] is not a solution, coordinate at [n+3] is 0.
double[] pySolution = solvePy(lines, y1, y2, y3, py);
//Handle NaN results due to vertical vertices
for(int i = 0; i < 6; i++){
if (Double.isNaN(pySolution[i])) {
if(i == 0){
pySolution[i] = x1;
}else if(i == 1){
pySolution[i] = x2;
}else if(i == 2){
pySolution[i] = x3;
}
}
}
int index = 0;
double [] finalSol = new double[2];
for(int i = 0; i < pySolution.length/2; i++){
if( pySolution[i+3] == 1){
finalSol[index] = pySolution[i];
index++;
System.out.println("Stored x Sol " + i + " at: "
+ (index-1));
}
}
if ( (finalSol[0] < px && px < finalSol[1]) || (finalSol[1] <
px && px < finalSol[0]) ){
System.out.println("Yay!");
} else if ( finalSol[0] == finalSol[1] && finalSol[0] == px){
System.out.println("Point is on top of a corner");
}
for(int i = 0; i < 6; i ++ ) System.out.println(pySolution[i]
);

}
private static double[] getLines( double x1, double y1, double x2, doubl
e y2,double x3, double y3) {
// y = mx + b
double m12 = (y2-y1)/(x2-x1);
double b12 = y2 - m12*x2;
double m13 = (y3-y1)/(x3-x1);
double b13 = y3 - m13*x3;
double m23 = (y3-y2)/(x3-x2);
double b23 = y3 - m23*x3;
double[] values = {m12, b12, m13, b13, m23, b23};
return values;
}
private static double[] solvePy(double[] lines, double y1, double y2, do
uble y3, double py) {
double m12 = lines[0];
double b12 = lines[1];
double m13 = lines[2];
double b13 = lines[3];
double m23 = lines[4];
double b23 = lines[5];
double[] ySolutions = {0, 0, 0, 0, 0, 0};
//Restrict lines to turn them into vertices/segment of lines 0
5 0
if(y1 < y2){
if( (y2 >= py && py >= y1) ){
//Solution in y2 -- y1
ySolutions[0] = (py - b12)/m12;
ySolutions[3] = 1;
System.out.println("Meets vertice m12b12");
}
}else if(y1 > y2){
if(y1 >= py && py >= y2){
//Solution in y1 -- y2
ySolutions[0] = (py - b12)/m12;
ySolutions[3] = 1;
System.out.println("Meets vertice m12b12--------
------");
}
}else{ //y1==y2
if(y1 == py){
System.out.println("Point is over vertice m12b12
");
ySolutions[0] = (py - b12)/m12;
ySolutions[3] = 1;
}
}
if(y1 < y3){
if(y3 >= py && py >= y1){
//Solution in y3 -- y1
ySolutions[1] = (py - b13)/m13;
ySolutions[4] = 1;
System.out.println("Meets vertice m13b13");
}
}else if(y1 > y3){
if(y1 >= py && py >= y3){
//Solution in y1 -- y3
ySolutions[1] = (py - b13)/m13;
ySolutions[4] = 1;
System.out.println("Meets vertice m13b13");
}
}else{ //y1==y3
if(y1 == py){
ySolutions[1] = (py - b13)/m13;
ySolutions[4] = 1;
System.out.println("Point is over vertice m13b13
");
}
}

if(y2 < y3){ //0 5 0


if(y3 >= py && py >= y2){
//Solution in y3 -- y2
ySolutions[2] = (py - b23)/m23;
ySolutions[5] = 1;
System.out.println("Meets vertice m23b23");
}
}else if(y2 > y3){
if(y2 >= py && py >= y3){
//Solution in y2 -- y3
ySolutions[2] = (py - b23)/m23;
ySolutions[5] = 1;
System.out.println("Meets vertice m23b23");
}
}else{ //y2==y3
if(y2 == py){
ySolutions[2] = (py - b23)/m23;
ySolutions[5] = 1;
System.out.println("Point is over vertice m23b23
");
}
}
return ySolutions;
}
}

You might also like