Write a method that returns the intersection of two straight lines. The method header is: public static double [] getIntersectingPoint (double [] [] points)

Topic:
Write a method that returns the intersection of two straight lines.
The method header is: public static double [] getIntersectingPoint (double [] [] points) The two points on the first line are (x1, y1) and (x2, y2), and the two points on the second line are (X3, y3) and (x4, y4), these points are stored in a 4 x 2 two-dimensional matrix points, where (points [0] [0], points [0] [1]) represents (x1, y1 ).
The method returns the intersection, or null if the two lines are parallel.
Write a program that prompts the user to enter four points and displays the intersection.

源代码如下:
package com;
import java.util.Arrays;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
System.out.println(“请输入4个坐标的值:”);
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.nextInt();
}
}
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] + “,”);
}
}
}
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 = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]);
double K2 = (points[3][1] - points[2][1]) / (points[3][0] - points[2][0]);
double[] arr=new double[2];
if (K1 != K2) {
double B1 = y1 - K1 * x1;
double B2 = y3 - K2 * x3;
arr[0]= (B2 - B1) / (K1 - K2);
arr[1]= (B2 * K1 - B1 * K2) / (K1 - K2);
} else {
System.out.println(“NULL”);
}
return arr;
}
}

The main thing is to find the intersection algorithm.
This article is original, please indicate the source.
If it helps you, please give me a thumbs up!

Published 9 original articles · won 9 · visited 2162

Guess you like

Origin blog.csdn.net/grandniu/article/details/105617432