示例#1
0
        private void modifyPolygon(ref Polygon polygon)
        {
            Vector2 first = polygon.points[0];
            Vector2 last = polygon.points[polygon.points.Count - 1];
            int firstIndex = getEdgeIndex(first);
            int lastIndex = getEdgeIndex(last);

            if (firstIndex == lastIndex)
                return;

            if (firstIndex % 2 == 1 && lastIndex % 2 == 1)
            {
                Vector2 point = new Vector2();
                point.x = getBoundary(0);
                point.y = getBoundary(3);
                if (VectorXMultiply(point, first, polygon.points[2])<0)
                {
                    polygon.addVertex(new Vector2(getBoundary(0), getBoundary(1)));
                    polygon.addVertex(new Vector2(getBoundary(0), getBoundary(3)));
                }
                else
                {
                    polygon.addVertex(new Vector2(getBoundary(2), getBoundary(3)));
                    polygon.addVertex(new Vector2(getBoundary(2), getBoundary(1)));
                }
            }
            if (firstIndex % 2 == 0 && lastIndex % 2 == 0)
            {
                Vector2 point = new Vector2();
                point.x = getBoundary(2);
                point.y = getBoundary(3);
                if (VectorXMultiply(point, first, polygon.points[2])<0)
                {
                    polygon.addVertex(new Vector2(getBoundary(0), getBoundary(3)));
                    polygon.addVertex(new Vector2(getBoundary(2), getBoundary(3)));

                }
                else
                {
                    polygon.addVertex(new Vector2(getBoundary(2), getBoundary(1)));
                    polygon.addVertex(new Vector2(getBoundary(0), getBoundary(1)));
                }
            }

            if (firstIndex % 2 == 0 && lastIndex % 2 == 1)
            {
                Vector2 point = new Vector2();
                point.x = getBoundary(firstIndex);
                point.y = getBoundary(lastIndex);
                polygon.addVertex(point);
            }

            if (firstIndex % 2 == 1 && lastIndex % 2 == 0)
            {
                Vector2 point = new Vector2();
                point.x = getBoundary(lastIndex);
                point.y = getBoundary(firstIndex);
                polygon.addVertex(point);
            }
        }
示例#2
0
        //构建并显示Voronoi图
        public void CreateVoronoi()
        {
            //可以用来起始搜索的

            for (int i = 0; i < DS.TinEdgeNum; i++)
            {
                if (!DS.TinEdges[i].NotHullEdge) //△边为凸壳边
                {
                    Vector2 endPnt = getEndPntVorEdge(i);
                    //Debug.Log(endPnt.ToString());
                    if (!endPnt.Equals(Vector2.zero))
                    {
                        //起始
                        int index = DS.TinEdges[i].AdjTriangle1ID;

                        DS.connectMap[index, DS.TriangleNum] = true;
                        DS.connectMap[DS.TriangleNum, index] = true;
                        startIndexs.Add(new Vector2(DS.TriangleNum, index));
                        Barycenter barycenter = new Barycenter();
                        barycenter.X = endPnt.x;
                        barycenter.Y = endPnt.y;

                        DS.Barycenters[DS.TriangleNum] = barycenter;
                        DS.TriangleNum++;
                    }

                }
            }

            //for (int i = 0; i < DS.VerticesNum; i++)
            //{
            //    GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //    go.transform.localScale = new Vector3(5, 5, 5);
            //    go.transform.localPosition = new Vector3(DS.Vertex[i].x * 10, 0, DS.Vertex[i].y * 10);
            //    go.GetComponent<MeshRenderer>().material.color = Color.white;
            //    go.transform.SetParent(scene.transform);

            //}

            //for (int i = 0; i < DS.TriangleNum; i++)
            //{
            //    GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //    go.transform.localScale = new Vector3(5, 5, 5);
            //    go.transform.localPosition = new Vector3(DS.Barycenters[i].X*10, 0, DS.Barycenters[i].Y*10);
            //    go.GetComponent<MeshRenderer>().material.color = Color.black;
            //    go.transform.SetParent(scene.transform);
            //}

            for (int i = 0; i < startIndexs.Count; i++)
            {
                Polygon polygon = new Polygon();
                int lastIndex = (int)startIndexs[i].x;
                int currIndex = (int)startIndexs[i].y;

                Vector2 lastPoint = DS.Barycenters[lastIndex].point;
                Vector2 currPoint = DS.Barycenters[currIndex].point;
                polygon.addVertex(lastPoint);
                polygon.addVertex(currPoint);
                polygons.Add(polygon);
                DS.connectMap[lastIndex, currIndex] = false;
                searchMap(lastIndex, lastIndex, currIndex);
            }

            for (int i = 0; i < DS.TriangleNum; i++)
            {
                for (int j = 0; j < DS.TriangleNum; j++)
                {
                    if (DS.connectMap[i, j])
                    {
                        Vector2 lastPoint = DS.Barycenters[i].point;
                        Vector2 currPoint = DS.Barycenters[j].point;
                        if (PointInBox(lastPoint) && PointInBox(currPoint))
                        {
                            Polygon polygon = new Polygon();
                            polygon.addVertex(lastPoint);
                            polygon.addVertex(currPoint);
                            polygons.Add(polygon);
                            DS.connectMap[i, j] = false;
                            searchMap(i, i, j);
                        }

                    }
                }
            }
            for (int i = 0; i < polygons.Count; i++)
            {
                Polygon polygon = polygons[i];
                modifyPolygon(ref polygon);
            }
        }