private List <NodeTriple> buildNodeTriples(List <Node> nodes) { // Build triples for connected nodes such that: // the nodes are connected as follows: (n1)----(n2)----(n3) List <NodeTriple> nodeTriples = new List <NodeTriple>(); foreach (Node n in nodes) { foreach (Edge e1 in n.edges) { foreach (Edge e2 in n.edges) { if (e1.Equals(e2)) { continue; } Node other1 = e1.getOpposite(n); Node other2 = e2.getOpposite(n); NodeTriple nt1 = new NodeTriple(other1, n, other2); NodeTriple nt2 = new NodeTriple(other2, n, other1); if (!nodeTriples.Contains(nt1)) { nodeTriples.Add(nt1); } if (!nodeTriples.Contains(nt2)) { nodeTriples.Add(nt2); } } } } return(nodeTriples); }
private void addCornerToBlock(ref Block block, NodeTriple old) { if (old.n2.onHighway) { block.highwayAdjacent = true; } block.corners.Add(old.n2.pos); block.nodeTriples.Add(old); }
// Find next nodetriple in sorted list private NodeTriple findNext(List <NodeTriple> triples, NodeTriple old) { for (int i = 0; i < triples.Count; i++) { if (old.n2.Equals(triples[i].n1) && old.n3.Equals(triples[i].n2)) { return(triples[i]); } } return(null); }
public override bool Equals(object other) { if (other == null) { return(false); } NodeTriple otherNodeTriple = (NodeTriple)other; if (this.n1.Equals(otherNodeTriple.n1) && this.n2.Equals(otherNodeTriple.n2) && this.n3.Equals(otherNodeTriple.n3)) { return(true); } return(false); }
public NodeTriple getBestSuccessor() { float bestAngle = float.MaxValue; NodeTriple bestSuccessor = null; foreach (NodeTriple a in successors) { // First node triple angle Vector2 a1 = a.n2.pos - a.n1.pos; Vector2 a2 = a.n3.pos - a.n2.pos; a1 = -a1; float angleA = Mathf.Atan2(a2.y, a2.x) - Mathf.Atan2(a1.y, a1.x); if (angleA < bestAngle) { bestAngle = angleA; bestSuccessor = a; } } return(bestSuccessor); }
// Finds all blocks enclosed by roads in the roadmap. private List <Block> getPolygon2Ds(List <NodeTriple> nodeTriples) { List <Block> polygons = new List <Block>(); // Repeat until we have considered all triples while (nodeTriples.Count > 0) { // Initialize new polygon object Block currentPolygon = new Block(); bool validPolygon = true; // Reference to first triple NodeTriple old = nodeTriples[0]; Node startNode = old.n2; //we store the nodeTriples which are used for this polygon List <NodeTriple> consideredTriples = new List <NodeTriple> (); consideredTriples.Add(old); NodeTriple next = null; //Debug.Log("New polygon being created with start nodetriple: " + old); while (!startNode.Equals((next == null) ? null : next.n2)) { //find the next node triple next = findNext(nodeTriples, old); if (next != null) { //Debug.Log ("Next nodetriple: " + next); //the node was already there, and it is not the start node.. invalid polygon if (currentPolygon.corners.Contains(next.n2.pos) && !next.n2.Equals(startNode)) { validPolygon = false; break; } addCornerToBlock(ref currentPolygon, next); consideredTriples.Add(next); } else { //road ends here validPolygon = false; break; } old = next; } //remove the triples that have been considered already foreach (NodeTriple nt in consideredTriples) { nodeTriples.Remove(nt); } //if the polygon is valid if (validPolygon) { //make sure the polygon is in clockwise order if (isClockwise(currentPolygon)) { //when the smallest angle is large enough, continue if (currentPolygon.getMinAngle() > 30) { currentPolygon.shrinkBlock(); if (currentPolygon.getArea() > CityGenerator.minBlockArea && currentPolygon.getArea() < CityGenerator.maxBlockArea) { polygons.Add(currentPolygon); } } } } } return(polygons); }