求两条线段的交点

       /// <summary>
        /// 求两条线段的交点
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="x3"></param>
        /// <param name="y3"></param>
        /// <param name="x4"></param>
        /// <param name="y4"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public bool GetSegInter(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, ref Point2d point)
        {
            double A1 = y2 - y1;
            double B1 = x1 - x2;
            double C1 = -A1 * x1 - B1 * y1;
            double A2 = y4 - y3;
            double B2 = x3 - x4;
            double C2 = -A2 * x3 - B2 * y3;
            double dlt = A1 * B2 - A2 * B1;
            double dltx = C1 * B2 - C2 * B1;
            double dlty = A1 * C2 - A2 * C1;
            if (Math.Abs(dlt) < 0.00000001)
            {
                return false;
            }
            else
            {
                point = new Point2d(-1.0 * (dltx / dlt), -1.0 * (dlty / dlt));
                bool bResInPoly = IsPointInLine(new Point2d(x1, y1), new Point2d(x2, y2), point);
                bool bResInTile = IsPointInLine(new Point2d(x3, y3), new Point2d(x4, y4), point);

                return bResInPoly && bResInTile;
            }
        }

        public bool IsPointInLine(Point2d a, Point2d b, Point2d c, double bAllowGap = 0.01)
        {
            double lac = Math.Sqrt((a.X - c.X) * (a.X - c.X) + (a.Y - c.Y) * (a.Y - c.Y));
            double lbc = Math.Sqrt((b.X - c.X) * (b.X - c.X) + (b.Y - c.Y) * (b.Y - c.Y));
            double lab = Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
            return (lac + lbc < lab + bAllowGap) ? true : false;
        }

 

posted @ 2015-03-16 15:33  秋月的私语  阅读(261)  评论(0编辑  收藏  举报