求凸多边形面积:
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] xpoints = new int[n + 1];
int[] ypoints = new int[n + 1];
double sum = 0;
for (int i = 0; i < n; i++) {
xpoints[i] = reader.nextInt();
ypoints[i] = reader.nextInt();
}
//再把第一个点丢到最后
xpoints[n] = xpoints[0];
ypoints[n] = ypoints[0];
for (int i = 0; i < n; i++) {
sum += (xpoints[i] * ypoints[i + 1]) - (xpoints[i + 1] * ypoints[i]);
}
sum /= 2;
System.out.println(String.format("%.0f", sum));
}
从stackOverFlow抄的。
然后是判断点是否在凸多边形内,用java自带的多边形类搞定(为什么这个类不带求面积???):
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
while (reader.hasNext()) {
int n = reader.nextInt();
Polygon polygon = new Polygon();
for (int i = 0; i < n; i++) {
int x = reader.nextInt();
int y = reader.nextInt();
polygon.addPoint(x, y);
}
int m = reader.nextInt();
boolean ans = true;
for (int i = 0; i < m; i++) {
int x = reader.nextInt();
int y = reader.nextInt();
ans &= polygon.contains(new Point(x, y));
}
System.out.println(ans ? "YES" : "NO");
}
}