From debd3f045e174045c0229b2ce2e3982e8b853294 Mon Sep 17 00:00:00 2001
From: fctom1215 <fctom1215@outlook.com>
Date: 星期日, 26 七月 2020 20:42:59 +0800
Subject: [PATCH] 坐标

---
 app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java |  155 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 147 insertions(+), 8 deletions(-)

diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java b/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java
index d878241..a1be614 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java
@@ -231,6 +231,27 @@
         }
     }
 
+    class Line {
+        double X1;
+        double Y1;
+        double X2;
+        double Y2;
+
+        public Line(double x1, double y1, double x2, double y2) {
+            this.X1 = x1;
+            this.Y1 = y1;
+            this.X2 = x2;
+            this.Y2 = y2;
+        }
+    }
+
+    private enum Relation {
+        GM_None,
+        GM_Tangent,
+        GM_Intersection,
+        GM_Containment
+    };
+
     public PointF rotatePoint(PointF oldPoint, PointF centre, double degree) {
         PointF newPoint = new PointF(0, 0);
 
@@ -238,6 +259,101 @@
         newPoint.setY ( (oldPoint.getX()-centre.getX())*Math.sin(Math.toRadians(degree)) + (oldPoint.getY()-centre.getY())*Math.cos(Math.toRadians(degree)) + centre.getY() );
 
         return newPoint;
+    }
+
+    boolean isEqual(double a, double b)
+    {
+        return (Math.abs(a - b) <= 1e-6);
+    }
+
+    Relation IntersectionOf(Line line1, Line line2)
+    {
+        if ((isEqual(line1.X1, line1.X2) && isEqual(line1.Y1, line1.Y2)) || (isEqual(line2.X1, line2.X2) && isEqual(line2.Y1, line2.Y2)))
+            return Relation.GM_None;
+
+        if ((isEqual(line1.X1, line2.X1) && isEqual(line1.Y1, line2.Y1)) || (isEqual(line1.X2, line2.X1) && isEqual(line1.Y2, line2.Y1)))
+            return Relation.GM_Intersection;
+
+        if ((isEqual(line1.X1, line2.X2) && isEqual(line1.Y1, line2.Y2)) || (isEqual(line1.X2, line2.X2) && isEqual(line1.Y2, line2.Y2)))
+            return Relation.GM_Intersection;
+
+        line1.X2 -= line1.X1; line1.Y2 -= line1.Y1;
+        line2.X1 -= line1.X1; line2.Y1 -= line1.Y1;
+        line2.X2 -= line1.X1; line2.Y2 -= line1.Y1;
+
+        double distAB = Math.sqrt(line1.X2 * line1.X2 + line1.Y2 * line1.Y2);
+
+        double theCos = line1.X2 / distAB;
+        double theSin = line1.Y2 / distAB;
+        double newX = line2.X1 * theCos + line2.Y1 * theSin;
+
+        line2.Y1 = line2.Y1 * theCos - line2.X1 * theSin;
+        line2.X1 = newX;
+        newX = line2.X2 * theCos + line2.Y2 * theSin;
+        line2.Y2 = line2.Y2 * theCos - line2.X2 * theSin;
+        line2.X2 = newX;
+
+        if ((line2.Y1 < 0 && line2.Y2 < 0) || (line2.Y1 >= 0 && line2.Y2 >= 0)) {
+            return Relation.GM_None;
+        }
+
+        double posAB = line2.X2 + (line2.X1 - line2.X2) * line2.Y2 / (line2.Y2 - line2.Y1);
+
+        if (posAB < 0 || posAB > distAB) {
+            return Relation.GM_None;
+        }
+
+        return Relation.GM_Intersection;
+    }
+
+    Relation IntersectionOf(Line line, double screen_w, double screen_h)
+    {
+        boolean tangent = false;
+
+        Line line1 = new Line(0, 0, screen_w, 0);
+        Line line2 = new Line(screen_w, 0, screen_w, screen_h);
+        Line line3 = new Line(screen_w, screen_h, 0, screen_h);
+        Line line4 = new Line(0, screen_h, 0, 0);
+
+        Relation relation = IntersectionOf(line, line1);
+        if (relation == Relation.GM_Intersection) {
+            return relation;
+        }
+        if (relation == Relation.GM_Tangent) {
+            tangent = true;
+        }
+
+        relation = IntersectionOf(line, line2);
+        if (relation == Relation.GM_Intersection) {
+            return relation;
+        }
+        if (relation == Relation.GM_Tangent) {
+            tangent = true;
+        }
+
+        relation = IntersectionOf(line, line3);
+        if (relation == Relation.GM_Intersection) {
+            return relation;
+        }
+        if (relation == Relation.GM_Tangent) {
+            tangent = true;
+        }
+
+        relation = IntersectionOf(line, line4);
+        if (relation == Relation.GM_Intersection) {
+            return relation;
+        }
+        if (relation == Relation.GM_Tangent) {
+            tangent = true;
+        }
+
+        if (tangent)
+            return Relation.GM_Tangent;
+
+        if (line.X1 > 0 && line.X1 < screen_w && line.Y1 > 0 && line.Y1 < screen_h)
+            return Relation.GM_Containment;
+
+        return Relation.GM_None;
     }
 
     static int angg = 0;
@@ -811,16 +927,24 @@
 
                         int draw_status = 0;
 
-                        for (int z = 0; z < line.size(); z++) {
+                        for (int z = 0; z < line.size() - 1; z++) {
                             float scr_x = (float) (base_x + (map[line.get(z)][0] - min_x) * scale_x);
                             float scr_y = (float) (base_y + (map[line.get(z)][1] - min_y) * scale_y);
 
-                            if (scr_x >= 0 && scr_x <= bmp.getWidth() && scr_y >= 0 && scr_y <= bmp.getHeight()) {
+                            float scr_x2 = (float) (base_x + (map[line.get(z+1)][0] - min_x) * scale_x);
+                            float scr_y2 = (float) (base_y + (map[line.get(z+1)][1] - min_y) * scale_y);
+
+                            Line linex = new Line(scr_x, scr_y, scr_x2, scr_y2);
+
+                            Relation relation = IntersectionOf(linex, bmp.getWidth(), bmp.getHeight());
+
+                            if (relation == Relation.GM_Intersection || relation == Relation.GM_Containment) {
                                 if (draw_status == 0) {
                                     path.moveTo(scr_x, scr_y);
+                                    path.lineTo(scr_x2, scr_y2);
                                     draw_status = 1;
                                 } else if (draw_status == 1) {
-                                    path.lineTo(scr_x, scr_y);
+                                    path.lineTo(scr_x2, scr_y2);
                                 }
                             } else if (draw_status == 1) {
                                 canvas2.drawPath(path, paint);
@@ -869,16 +993,24 @@
 
                         int draw_status = 0;
 
-                        for (int z = 0; z < line.size(); z++) {
+                        for (int z = 0; z < line.size()-1; z++) {
                             float scr_x = (float) (base_x + (map[line.get(z)][0] - min_x) * scale_x);
                             float scr_y = (float) (base_y + (map[line.get(z)][1] - min_y) * scale_y);
 
-                            if (scr_x >= 0 && scr_x <= bmp.getWidth() && scr_y >= 0 && scr_y <= bmp.getHeight()) {
+                            float scr_x2 = (float) (base_x + (map[line.get(z+1)][0] - min_x) * scale_x);
+                            float scr_y2 = (float) (base_y + (map[line.get(z+1)][1] - min_y) * scale_y);
+
+                            Line linex = new Line(scr_x, scr_y, scr_x2, scr_y2);
+
+                            Relation relation = IntersectionOf(linex, bmp.getWidth(), bmp.getHeight());
+
+                            if (relation == Relation.GM_Intersection || relation == Relation.GM_Containment) {
                                 if (draw_status == 0) {
                                     edgePath.moveTo(scr_x, scr_y);
+                                    path.lineTo(scr_x2, scr_y2);
                                     draw_status = 1;
                                 } else if (draw_status == 1) {
-                                    edgePath.lineTo(scr_x, scr_y);
+                                    edgePath.lineTo(scr_x2, scr_y2);
                                 }
                             } else if (draw_status == 1) {
                                 canvas2.drawPath(edgePath, paint);
@@ -936,16 +1068,23 @@
 
                                 int draw_status = 0;
 
-                                for (int za = 0; za < points.size(); za++) {
+                                for (int za = 0; za < points.size()-1; za++) {
                                     float scr_x = (float) (base_x + (map[points.get(za)][0] - min_x) * scale_x);
                                     float scr_y = (float) (base_y + (map[points.get(za)][1] - min_y) * scale_y);
 
+                                    float scr_x2 = (float) (base_x + (map[points.get(za+1)][0] - min_x) * scale_x);
+                                    float scr_y2 = (float) (base_y + (map[points.get(za+1)][1] - min_y) * scale_y);
+
+                                    Line linex = new Line(scr_x, scr_y, scr_x2, scr_y2);
+
+                                    Relation relation = IntersectionOf(linex, bmp.getWidth(), bmp.getHeight());
                                     if (scr_x >= 0 && scr_x <= bmp.getWidth() && scr_y >= 0 && scr_y <= bmp.getHeight()) {
                                         if (draw_status == 0) {
                                             sepPath.moveTo(scr_x, scr_y);
+                                            sepPath.lineTo(scr_x2, scr_y2);
                                             draw_status = 1;
                                         } else if (draw_status == 1) {
-                                            sepPath.lineTo(scr_x, scr_y);
+                                            sepPath.lineTo(scr_x2, scr_y2);
                                         }
                                     } else if (draw_status == 1) {
                                         canvas2.drawPath(sepPath, paint);

--
Gitblit v1.8.0