package com.whyc.util;
|
|
import com.whyc.dto.Point;
|
|
public class PointUtil {
|
public static void main(String[] args) {
|
/*double a1 = 1, b1 = 2;
|
double a2 = 3, b2 = 4;
|
double x0 = 9, y0 = 9;
|
Point p1 = new Point(1, 2);
|
Point p2 = new Point(3, 4);
|
Point p0 = new Point(9, 9);
|
|
Point p = findIntersection2(p1, p2, p0);
|
System.out.println(p);
|
|
double[] p3 = findIntersection(a1, b1, a2, b2, x0, y0);
|
System.out.println("Intersection Point P3: (" + p3[0] + ", " + p3[1] + ")");
|
|
String position = determinePosition(a1, b1, a2, b2, x0, y0);
|
System.out.println("Point P is on the " + position + " of the line P1P2.");*/
|
double x0 = 5, y0 = 5;
|
double x1 = 0, y1 = 0;
|
double x2 = 2, y2 = 2;
|
|
double proportion = calculateProportion(x0, y0, x1, y1, x2, y2);
|
if (proportion >= 0) {
|
System.out.println("垂足到mstPointStart的距离占整条线段长度的比例是: " + proportion);
|
} else {
|
System.out.println("垂足不在线段上");
|
}
|
}
|
|
public static double[] findIntersection(double a1, double b1, double a2, double b2, double x0, double y0) {
|
// 检查 p1p2 是否为垂直线
|
if (a2 == a1) {
|
return new double[]{a1, y0};
|
}
|
|
// 检查 p1p2 是否为水平线
|
if (b2 == b1) {
|
return new double[]{x0, b1};
|
}
|
double m = (b2 - b1) / (a2 - a1);
|
double mPerpendicular = (a1 - a2) / (b2 - b1);
|
|
double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m);
|
double y = m * x + (b1 - m * a1);
|
|
return new double[]{x, y};
|
}
|
|
public static Point findIntersection2(Point p1, Point p2, Point p0) {
|
// 检查 p1p2 是否为垂直线
|
int a1 = p1.getX();
|
int b1 = p1.getY();
|
|
int a2 = p2.getX();
|
int b2 = p2.getY();
|
|
int x0 = p0.getX();
|
int y0 = p0.getY();
|
|
if (a2 == a1) {
|
return new Point(a1, y0);
|
}
|
|
// 检查 p1p2 是否为水平线
|
if (b2 == b1) {
|
return new Point(x0, b1);
|
}
|
double m = (b2 - b1) / (a2 - a1);
|
double mPerpendicular = (a1 - a2) / (b2 - b1);
|
|
double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m);
|
double y = m * x + (b1 - m * a1);
|
|
return new Point((int) x, (int) y);
|
}
|
public static String determinePosition(double a1, double b1, double a2, double b2, double x0, double y0) {
|
double cross = (a2 - a1) * (y0 - b1) - (x0 - a1) * (b2 - b1);
|
if (cross > 0) {
|
return "left";
|
} else if (cross < 0) {
|
return "right";
|
} else {
|
return "line";
|
}
|
}
|
|
/**
|
* 计算点到线段的距离占线段长度的比例
|
* @param x0 垂足x点
|
* @param y0 垂足y点
|
* @param x1 起点x点
|
* @param y1 起点y点
|
* @param x2 终点x点
|
* @param y2 终点y点
|
* @return
|
*/
|
public static double calculateProportion(double x0, double y0, double x1, double y1, double x2, double y2) {
|
// 向量 AB
|
double ABx = x2 - x1;
|
double ABy = y2 - y1;
|
|
// 向量 AP
|
double APx = x0 - x1;
|
double APy = y0 - y1;
|
|
// 向量积 AB × AP
|
double crossProduct = ABx * APy - APx * ABy;
|
|
if (crossProduct == 0) {
|
// 点在直线上,判断是否在线段上
|
double dotProduct = APx * ABx + APy * ABy;
|
double squaredLengthAB = ABx * ABx + ABy * ABy;
|
double t = dotProduct / squaredLengthAB;
|
|
if (t >= 0 && t <= 1) {
|
return t; // 垂足到mstPointStart的距离占整条线段长度的比例
|
} else if (t < 0) {
|
//点在mstPointStart的延长线上
|
return 0;
|
} else {
|
//点在mstPointEnd的延长线上
|
return 1;
|
}
|
} else {
|
return -1; // 点不在直线上
|
}
|
}
|
|
|
|
}
|