Как всегда, не понимаю, что ему не нравится. "Метод isPointInPolygon должен корректно работать для любых входных данных."
package com.javarush.task.task40.task4004;
import java.util.ArrayList;
import java.util.List;
/*
Принадлежность точки многоугольнику
*/
class Point {
public int x;
public int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Lin {
public Point begin, end;
public Lin(Point begin, Point end) {
this.begin = begin;
this.end = end;
}
}
public class Solution {
public static void main(String[] args) {
List<Point> polygon = new ArrayList<>();
polygon.add(new Point(0, 0));
polygon.add(new Point(0, 10));
polygon.add(new Point(10, 10));
polygon.add(new Point(10, 0));
System.out.println(isPointInPolygon(new Point(5, 5), polygon));
System.out.println(isPointInPolygon(new Point(100, 100), polygon));
}
public static boolean isPointInPolygon(Point point, List<Point> polygon) {
//напишите тут ваш код
Solution solution = new Solution();
if (point == null || polygon == null || polygon.size() < 3) {
return false;
}
List<Lin> lines = solution.getLines(polygon);
return solution.pointIntersectsLin(point, lines);
}
private List<Lin> getLines(List<Point> polygon) {
List<Lin> lines = new ArrayList<>();
for (int i = 1; i < polygon.size(); i++) {
lines.add(new Lin(polygon.get(i - 1), polygon.get(i)));
}
lines.add(new Lin(polygon.get(polygon.size() - 1), polygon.get(0)));
return lines;
}
private boolean pointIntersectsLin(Point point, List<Lin> lines) {
int upInts = 0, dnInts = 0, rInts = 0, lInts = 0;
//1 луч вверх пересекает линию
for (Lin line : lines) {
if ((line.begin.x < point.x && line.end.x < point.x) || (line.begin.x > point.x && line.end.x > point.x) ||
(Math.max(line.begin.y, line.end.y) < point.y)) {
continue;
}
int x1, x2, y1, y2, y;
x1 = line.begin.x;
y1 = line.begin.y;
x2 = line.end.x;
y2 = line.end.y;
y = (y2 - y1) / (x2 - x1) * point.x + (y1 - x1 * (y2 - y1) / (x2 - x1));
if (y < point.y) {
continue;
} else {
upInts = upInts + 1;
}
}
if (upInts % 2 == 0) return false;
//2 луч вниз пересекает линию
for (Lin line : lines) {
if ((line.begin.x < point.x && line.end.x < point.x) || (line.begin.x > point.x && line.end.x > point.x) ||
(Math.min(line.begin.y, line.end.y) > point.y)) {
continue;
}
int x1, x2, y1, y2, y;
x1 = line.begin.x;
y1 = line.begin.y;
x2 = line.end.x;
y2 = line.end.y;
y = (y2 - y1) / (x2 - x1) * point.x + (y1 - x1 * (y2 - y1) / (x2 - x1));
if (y > point.y) {
continue;
} else {
dnInts = dnInts + 1;
}
}
if (dnInts % 2 == 0) return false;
//3 луч вправо пересекает линию
for (Lin line : lines) {
if ((line.begin.y < point.y && line.end.y < point.y) || (line.begin.y > point.y && line.end.y > point.y) ||
(Math.max(line.begin.x, line.end.x) < point.x)) {
continue;
}
int x1, x2, y1, y2, x;
x1 = line.begin.x;
y1 = line.begin.y;
x2 = line.end.x;
y2 = line.end.y;
x = x1 + (point.y - y1) / (y2 - y1) * (x2 - x1);
if (x < point.x) {
continue;
} else {
rInts = rInts + 1;
}
}
if (rInts % 2 == 0) return false;
//4 луч влево пересекает линию
for (Lin line : lines) {
if ((line.begin.y < point.y && line.end.y < point.y) || (line.begin.y > point.y && line.end.y > point.y) ||
(Math.min(line.begin.x, line.end.x) > point.x)) {
continue;
}
int x1, x2, y1, y2, x;
x1 = line.begin.x;
y1 = line.begin.y;
x2 = line.end.x;
y2 = line.end.y;
x = x1 + (point.y - y1) / (y2 - y1) * (x2 - x1);
if (x > point.x) {
continue;
} else {
lInts = lInts + 1;
}
}
if (lInts % 2 == 0) return false;
return true;
}
}