// Use this for initialization
    void Start()
    {
        DT = new DelaunayTriangles(new Rect(0,0,128,64));
        DT.VertexList.Add(new Vertex(3,6));
        DT.VertexList.Add(new Vertex(34, 21));
        DT.VertexList.Add(new Vertex(10, 15));
        DT.VertexList.Add(new Vertex(98, 62));
        DT.VertexList.Add(new Vertex(73, 21));
        for (int i = 0; i < 20; i++) {
            float x = Random.Range(0, 128);
            float y = Random.Range(0, 64);
            DT.VertexList.Add(new Vertex(x,y));
        }

        List<Node> nodes = new List<Node>();
        foreach (Vertex v in DT.VertexList) {
            //sp.transform.position = v.Position;
            GameObject obj = GameObject.Instantiate(sp);
            obj.transform.position = v.Position;
            obj.SetActive(true);
            nodes.Add(new Node(v.Position));
        }
        DT.DelaunayTrianglation();
        //DT.DelaunayTrianglation();
        //Debug.Log(DT.RootTriangle.ToString());
        List<Edge> edges = new List<Edge>();
        foreach (Triangle t in DT.TriangleSet.Keys) {
            Node n1 = nodes.Find(n => n.Position.Equals(t.V1.Position));
            Node n2 = nodes.Find(n => n.Position.Equals(t.V2.Position));
            Node n3 = nodes.Find(n => n.Position.Equals(t.V3.Position));
            edges.Add(new Edge(n1, n2, (n1.Position - n2.Position).sqrMagnitude));
            edges.Add(new Edge(n2, n3, (n2.Position - n3.Position).sqrMagnitude));
            edges.Add(new Edge(n3, n1, (n3.Position - n1.Position).sqrMagnitude));
        }
        SpanningTree spt = new SpanningTree(nodes.ToArray(),edges.ToArray());
        spt.createTree(0);
        Spt = spt;
    }
    void createMapGraph(List<RoomArea> roomareas, float edgerelocationrate)
    {
        Debug.logger.logEnabled = false;
        //Vector2 upperlimit = AverageRoomSize * 100f;
        List<Vertex> vertexs = new List<Vertex>();
        foreach (RoomArea area in roomareas)
        {
            Vertex v = new Vertex(area.CenterToInt);
            vertexs.Add(v);
        }
        Debug.logger.LogFormat(LogType.Log,"v:{0} A:{1}",vertexs.Count,roomareas.Count);
        List<Node> nodes = new List<Node>();
        List<Edge> edges = new List<Edge>();
        DelaunayTriangles dt = new DelaunayTriangles(new Rect(Vector2.zero, DgParam.MaxMapSize), vertexs);
        if (vertexs.Count > 3)
        {

            dt.DelaunayTrianglation();
            nodes = Node.createNodes(new List<Triangle>(dt.TriangleSet.Keys).ToArray());
            edges = Edge.createEdges(new List<Triangle>(dt.TriangleSet.Keys).ToArray());
            //Debug.Log(string.Format("N:{0},E:{1}", nodes.Count, edges.Count));
        }
        else
        {
            Debug.Log("Vertex amount lower than 3!!!");
            List<Node> ns = new List<Node>();
            List<Node> select = new List<Node>();
            List<Edge> eds = new List<Edge>();
            vertexs.ForEach(v => ns.Add(new Node(v.Position)));
            int maxcount = Mathf.Max(0,ns.Count - 1);
            for (int i = 0; i < maxcount; i++) {
                var rest = ns.FindAll(n=> !select.Contains(n));
                var sel = rest[Rnd.Next(rest.Count)];
                select.Add(sel);
            }
            var rest2 = ns.FindAll(n => !select.Contains(n));
            rest2.ForEach(s2 => {
                select.ForEach(s1 =>
                {
                    eds.Add(new Edge(s2,s1,(s1.Position-s2.Position).sqrMagnitude ));
                });
            });
            nodes = ns;
            edges = eds;
        }
        var restv = vertexs.FindAll( v => nodes.Find(n => n.Position.Equals(v.Position))==null);
        Debug.logger.LogFormat(LogType.Warning,"Rest Vertex:{0}", restv.Count());
        if (restv.Count != 0) {
            restv.ForEach(v => {
                var n1 = new Node(v.Position);
                var n2 = nodes[Rnd.Next(nodes.Count)];
                nodes.Add(n1);
                edges.Add(new Edge(n1,n2,(n1.Position - n2.Position).sqrMagnitude));
            });
        }
        SpanningTree spt = new SpanningTree(nodes.ToArray(), edges.ToArray());
        Debug.logger.LogFormat(LogType.Log,"N:{0},E:{1}", nodes.Count, edges.Count);
        //Debug.Log("Create Tree");
        spt.createTree(0);
        //Debug.Log("Create Tree End");
        ////Debug.Log("After: " + RoomAreas.Count);
        var es = new List<Edge>(spt.NewEdgeList);
        edges.RemoveAll(e => spt.NewEdgeList.Contains(e));
        ////Debug.Log("Edgecount: " + edges.Count);
        int limit = (int)(edges.Count * edgerelocationrate);
        ////Debug.Log("Limit: "+limit+","+edgerelocationrate);
        for (int i = 0; i < limit; i++)
        {
            //////Debug.Log("Edgecount: "+edges.Count);
            Edge adding = edges[Rnd.Next(edges.Count)];
            es.Add(adding);
            edges.Remove(adding);
            if (edges.Count == 0) break;
        }
        //es.Distinct();
        Mapgraph = new MapGraph(spt.NewNodeList, es, dt, spt, roomareas);
        Debug.logger.logEnabled = true;
    }
    public void createMapGraph(List<RoomArea> roomareas,float edgerelocationrate)
    {
        if (roomareas.Count == 0) {
            Debug.LogAssertion("Empty RoomAreas.");
            throw new ArgumentException();
        }
        //Vector2 upperlimit = AverageRoomSize * 100f;
        List<Vertex> vertexs = new List<Vertex>();
        foreach (RoomArea area in roomareas) {
            Vertex v = new Vertex(area.CenterToInt);
            vertexs.Add(v);
        }
        Debug.Log("v: "+vertexs.Count);
        List<Node> nodes = new List<Node>();
        List<Edge> edges = new List<Edge>();
        DelaunayTriangles dt = new DelaunayTriangles(new Rect(Vector2.zero, MaxMapSize), vertexs);
        if (vertexs.Count >= 3)
        {

            dt.DelaunayTrianglation();
            nodes = Node.createNodes(new List<Triangle>(dt.TriangleSet.Keys).ToArray());
            edges = Edge.createEdges(new List<Triangle>(dt.TriangleSet.Keys).ToArray());
            //Debug.Log(string.Format("N:{0},E:{1}", nodes.Count, edges.Count));
        }
        else{
            Debug.Log("Vertex amount lower than 3!!!");
            foreach (Vertex v in vertexs) {
                nodes.Add(new Node(v.Position));
            }
            Node n1 = nodes[0];
            Node n2 = nodes[nodes.Count - 1];
            float cost = (n1.Position - n2.Position).sqrMagnitude;
            edges.Add(new Edge(n1,n2,cost));
        }
        SpanningTree spt = new SpanningTree(nodes.ToArray(), edges.ToArray());
        Debug.LogFormat("N:{0},E:{1}",nodes.Count,edges.Count);
        //Debug.Log("Create Tree");
        spt.createTree(0);
        //Debug.Log("Create Tree End");
        ////Debug.Log("After: " + RoomAreas.Count);
        var es = new List<Edge>(spt.NewEdgeList);
        edges.RemoveAll(e => spt.NewEdgeList.Contains(e));
        ////Debug.Log("Edgecount: " + edges.Count);
        int limit = (int)(edges.Count * edgerelocationrate);
        ////Debug.Log("Limit: "+limit+","+edgerelocationrate);
        for (int i = 0; i < limit; i++)
        {
            //////Debug.Log("Edgecount: "+edges.Count);
            Edge adding = edges[GameController.Rnd.Next(edges.Count)];
            es.Add(adding);
            edges.Remove(adding);
            if (edges.Count == 0) break;
        }
        //es.Distinct();
        Mapgraph =  new MapGraph(spt.NewNodeList, es,dt,spt,roomareas);
    }