/// <summary> /// 判断一个卦画是否由另一个卦画,通过指定的方式变换而来。 /// Judge whether the object painting is derived from the basis painting, /// through the given derivation function. /// </summary> /// <param name="obj"> /// 要判断的目标卦画。 /// The object painting to judge. /// </param> /// <param name="basis"> /// 作为判断基础的卦画。 /// The paintings to be used as the comparing basis. /// </param> /// <param name="derivationFunction"> /// 变换过程函数。 /// The derivation function. /// </param> /// <returns> /// 判断结果。 /// The result. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="basis"/> 或 <paramref name="derivationFunction"/> 或 <paramref name="obj"/> 是 <c>null</c> 。 /// <paramref name="basis"/> or <paramref name="derivationFunction"/> or <paramref name="obj"/> is <c>null</c>. /// </exception> public static bool IsDerivedFrom( this Core.Painting obj, Core.Painting basis, DerivationFunc derivationFunction) { if (obj is null) { throw new ArgumentNullException(nameof(obj)); } if (basis is null) { throw new ArgumentNullException(nameof(basis)); } if (derivationFunction is null) { throw new ArgumentNullException(nameof(derivationFunction)); } var functionComparer = new FunctionComparer(derivationFunction); return(functionComparer.Compare(basis, obj)); }
static void Main(string[] args) { #region to derive from a painting _ = Painting.TryParse("110000", out Painting painting); Debug.Assert(painting is not null); // Traditionally: IDerivation derivation = new OverturningDerivation(); Console.WriteLine($"110000 -o> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -o> 000011 derivation = new ChangingDerivation(3, 4); Console.WriteLine($"110000 -c> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -c> 110110 derivation = new FunctionDerivation((painting) => { return(new Painting(painting.Select((_, _) => YinYang.Yang))); }); // This derivation will turn all the lines to yang lines. Console.WriteLine($"110000 -f> {derivation.Derive(painting)}"); Console.WriteLine(); // Output: 110000 -f> 111111 // With extension methods: Console.WriteLine($"110000 -o2> {painting.ToOverlapping()}"); Console.WriteLine(); // Output: 110000 -o2> 100000 Console.WriteLine($"110000 -o2> " + $"{painting.ApplyDerivation((_) => new Painting(YinYang.Yang))}"); Console.WriteLine(); // Output: 110000 -o2> 1 #endregion #region to compare two paintings _ = Painting.TryParse("110000", out Painting painting1); _ = Painting.TryParse("000011", out Painting painting2); _ = Painting.TryParse("111111", out Painting painting3); Debug.Assert(painting1 is not null && painting2 is not null); // Traditionally: IComparer <bool> comparer = new OverlappingComparer(); Console.WriteLine(comparer.Compare(painting1, painting2)); Console.WriteLine(); // Output: False comparer = new OverturningComparer(); Console.WriteLine(comparer.Compare(painting1, painting2)); Console.WriteLine(); // Output: True comparer = new FunctionComparer(derivation); // The current derivation will turn all the lines to yang lines, // and now the compare will check that. Console.WriteLine($"{comparer.Compare(painting1, painting3)} " + $"{comparer.Compare(painting3, painting1)}"); Console.WriteLine(); // Output: True False // With extension methods: Console.WriteLine(painting1.IsOverturnedFrom(painting2)); Console.WriteLine(); // Output: True foreach (var lineIndex in painting1.GetDifferentLinesBetween(painting2)) { Console.Write(lineIndex); } Console.WriteLine(); Console.WriteLine(); // Output: 0145 #endregion }