コード例 #1
0
        public static bool intersection(Line lineA, Line lineB)
        {
            /* Returns true if segments collide
             * If they have in common a segment edge returns false
             * Algorithm obtained from:
             * http://stackoverflow.com/questions/3838329/how-can-i-check-if-two-segments-intersect
             * Thanks OMG_peanuts !
             * */
            double  dif;
            double  A1, A2;
            double  b1, b2;
            decimal X;

            if (Math.Max(lineA.nodes[0].x, lineA.nodes[1].x) < Math.Min(lineB.nodes[0].x, lineB.nodes[1].x))
            {
                return(false); //Not a chance of intersection
            }

            dif = lineA.nodes[0].x - lineA.nodes[1].x;
            if (dif != 0)   //Avoids dividing by 0
            {
                A1 = (lineA.nodes[0].y - lineA.nodes[1].y) / dif;
            }
            else
            {
                //Segment is vertical
                A1 = 9999999;
            }

            dif = lineB.nodes[0].x - lineB.nodes[1].x;
            if (dif != 0)   //Avoids dividing by 0
            {
                A2 = (lineB.nodes[0].y - lineB.nodes[1].y) / dif;
            }
            else
            {
                //Segment is vertical
                A2 = 9999999;
            }

            if (A1 == A2)
            {
                return(false); //Parallel
            }
            else if (A1 == 9999999)
            {
                return(verticalIntersection(lineA, lineB));
            }
            else if (A2 == 9999999)
            {
                return(verticalIntersection(lineB, lineA));
            }

            b1 = lineA.nodes[0].y - (A1 * lineA.nodes[0].x);
            b2 = lineB.nodes[0].y - (A2 * lineB.nodes[0].x);
            X  = Math.Round(System.Convert.ToDecimal((b2 - b1) / (A1 - A2)), 4);
            if ((X <= System.Convert.ToDecimal(Math.Max(Math.Min(lineA.nodes[0].x, lineA.nodes[1].x), Math.Min(lineB.nodes[0].x, lineB.nodes[1].x)))) ||
                (X >= System.Convert.ToDecimal(Math.Min(Math.Max(lineA.nodes[0].x, lineA.nodes[1].x), Math.Max(lineB.nodes[0].x, lineB.nodes[1].x)))))
            {
                return(false); //Out of bound
            }
            else
            {
                return(true);
            }
        }
コード例 #2
0
        public static bool tangentToHull(Line line_treated, Node node, decimal cos1, decimal cos2, List <Line> concave_hull)
        {
            /* A new middlepoint could (rarely) make a segment that's tangent to the hull.
             * This method detects these situations
             * I suggest turning this method of if you are not using square grids or if you have a high dot density
             * */
            bool       isTangent = false;
            decimal    current_cos1;
            decimal    current_cos2;
            double     edge_length;
            List <int> nodes_searched = new List <int>();
            Line       line;
            Node       node_in_hull;
            int        count_line = 0;
            int        count_node = 0;

            edge_length = Line.getLength(node, line_treated.nodes[0]) + Line.getLength(node, line_treated.nodes[1]);


            while (!isTangent && count_line < concave_hull.Count)
            {
                line = concave_hull[count_line];
                while (!isTangent && count_node < 2)
                {
                    node_in_hull = line.nodes[count_node];
                    if (!nodes_searched.Contains(node_in_hull.id))
                    {
                        if (node_in_hull.id != line_treated.nodes[0].id && node_in_hull.id != line_treated.nodes[1].id)
                        {
                            current_cos1 = getCos(node_in_hull, line_treated.nodes[0], line_treated.nodes[1]);
                            current_cos2 = getCos(node_in_hull, line_treated.nodes[1], line_treated.nodes[0]);
                            if (current_cos1 == cos1 || current_cos2 == cos2)
                            {
                                isTangent = (Line.getLength(node_in_hull, line_treated.nodes[0]) + Line.getLength(node_in_hull, line_treated.nodes[1]) < edge_length);
                            }
                        }
                    }
                    nodes_searched.Add(node_in_hull.id);
                    count_node++;
                }
                count_node = 0;
                count_line++;
            }
            return(isTangent);
        }