/// <summary> /// Adds an intersection between two line segments. /// </summary> /// <param name="lineSegment1">The first line segment.</param> /// <param name="lineSegment2">The second line segment.</param> /// <remarks> /// <para>Each of the two line segments is added to the other's set of intersecting line /// segments and the number of intersections in the game level is increased by 1.</para> /// </remarks> private void AddIntersection(LineSegment lineSegment1, LineSegment lineSegment2) { _intersections[lineSegment1].Add(lineSegment2); _intersections[lineSegment2].Add(lineSegment1); IntersectionCount++; lineSegment1.State = LineSegmentState.Intersected; lineSegment2.State = LineSegmentState.Intersected; }
/// <summary> /// Removes an intersection between two line segments. /// </summary> /// <param name="lineSegment1">The first line segment.</param> /// <param name="lineSegment2">The second line segment.</param> /// <remarks> /// <para>Each of the two line segments is removed from the other's set of intersecting /// line segments and the number of intersections in the game level is decreased by 1. /// </para> /// </remarks> private void RemoveIntersection(LineSegment lineSegment1, LineSegment lineSegment2) { HashSet<LineSegment> intersectingSegments1 = _intersections[lineSegment1]; HashSet<LineSegment> intersectingSegments2 = _intersections[lineSegment2]; intersectingSegments1.Remove(lineSegment2); intersectingSegments2.Remove(lineSegment1); IntersectionCount--; lineSegment1.State = (intersectingSegments1.Count > 0 ? LineSegmentState.Intersected : LineSegmentState.Normal); lineSegment2.State = (intersectingSegments2.Count > 0 ? LineSegmentState.Intersected : LineSegmentState.Normal); }
/// <summary> /// Connects the vertex to another vertex with a line segment and returns it. /// </summary> /// <param name="otherVertex">The vertex which should be connected to the current one. /// </param> /// <returns>The created line segment between the two vertices.</returns> /// <exception cref="InvalidOperationException"> /// An attempt is made to connect the vertex to itself. /// /// -or- /// A line segment between the two vertices already exists. /// </exception> public LineSegment ConnectToVertex(Vertex otherVertex) { if (this == otherVertex) throw new InvalidOperationException("A vertex cannot be connected to itself."); if (_lineSegmentsMap.ContainsKey(otherVertex)) { throw new InvalidOperationException( "A line segment between the two vertices already exists."); } var lineSegment = new LineSegment(this, otherVertex); _lineSegmentsMap[otherVertex] = lineSegment; otherVertex._lineSegmentsMap[this] = lineSegment; return lineSegment; }