在计算几何中经常需要从一个点集中找到最右下角的点。编写以下方法,从一个点的集合中返回最右下角的点:
public static double[] getRightmostLowestPoint(double[][] points)
编写一个测试程序,提示用户输入6个点的坐标,然后显示最右下角的点。
package pack2;
import java.util.Scanner;
public class RightMostLowestPoint {
public static void main(String[] args) {
rightMostLowestPoint();
}
// 1.5 2.5 -3 4.5 5.6 -7 6.5 -7 8 1 10 2.5
public static void rightMostLowestPoint() {
try(Scanner input = new Scanner(System.in);) {
final int COLUMN = 2;
System.out.print("Enter the number of points: ");
double[][] points = new double[input.nextInt()][COLUMN];
System.out.print("Enter " + points.length + " points: ");
inputData(input, points);
double[] point = getRightMostLowestPoint(points);
System.out.println("The rightmost lowest point is (" + point[0] + ", "
+ point[1] + ")");
}
}
/**接收数据输入*/
public static void inputData(Scanner input, double[][] points) {
for (int i = 0; i < points.length; i++)
for (int j = 0; j < points[i].length; j++)
points[i][j] = input.nextDouble();
}
/**最右下的点*/
public static double[] getRightMostLowestPoint(double[][] points) {
double[] mostLowestPoint = new double[2];
for (int i = 0; i < points.length; i++)
if((points[i][0] >= mostLowestPoint[0]) && (points[i][1] <=
mostLowestPoint[1])) {
mostLowestPoint[0] = points[i][0];
mostLowestPoint[1] = points[i][1];
}
return mostLowestPoint;
}
}