class Solution {
public boolean inLine(double x, double y, double x1, double y1, double x2, double y2) {
if (y >= Math.min(y1, y2) && y <= Math.max(y1, y2) &&
x >= Math.min(x1, x2) && x <= Math.max(x1, x2)) return true;
return false;
}
public double[] intersection(int[] start1, int[] end1, int[] start2, int[] end2) {
int dx1 = start1[0] - end1[0];
int dy1 = start1[1] - end1[1];
int dx2 = start2[0] - end2[0];
int dy2 = start2[1] - end2[1];
if (dx1 == 0 && dx2 != 0) {
double k2 = (double)dy2 / dx2;
double b2 = (double)start2[1] - k2 * start2[0];
double x = start1[0];
double y = k2 * x + b2;
if (inLine(x, y, start1[0], start1[1], end1[0], end1[1]) &&
inLine(x, y, start2[0], start2[1], end2[0], end2[1])) {
return new double[]{
x, y};
}
} else if (dx2 == 0 && dx1 != 0) {
double k1 = (double)dy1 / dx1;
double b1 = (double)start1[1] - k1 * start1[0];
double x = start2[0];
double y = k1 * x + b1;
if (inLine(x, y, start1[0], start1[1], end1[0], end1[1]) &&
inLine(x, y, start2[0], start2[1], end2[0], end2[1])) {
return new double[]{
x, y};
}
} else if (dx2 == 0 && dx1 == 0) {
if (start1[0] == start2[0]) {
int x = start1[0];
int y1_h, y1_l, y2_h, y2_l;
if (Math.min(start1[1], end1[1]) >= Math.min(start2[1], end2[1])) {
y1_l = Math
LeetCode刷题记203-Geometry(几何)
最新推荐文章于 2022-12-11 16:22:16 发布
本文记录了LeetCode中涉及几何的几道经典题目,包括找到两条线段的交点,平分正方形的问题,以及实际场景中的安装栅栏问题和服务中心选址的优化问题。
摘要由CSDN通过智能技术生成