/// <summary> /// Adds point to list, keeping order with the other nodes /// Returns true if addition successful, false otherwise /// </summary> public bool Add(ModelPoint point) { // check that point is within the model's bounds if (!PointInBounds(point)) { Debug.LogWarning("Point " + point.ToString() + " is not in bounds"); return(false); } // don't add if point already in the list if (Contains(point)) { Debug.LogWarning("List already contains point " + point.ToString()); return(false); } ModelPointNode currentNode = _start; while (currentNode.next != null && !(_comparer.Compare(point, currentNode.Value) == 1 && _comparer.Compare(point, currentNode.next.Value) == -1)) { currentNode = currentNode.next; } if (currentNode.next == null) { return(false); } else { ModelPointNode newNode = new ModelPointNode(point); ModelPointNode nextNode = currentNode.next; currentNode.next = newNode; newNode.next = nextNode; newNode.previous = currentNode; nextNode.previous = newNode; return(true); } }
/// <summary> /// Removes point from the list /// </summary> public bool Remove(ModelPoint point) { // can't remove an out of bounds point if (!PointInBounds(point)) { Debug.LogWarning("Point " + point.ToString() + " is not in bounds"); return(false); } // can't remove the start or end points if (point == _start.Value || point == _end.Value) { Debug.LogWarning("Cannot remove the start or end point"); return(false); } // iterate through intermediate points to find the point ModelPointNode currentNode = _start; while (currentNode.next != null && currentNode.next.Value != point) { currentNode = currentNode.next; } // point not in list if (currentNode.next == null) { return(false); } else { ModelPointNode newNext = currentNode.next.next; currentNode.next = newNext; newNext.previous = currentNode; return(true); } }