From 506168ae540c5aabb34f97a46a6248cd73b77f8b Mon Sep 17 00:00:00 2001 From: whycxzp <glperry@163.com> Date: 星期三, 02 四月 2025 00:46:03 +0800 Subject: [PATCH] 更新计算 --- src/main/java/com/whyc/util/PointUtil.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/whyc/util/PointUtil.java b/src/main/java/com/whyc/util/PointUtil.java index a8aff9b..0289312 100644 --- a/src/main/java/com/whyc/util/PointUtil.java +++ b/src/main/java/com/whyc/util/PointUtil.java @@ -4,7 +4,7 @@ public class PointUtil { public static void main(String[] args) { - double a1 = 1, b1 = 2; + /*double a1 = 1, b1 = 2; double a2 = 3, b2 = 4; double x0 = 9, y0 = 9; Point p1 = new Point(1, 2); @@ -18,7 +18,17 @@ 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."); + 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("鍨傝冻鍒癿stPointStart鐨勮窛绂诲崰鏁存潯绾挎闀垮害鐨勬瘮渚嬫槸: " + proportion); + } else { + System.out.println("鍨傝冻涓嶅湪绾挎涓�"); + } } public static double[] findIntersection(double a1, double b1, double a2, double b2, double x0, double y0) { @@ -77,5 +87,50 @@ 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; // 鍨傝冻鍒癿stPointStart鐨勮窛绂诲崰鏁存潯绾挎闀垮害鐨勬瘮渚� + } else if (t < 0) { + //鐐瑰湪mstPointStart鐨勫欢闀跨嚎涓� + return 0; + } else { + //鐐瑰湪mstPointEnd鐨勫欢闀跨嚎涓� + return 1; + } + } else { + return -1; // 鐐逛笉鍦ㄧ洿绾夸笂 + } + } + + + } -- Gitblit v1.9.1