java 判斷點是否在一條線段上

public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		Point point1 = new Point();
		Point point2 = new Point();
		double x, y;
		point1.setLocation(5.0, 5.0);
		point2.setLocation(1.0, 1.0);
		System.out.println("請分別輸入點x,y的座標:");
		x = scan.nextDouble();
		y = scan.nextDouble();
		boolean pdline = (x - point1.getX()) * (point1.getY() - point2.getY()) == (point1.getX() - point2.getX())
				* (y - point1.getY());
		if (pdline) {
			System.out.println("您輸入的點在該條直線上");
		} else {
			System.out.println("您輸入的點不在該條直線上");
		}
		scan.close();
	}
相關文章
相關標籤/搜索