// 按逆时针排序插入, compare(a, b) < 0 表示a -> b为逆时针 private void InsertEdgeNode(int vertexIndex, EdgeNode edgeNode, FuncCompareEdgeByRadian compare) { EdgeNode p = this.vertexList[vertexIndex].FirstEdge, q = null; //string str = "compare: (" + (this.vertexList[vertexIndex].Cell % 144) + "," + (this.vertexList[vertexIndex].Cell / 144) + ") ->"; //str += "(" + (this.vertexList[edgeNode.AdjacentVertex].Cell % 144) + "," + (this.vertexList[edgeNode.AdjacentVertex].Cell / 144) + ") with "; while (p != null) { int rs = compare(edgeNode.EdgeLink, p.EdgeLink); //str += "(" + (this.vertexList[p.AdjacentVertex].Cell % 144) + "," + (this.vertexList[p.AdjacentVertex].Cell / 144) + ")" + "rs=" + rs; if (rs > 0) { break; } q = p; p = p.Next; } //if (this.vertexList[vertexIndex].Cell == 11038) //{ // XxdwDebugger.Log(str); //} if (q == null) { this.vertexList[vertexIndex].FirstEdge = edgeNode; } else { q.Next = edgeNode; } edgeNode.Next = p; this.vertexList[vertexIndex].Degree++; }
public bool AddEdge(int cell1, int cell2, float weight, int[] spots, string identifer, FuncCompareEdgeByRadian compare) { if (cell1 > cell2) { int temp = cell1; cell1 = cell2; cell2 = temp; } if (FindEdge(cell1, cell2) >= 0) { XxdwDebugger.Log("The edge already exists."); return(false); } Edge edge = new Edge(cell1, cell2, weight, spots, identifer); this.edgeList.Add(edge); int index1 = FindVertex(cell1); if (index1 < 0) { index1 = this.vertexList.Count; this.vertexList.Add(new VertexNode(cell1)); } int index2 = FindVertex(cell2); if (index2 < 0) { index2 = this.vertexList.Count; this.vertexList.Add(new VertexNode(cell2)); } EdgeNode edgeNode1 = new EdgeNode(index2, edge); InsertEdgeNode(index1, edgeNode1, compare); EdgeNode edgeNode2 = new EdgeNode(index1, edge); InsertEdgeNode(index2, edgeNode2, compare); return(true); }