Exemple #1
0
        public bool isConvexVertex(Point v1)
        {
            var v0 = prev(v1).vec;
            var v2 = next(v1).vec;

            return(GeomUtils.cross(v1.x - v0.x, v1.y - v0.y, v2.x - v1.x, v2.y - v1.y) > 0);
        }
Exemple #2
0
        public bool isConvexVertexi(int i)
        {
            var len = this.Count;
            var v0  = this[(i + len - 1) % len].vec;
            var v1  = this[i].vec;
            var v2  = this[(i + 1) % len].vec;

            return(GeomUtils.cross(v1.x - v0.x, v1.y - v0.y, v2.x - v1.x, v2.y - v1.y) > 0);
        }
Exemple #3
0
        public Point _centroid()
        {
            var x = 0.0;
            var y = 0.0;
            var a = 0.0;

            forEdge((Point v0, Point v1) =>
            {
                var f = GeomUtils.cross(v0.x, v0.y, v1.x, v1.y);
                a    += f;
                x    += (v0.x + v1.x) * f;
                y    += (v0.y + v1.y) * f;
            });
            var s6 = 1 / (3 * a);

            return(new Point((float)(s6 * x), (float)(s6 * y)));
        }
Exemple #4
0
        public List <Polygon> cut(Point p1, Point p2, float gap = 0)
        {
            var x1  = p1.x;
            var y1  = p1.y;
            var dx1 = p2.x - x1;
            var dy1 = p2.y - y1;

            var len    = this.Count;
            var edge1  = 0;
            var ratio1 = 0.0f;
            var edge2  = 0;
            var ratio2 = 0.0f;
            var count  = 0;

            for (var i = 0; i < len; i++)
            {
                var v0 = this[i];
                var v1 = this[(i + 1) % len];

                var x2  = v0.x;
                var y2  = v0.y;
                var dx2 = v1.x - x2;
                var dy2 = v1.y - y2;

                var t = GeomUtils.intersectLines(x1, y1, dx1, dy1, x2, y2, dx2, dy2);
                if (t != null && t?.y >= 0 && t?.y <= 1)
                {
                    switch (count)
                    {
                    case 0:
                        edge1  = i;
                        ratio1 = t.x;
                        break;

                    case 1:
                        edge2  = i;
                        ratio2 = t.x;
                        break;
                    }
                    count++;
                }
            }

            if (count == 2)
            {
                var point1 = new Point(p1.vec + ((p2.vec - p1.vec) * (ratio1)));
                var point2 = new Point(p1.vec + ((p2.vec - p1.vec) * (ratio2)));

                var half1 = new Polygon(this.Skip(edge1 + 1).Take((edge2 + 1) - (edge1 + 1)).ToList());
                half1.Insert(0, point1);
                half1.Add(point2);

                var half2 = new Polygon(this.Skip(edge2 + 1).Concat(this.Take(edge1 + 1)).ToList());
                half2.Insert(0, point2);
                half2.Add(point1);

                if (gap > 0)
                {
                    half1 = half1.peel(point2, gap / 2);
                    half2 = half2.peel(point1, gap / 2);
                }

                var v = vectori(edge1);
                return(GeomUtils.cross(dx1, dy1, v.x, v.y) > 0 ? new List <Polygon> {
                    half1, half2
                } : new List <Polygon> {
                    half2, half1
                });
            }
            else
            {
                return new List <Polygon> {
                           new Polygon(this)
                }
            };
        }