Example #1
0
    private void Update()
    {
        ConvexPolygon p1 = polygon1.Polygon;
        ConvexPolygon p2 = polygon2.Polygon;

        if (p1 == null || p2 == null)
        {
            return;
        }
        ConvexPolygon polygon = IntersectionOperation.Execute(p1, p2, lineFactory);

        //DrawLine
        if (polygon == null)
        {
            return;
        }
        List <Vector3> vertices = polygon.GetVertices3Copy();

        vertices.Add(vertices[0]);                      //末尾に先頭を追加
        if (line != null)
        {
            lineFactory.DeleteLine(line);
        }
        //line = lineFactory.CreateLine(vertices, Color.green);

        //DrawEdgeLine
        //DrawEdgeLine(p1, leftEdge);
    }
        /// <summary>
        /// 重み付きボロノイ図の生成
        /// </summary>
        public List <ConvexPolygon> Execute(ConvexPolygon area, List <WaitedVoronoiSite> sites)
        {
            List <ConvexPolygon> result = new List <ConvexPolygon>();

            foreach (var s1 in sites)
            {
                ConvexPolygon region = null;                    //途中計算結果格納用の領域
                foreach (var s2 in sites)
                {
                    if (s1 == s2)
                    {
                        continue;
                    }
                    //s1とs2の垂直線を重み付きで求める
                    Line line = Line.PerpendicularWaitLine(s1.Point, s1.Wait, s2.Point, s2.Wait);
                    //垂直二等分線による半平面のうち,s1を含む方を求める
                    ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1.Point);
                    if (region == null)
                    {
                        //初回計算時
                        region = IntersectionOperation.Execute(area, halfPlane);
                    }
                    else
                    {
                        //二回目以降
                        region = IntersectionOperation.Execute(region, halfPlane);
                    }
                }
                //最終的な計算結果をボロノイ領域とする
                result.Add(region);
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// ボロノイ図の生成
        /// </summary>
        public List <ConvexPolygon> Execute(ConvexPolygon area, List <Vector2> sites)
        {
            List <ConvexPolygon> result = new List <ConvexPolygon>();

            foreach (Vector2 s1 in sites)
            {
                ConvexPolygon region = null;                    //途中計算結果格納用の領域
                foreach (Vector2 s2 in sites)
                {
                    if (s1 == s2)
                    {
                        continue;
                    }
                    //s1とs2の垂直二等分線を求める
                    Line line = Line.PerpendicularBisector(s1, s2);
                    //垂直二等分線による半平面のうち,s1を含む方を求める
                    ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);
                    if (region == null)
                    {
                        //初回計算時
                        region = IntersectionOperation.Execute(area, halfPlane);
                    }
                    else
                    {
                        //二回目以降
                        region = IntersectionOperation.Execute(region, halfPlane);
                    }
                }
                //最終的な計算結果をボロノイ領域とする
                result.Add(region);
            }
            return(result);
        }
Example #4
0
    /// <summary>
    /// ボロノイ図生成コルーチン
    /// </summary>
    private IEnumerator Voronoi()
    {
        PseudoHalfPlaneGenerator halfPlaneGenerator = new PseudoHalfPlaneGenerator(100f);

        while (true)
        {
            List <Vector2>       sites   = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));
            List <ConvexPolygon> results = new List <ConvexPolygon>();
            List <ChainLine>     lines   = new List <ChainLine>();

            for (int i = 0; i < sites.Count; ++i)
            {
                Vector3       s1     = sites[i];
                ConvexPolygon region = null;                    //途中計算結果格納用の領域
                for (int j = 0; j < sites.Count; ++j)
                {
                    Vector3 s2 = sites[j];
                    if (i == j)
                    {
                        continue;
                    }

                    //s1とs2の垂直二等分線を求める
                    Line line = Line.PerpendicularBisector(s1, s2);
                    //垂直二等分線による半平面のうち,s1を含む方を求める
                    ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);

                    if (region == null)
                    {
                        //初回計算時
                        region = IntersectionOperation.Execute(areaPolygon, halfPlane);
                    }
                    else
                    {
                        //二回目以降
                        region = IntersectionOperation.Execute(region, halfPlane);
                    }
                    Debug.Log(region + " : " + halfPlane);

                    //halfPlaneを可視化
                    List <Vector3> vertices = halfPlane.GetVertices3Copy();
                    vertices.Add(vertices[0]);                          //末尾を追加
                    ChainLine subl = lineFactory.CreateLine(vertices, subColor);

                    //regionを可視化
                    vertices = region.GetVertices3Copy();
                    vertices.Add(vertices[0]);                          //末尾を追加
                    ChainLine l = lineFactory.CreateLine(vertices, lineColor.Evaluate((float)j / sites.Count));
                    yield return(StartCoroutine(Wait()));

                    lineFactory.DeleteLine(subl);
                    lineFactory.DeleteLine(l);
                }
            }
            Debug.Log("Complete");

            yield return(StartCoroutine(Wait()));
        }
    }
Example #5
0
    /// <summary>
    /// ボロノイ図生成コルーチン
    /// </summary>
    private IEnumerator Voronoi()
    {
        while (true)
        {
            List <Vector2>       sites   = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));
            List <ConvexPolygon> results = new List <ConvexPolygon>();
            List <ChainLine>     lines   = new List <ChainLine>();

            foreach (Vector2 s1 in sites)
            {
                ConvexPolygon region = null;                    //途中計算結果格納用の領域
                foreach (Vector2 s2 in sites)
                {
                    if (s1 == s2)
                    {
                        continue;
                    }
                    //ウェイト
                    yield return(StartCoroutine(WaitClick()));

                    //s1とs2の垂直二等分線を求める
                    Line line = Line.PerpendicularBisector(s1, s2);
                    //垂直二等分線による半平面のうち,s1を含む方を求める
                    ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);

                    //線を描画
                    lines.Add(lineFactory.CreateLine(halfPlane.GetVertices3Copy()));

                    if (region == null)
                    {
                        //初回計算時
                        region = IntersectionOperation.Execute(areaPolygon, halfPlane);
                    }
                    else
                    {
                        //二回目以降
                        region = IntersectionOperation.Execute(region, halfPlane);
                    }
                }
                results.Add(region);
            }

            for (int i = 0; i < results.Count; ++i)
            {
                lines.Add(lineFactory.CreateLine(results[i].GetVertices3Copy(), Color.red));
            }
            Debug.Log("Complete");

            yield return(StartCoroutine(WaitClick()));

            for (int i = 0; i < lines.Count; ++i)
            {
                lineFactory.DeleteLine(lines[i]);
            }
            lines.Clear();
        }
    }
Example #6
0
    private void Update()
    {
        List <Vector2>       sites   = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));
        List <ConvexPolygon> results = new List <ConvexPolygon>();

        //線の削除
        if (lines.Count > 0)
        {
            for (int i = 0; i < lines.Count; ++i)
            {
                lineFactory.DeleteLine(lines[i]);
            }
            lines.Clear();
        }

        foreach (Vector2 s1 in sites)
        {
            ConvexPolygon region = null;                //途中計算結果格納用の領域
            foreach (Vector2 s2 in sites)
            {
                if (s1 == s2)
                {
                    continue;
                }
                //s1とs2の垂直二等分線を求める
                Line line = Line.PerpendicularBisector(s1, s2);
                //垂直二等分線による半平面のうち,s1を含む方を求める
                ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);
                if (region == null)
                {
                    //初回計算時
                    region = IntersectionOperation.Execute(areaPolygon, halfPlane);
                }
                else
                {
                    //二回目以降
                    region = IntersectionOperation.Execute(region, halfPlane);
                }
            }
            if (region != null)
            {
                results.Add(region);
            }
            else
            {
                Debug.Log("Region is null");
            }
        }

        for (int i = 0; i < results.Count; ++i)
        {
            results[i].Scale(sites[i], 0.9f);
            List <Vector3> vertices = results[i].GetVertices3Copy();
            vertices.Add(vertices[0]);
            lines.Add(lineFactory.CreateLine(vertices, Color.green));
        }
    }
Example #7
0
    private void DrawEdgeLine(ConvexPolygon p, ChainLine line)
    {
        IntersectionOperation.Edge left     = IntersectionOperation.CreateEdgeChain(p, true);
        List <Vector3>             vertices = new List <Vector3>();

        vertices.Add(left.startPoint);
        foreach (var e in left)
        {
            vertices.Add(e.endPoint);
        }
        if (line != null)
        {
            lineFactory.DeleteLine(line);
        }
        line = lineFactory.CreateLine(vertices, Color.red);
    }