第三章第二十五題(幾何:交點)(Geometry: intersecting point)
第三章第二十五題(幾何:交點)(Geometry: intersecting point)
-
*3.25(幾何:交點)兩條直線的交點可以通過下面的線性方程組求解:
這個線性方程組可以應用Cramer法則求解(見程式設計練習題3.3)。如果方程無解,則兩條直線平行。
編寫一個程式,提示使用者輸入這四個點,然後顯示它們的交點。
下面是這個程式的執行示例:
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at (2.88889, 1.1111)
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
The two lines are parallel*3.25(Geometry: intersecting point) The intersecting point of the two lines can be found by solving the following linear equations:
This linear equation can be solved using Cramer’s rule (see Programming Exercise 3.3). If the equation has no solutions, the two lines are parallel.
Write a program that prompts the user to enter four points and displays the intersecting point.Here are sample runs:
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at (2.88889, 1.1111)
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
The two lines are parallel -
參考程式碼:
package chapter03;
import java.util.Scanner;
public class Code_25 {public static void main(String[] args) {
double x1, y1, x2, y2, x3, y3, x4, y4;
double a, b, c, d, e, f, x, y, discriminant;
System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");
Scanner input = new Scanner(System.in);
x1 = input.nextDouble(); y1 = input.nextDouble();
x2 = input.nextDouble(); y2 = input.nextDouble();
x3 = input.nextDouble(); y3 = input.nextDouble();
x4 = input.nextDouble(); y4 = input.nextDouble();
a = y1 - y2;
b = x2 - x1;
c = y3 - y4;
d = x4 - x3;
e = a * x1 + b * y1;
f = c * x3 + d * y3;
discriminant = a * d - b * c;
if(discriminant != 0)
{
x = (e * d - b * f) / discriminant;
y = (a * f - e * c) / discriminant;
System.out.println("The intersecting point is at(" + x + ", " + y + ")");
}
else
System.out.println("The two lines are parallel");
input.close();
}
}
- 結果顯示:
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at(2.888888888888889, 1.1111111111111112)
Process finished with exit code 0
相關文章
- 第九章第十二題(幾何:交點)(Geometry: Intersections)
- 第三章第二十九題(幾何:兩個圓)(Geometry: two circles)
- three.js基礎之幾何體Curve、GeometryJS
- 不可不知的WPF幾何圖形(Geometry)
- WPF繪圖(一):幾何(Geometry)與形狀(Shape)繪圖
- 【第一道計算幾何題】 UVA11178 Morley‘s Theorem (二維幾何,旋轉直線求求交點)REM
- 第九章第九題(幾何:正多邊形)(Geometry: regular polygons)Go
- POJ 1039-Pipe(計算幾何-線段相交、求交點)
- WPF 反射載入Geometry幾何圖形資料圖示反射
- Civil 3D 2013新API -- 建立空間幾何點(COGO Point)3DAPIGo
- Using Geometry to Detect Grasp Poses in 3D Point Clouds3DCloud
- POJ 1408-Fishnet(計算幾何-根據交點求多邊形面積)
- 異構幾何問題
- ABC 355 D題Intersecting Intervals
- 計算幾何——平面最近點對
- Android每週熱點第二十五期Android
- 幾道經典的幾何作圖趣題
- JSXGraph幾何繪圖證明解答【五點共圓】問題JS繪圖
- 數學趣題:平面幾何(一)
- 數學趣題:平面幾何(二)
- 俄羅斯總理的幾何題
- [每日一題] 第二十五題:二叉樹的映象每日一題二叉樹
- 17點圖:xy軸的幾何圖案
- 【計算幾何】多邊形點集排序排序
- D - Intersecting Intervals
- hdu5365 簡單幾何問題
- 平面幾何
- Mysql系列第二十五講 mysql如何確保資料不丟失?有幾點值得我們借鑑MySql
- 【計算幾何】點在多邊形內部
- Multiple View Geometry(多檢視幾何)學習筆記(9)—無窮遠平面&絕對二次曲線View筆記
- 閃回還原點restore pointREST
- 不動點迭代(Fixed Point Iteration)
- 【計算幾何】求線段相交交點座標
- 計算幾何
- 前端週刊第二十五期前端
- BZOJ 1043: [HAOI2008]下落的圓盤 計算幾何,貪心,線段交
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- 計算幾何:模板