static mPoint intersection(mPoint a, mPoint b, mPoint c, mPoint d) { mPoint p = a; double t = ((a.x - c.x) * (c.y - d.y) - (a.y - c.y) * (c.x - d.x)) / ((a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x)); p.x += (b.x - a.x) * t; p.y += (b.y - a.y) * t; return(p); }
/// <summary> /// ConvexPolygonIntersectArea /// return the intersection area, /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="intersectPolygon">full close loop polygon</param> /// <returns></returns> public static double CPIA(List <mPoint> a, List <mPoint> b, out List <mPoint> intersectPolygon) { int na = a.Count; int nb = b.Count; int tn, sflag, eflag; a.Add(a[0]); b.Add(b[0]); intersectPolygon = new List <mPoint>(); List <mPoint> tmp = new List <mPoint>(); foreach (mPoint pa in a) { intersectPolygon.Add(pa); } for (int i = 0; i < na && nb > 2; i++) { sflag = SignofDouble(cross(a[i + 1], intersectPolygon[0], a[i])); for (int j = tn = 0; j < nb; j++, sflag = eflag) { if (sflag >= 0) { tmp.Add(intersectPolygon[j]); tn++; } eflag = SignofDouble(cross(a[i + 1], intersectPolygon[j + 1], a[i])); if ((sflag ^ eflag) == -2) { mPoint intersets = intersection(a[i], a[i + 1], intersectPolygon[j], intersectPolygon[j + 1]); ///find the intersection point tmp.Add(intersets); tn++; } } //memcpy(p, tmp, sizeof(Point) * tn); intersectPolygon.Clear(); intersectPolygon = tmp; //for(int k = 0; k < tn; k++) //{ // p.Add(tmp[k]); //} nb = tn; intersectPolygon.Add(intersectPolygon[0]); } if (nb < 3) { return(0.0); } return(PolygonArea(intersectPolygon, nb)); }
/// <summary> /// /// SimplePolygonIntersectArea /// Decompose to triangles and calculate the intersection area. /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="intersectPolygon">full close loop polygon</param> /// <returns></returns> public static double SPIA(List <mPoint> a, List <mPoint> b, out List <List <mPoint> > intersectPolygon)///SimplePolygonIntersectArea { intersectPolygon = new List <List <mPoint> >(); int na = a.Count; int nb = b.Count; int i, j; mPoint[] t1 = new mPoint[3]; mPoint [] t2 = new mPoint[3]; double res = 0, num1, num2; a.Add(t1[0] = a[0]); b.Add(t2[0] = b[0]); for (i = 2; i < na; i++) { t1[1] = a[i - 1]; t1[2] = a[i]; num1 = SignofDouble(cross(t1[1], t1[2], t1[0])); // area with directions. positive when sweeping towards the end, negative when backwards if (num1 < 0) { GenericMethods.Swap <mPoint>(ref t1[1], ref t1[2]); } for (j = 2; j < nb; j++) { t2[1] = b[j - 1]; t2[2] = b[j]; num2 = SignofDouble(cross(t2[1], t2[2], t2[0])); if (num2 < 0) { GenericMethods.Swap <mPoint>(ref t2[1], ref t2[2]); } List <mPoint> convexIntersectPolygon = new List <mPoint>(); res += CPIA(t1.ToList(), t2.ToList(), out convexIntersectPolygon) * num1 * num2; intersectPolygon.Add(convexIntersectPolygon); } } // merge all mergable convex polygons intersectPolygon = mergeAllConvexPolygons(intersectPolygon); return(res);//res is the intersection area }
static double cross(mPoint a, mPoint b, mPoint c) { return((a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y)); }