図に示すように、4つの頂点を持つ凸多角形は4つの三角形に分割されます。ユーザーに4つの頂点の座標を入力するように要求し、4つの三角形の領域を昇順で表示するプログラムを記述します。

タイトル:
4つの頂点を持つ凸型ポリゴンは、図のように4つの三角形に分割されます。
ユーザーに4つの頂点の座標を入力するように要求し、4つの三角形の領域を昇順で表示するプログラムを記述します。
ここに画像の説明を挿入

源代码以下:
パッケージcom;
import java.util.Scanner;
public class test {
public static void main(String [] args){
System.out.println(“ Enter x1、y1、x2、y2、x3、y3、x4、y4:”);
double [] [] points = new double [4] [2];
Scanner in = new Scanner(System.in);
for(int i = 0; i <4; i ++){
for(int j = 0; j <2; j ++){
points [i] [j] = in.nextDouble();
}
}
double array [] = getIntersectingPoint(points);
System.out.print(“交点:(”);
for(int i = 0; i <2; i ++){
if(i == 1){
System.out.print(array [i] + ")" );
} else {
System.out.print(array [i] +“、”);
}
}
double x = array [0];
double y = array [1];
double x1 = points [0] [0];
double y1 = points [0] [1];
double x2 = points [1] [0];
double y2 = points [1] [1];
double x3 = points [2] [0];
double y3 = points [2] [1];
double x4 = points [3] [0];
double y4 = points [3] [1];
double [] S =新しいdouble [4];
S [0] = Math.abs((x1 y2 + x2 y + x y1-x1 y-x2 y1-x y2)/ 2);
S [1] = Math.abs((x1 y4 + x4 y + x y1-x1 y-x4 y1-x y4)/ 2);
S [2] = Math.abs((x3 y2 + x2 y + x y3-x3 y-x2 y3-x y2)/ 2);
S [3] = Math.abs((x3y4 + x4 y + x y3-x3 y-x4 y3-x y4)/ 2);
バブル(S);
System.out.println();
System.out.print( "領域は");
for(int i = 0; i <4; i ++){
if(i == 3){
System.out.print(S [i]);
} else {
System.out.print(S [i] + "");
}
}
}
public static double [] getIntersectingPoint(double [] [] points){
double x1 = points [0] [0];
double y1 = points [0] [1];
double x2 = points [1] [0];
double y2 = points [1] [1];
double x3 = points [2] [0];
double y3 = points [2] [1];
double x4 = points [3] [0];
double y4 = points [3] [1];
double K1 =(y1-y3)/(x1-x3);
double K2 =(y2-y4)/(x2-x4);
double [] arr = new double [2];
double B1 = y1-K1 x1;
double B2 = y2-K2
x2;
arr [0] =(B2-B1)/(K1-K2);
arr [1] =(B2 * K1-B1 * K2)/(K1-K2);
arrを返します。
}
public static void bubble(double [] S){
double temp;
for(int i = 0; i <S.length; i ++){
for(int j = S.length-1; j> i;
j– ){ if(S [j] <S [j-1]){
temp = S [j-1];
S [j-1] = S [j];
S [j] = temp;
}
}
}
}
}
ここに画像の説明を挿入

この質問の共通部分に関する知識は、以前のブログで見ることができます。
メインの最終領域昇順アルゴリズムは、バブルソートが使用されていることを示しています。これは比較的単純で基本的なものです。
この記事はオリジナルです。出典を明記してください。
この記事が参考になれば、ほめ言葉をください。

リリース9件のオリジナルの記事 ウォンの賞賛9 ビュー2161

おすすめ

転載: blog.csdn.net/grandniu/article/details/105617653