【Java】三角形内外点判定_Ver2
コード更新致しました。
固有処理基底クラスを更新したので、更新したコードを紹介いたします。
import static java.lang.Double.parseDouble;
import static java.lang.System.out;
import volume0.BaseExe;
public class APointInATriangle extends BaseExe {
public static void main(String[] args) {
new APointInATriangle().exeSysIn();
}
@Override
protected void execute() throws Exception {
while (judgeInData()) {
Point[] points = getPoints(getInData().split(" "));
Point[] vect = getVects(points);
double[] vectProducts = getProducts(vect);
if (judgeInner(vectProducts)) {
out.println("YES");
} else {
out.println("NO");
}
}
}
private boolean judgeInner(double[] vectProducts) {
return (judgeIsClockWise(vectProducts) || judgeIsNotClockWise(vectProducts));
}
private boolean judgeIsClockWise(double[] vectProducts) {
return (0.0 < vectProducts[0] && 0.0 < vectProducts[1] && 0.0 < vectProducts[2]);
}
private boolean judgeIsNotClockWise(double[] vectProducts) {
return (0.0 > vectProducts[0] && 0.0 > vectProducts[1] && 0.0 > vectProducts[2]);
}
private Point[] getPoints(String[] inDataList) {
Point[] points = new Point[4];
for (int index = 0; index < 4; index++) {
points[index] = new Point(parseDouble(inDataList[index * 2]),
parseDouble(inDataList[index * 2 + 1]));
}
return points;
}
private Point[] getVects(Point[] points) {
Point[] vect = new Point[6];
vect[0] = getXY(points[0], points[1]);
vect[1] = getXY(points[1], points[2]);
vect[2] = getXY(points[2], points[0]);
vect[3] = getXY(points[0], points[3]);
vect[4] = getXY(points[1], points[3]);
vect[5] = getXY(points[2], points[3]);
return vect;
}
private Point getXY(Point a, Point b) {
return new Point(b.getX() - a.getX(), b.getY() - a.getY());
}
private double[] getProducts(Point[] vect) {
double[] vectProducts = new double[3];
vectProducts[0] = getProd(vect[0], vect[3]);
vectProducts[1] = getProd(vect[1], vect[4]);
vectProducts[2] = getProd(vect[2], vect[5]);
return vectProducts;
}
private double getProd(Point ab, Point ap) {
return ab.getX() * ap.getY() - ab.getY() * ap.getX();
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
@Override
public String toString() {
return "x : " + this.x + ",y : " + this.y;
}
}
テストドライバは以下になります。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import volume0.BaseTest;
public class APointInATriangleTest extends BaseTest {
private APointInATriangle apiat;
@Before
public void setUp() throws Exception {
super.setUp();
apiat = new APointInATriangle();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void test() {
apiat.exeFileIn("./data/volume0_0012/in.txt");
assertOutList("./data/volume0_0012/out.txt");
}
}
テストドライバ内で使用している「」「」の内容は以下になります。
in.txt
0.0 0.0 2.0 0.0 2.0 2.0 1.5 0.5
2.0 0.0 2.0 2.0 0.0 0.0 1.5 0.5
2.0 2.0 0.0 0.0 2.0 0.0 1.5 0.5
2.0 2.0 2.0 0.0 0.0 0.0 1.5 0.5
2.0 0.0 0.0 0.0 2.0 2.0 1.5 0.5
0.0 0.0 2.0 2.0 2.0 0.0 1.5 0.5
0.0 0.0 1.0 4.0 5.0 3.0 -1.0 3.0
1.0 4.0 5.0 3.0 0.0 0.0 -1.0 3.0
5.0 3.0 0.0 0.0 1.0 4.0 -1.0 3.0
0.0 0.0 5.0 3.0 1.0 4.0 -1.0 3.0
1.0 4.0 0.0 0.0 5.0 3.0 -1.0 3.0
5.0 3.0 1.0 4.0 0.0 0.0 -1.0 3.0
0.0 0.0 2.0 0.0 2.0 2.0 1.0 0.0
out.txt
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
Ver1で問題の解説等を行なっているので、そちらも参照してください。
アルゴリスムで困った時は、以下の本を参考にしてます。Javaに置き換えるのが少々難解ですが、、、
固有処理の基底クラスは以下の記事に記載していますので、参考にしてください。
テストドライバで継承しているクラスは以下の記事に記載していますので、参考にしてみてください。