protected override void OnComponentsChanged() { base.OnComponentsChanged(); _cfCoordinate = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.C || hvc.Direction == HexAxis.F); _daCoordinate = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.D || hvc.Direction == HexAxis.A); _altitude = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.Up || hvc.Direction == HexAxis.Down); }
protected override void OnComponentsChanged() { base.OnComponentsChanged(); _cfCoordinate = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.C || hvc.Direction == HexAxis.F); _daCoordinate = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.D || hvc.Direction == HexAxis.A); _altitude = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.Up || hvc.Direction == HexAxis.Down); }
private int VectorComponentToMagnitude(HexVectorComponent vectorComponent, HexAxis positiveDirection) { if (vectorComponent == null) { return(0); } int coordinateSign = vectorComponent.Direction == positiveDirection ? +1 : -1; return(coordinateSign * vectorComponent.Magnitude); }
private HexVectorComponent ReplaceVectorComponent(int coordinateValue, HexVectorComponent existingComponent, HexAxis positiveAxis, HexAxis negativeAxis) { if (existingComponent != null) { _components.Remove(existingComponent); } var newComponent = new HexVectorComponent(coordinateValue >= 0 ? positiveAxis : negativeAxis, Math.Abs(coordinateValue)); _components.Add(newComponent); return(newComponent); }
public void TestComponentEquality() { var componentA = new HexVectorComponent(HexAxis.A, 5); var componentB = new HexVectorComponent(HexAxis.A, 7); var componentC = new HexVectorComponent(HexAxis.C, 2); var componentD = new HexVectorComponent(HexAxis.A, 5); componentA.Equals(componentB).Should().BeFalse(); componentA.Equals(componentC).Should().BeFalse(); componentA.Equals(componentD).Should().BeTrue(); (componentA == componentD).Should().BeFalse(); componentA.Equals(HexVectorComponent.Zero).Should().BeFalse(); HexVectorComponent.Zero.Equals(null).Should().BeTrue(); }
public HexVector( HexAxis directionA, int magnitudeA, HexAxis directionB, int magnitudeB, HexAxis directionZ, int magnitudeZ) :base() { if (directionA == HexAxis.Up || directionA == HexAxis.Down || directionA == HexAxis.Undefined) { throw new ArgumentException(string.Format("DirectionA should be planar and defined, but found {0}", directionA), "directionA"); } if (directionB == HexAxis.Up || directionB == HexAxis.Down || directionB == HexAxis.Undefined) { throw new ArgumentException(string.Format("DirectionB should be planar and defined, but found {0}", directionB), "directionA"); } if ((directionZ != HexAxis.Up && directionZ != HexAxis.Down) || directionZ == HexAxis.Undefined) { throw new ArgumentException(string.Format("DirectionA should be vertical and defined, but found {0}", directionZ), "directionA"); } if (magnitudeA < 0 || magnitudeB < 0 || magnitudeZ < 0) { throw new ArgumentException(string.Format("All magnitudes should be positive (actual magnitudes are: A{0}, B{1}, Z{2}).", magnitudeA, magnitudeB, magnitudeZ)); } if (magnitudeA >= magnitudeB) { PrimaryComponent = new HexVectorComponent(directionA, magnitudeA); SecondaryComponent = new HexVectorComponent(directionB, magnitudeB); } else { PrimaryComponent = new HexVectorComponent(directionB, magnitudeB); SecondaryComponent = new HexVectorComponent(directionA, magnitudeA); } VerticalComponent = new HexVectorComponent(directionZ, magnitudeZ); }
private HexAxis?FindVectors120Apart(List <HexVectorComponent> components, out HexVectorComponent compA, out HexVectorComponent compB) { compA = null; compB = null; if (components.Count <= 1) { return(null); } for (int i = 0; i < components.Count - 1; i++) { var componentA = components[i]; for (int j = i + 1; j < components.Count; j++) { var componentB = components[j]; var vANextDirection = _orderedPlanarDirections[componentA.Direction]; var vBNextDirection = _orderedPlanarDirections[componentB.Direction]; // Example 1: componentA.Direction = A; componentB.Direction = D. A=>B=>C != D; D=>E=>F != A; continue. // Example 2: componentA.Direction = C; componentB.Direction = E. C=>D=>E == E; E=>F=>A != C; return D. // Example 3: componentA.Direction = A; componentB.Direction = E. A=>B=>C != E; E=>F=>A == A; return F. if (_orderedPlanarDirections[vANextDirection] == componentB.Direction) { compA = componentA; compB = componentB; return(vANextDirection); } if (_orderedPlanarDirections[vBNextDirection] == componentA.Direction) { compA = componentA; compB = componentB; return(vBNextDirection); } } } return(null); }
private void SetComponent(ref HexVectorComponent component, HexVectorComponent newValue) { if (component != null) { _components.Remove(component); } component = newValue; if (newValue != null) { _components.Add(newValue); } }
protected override void OnComponentsChanged() { base.OnComponentsChanged(); _verticalComponent = Components.FirstOrDefault(hvc => hvc.Direction == HexAxis.Up || hvc.Direction == HexAxis.Down); var planarComponents = Components.Where(hvc => hvc.Direction != HexAxis.Up && hvc.Direction != HexAxis.Down).ToList(); planarComponents.Sort((va, vb) => Math.Sign(vb.Magnitude - va.Magnitude)); _primaryCompnoent = planarComponents.Count > 0 ? planarComponents[0] : null; _secondaryComponent = planarComponents.Count > 1 ? planarComponents[1] : null; }
private HexVectorComponent InvertVectorComponent(HexVectorComponent initialComponent) { var oppositeDirection = initialComponent.Direction == HexAxis.Undefined ? HexAxis.Undefined : _oppositeDirections[initialComponent.Direction]; return(new HexVectorComponent(oppositeDirection, initialComponent.Magnitude)); }
public static void CheckComponent(HexVectorComponent component, HexAxis expectedDirection, int expectedMagnitude) { component.Direction.Should().Be(expectedDirection); component.Magnitude.Should().Be(expectedMagnitude); }
private int VectorComponentToMagnitude(HexVectorComponent vectorComponent, HexAxis positiveDirection) { if (vectorComponent == null) { return 0; } int coordinateSign = vectorComponent.Direction == positiveDirection ? +1 : -1; return coordinateSign * vectorComponent.Magnitude; }
private HexVectorComponent ReplaceVectorComponent(int coordinateValue, HexVectorComponent existingComponent, HexAxis positiveAxis, HexAxis negativeAxis) { if (existingComponent != null) { _components.Remove(existingComponent); } var newComponent = new HexVectorComponent(coordinateValue >= 0 ? positiveAxis : negativeAxis, Math.Abs(coordinateValue)); _components.Add(newComponent); return newComponent; }