public void MathOp(MathOperationType operation, Imagem imagemB, int r) { if (imagemB != null) { int width = this.MatrizCor.Width; int height = this.MatrizCor.Height; int[] rgbA = { 0, 0, 0 }, rgbB = { 0, 0, 0 }; int x, y, canal; for (x = r; x < width - r; x++) { for (y = r; y < height - r; y++) { for (canal = 0; canal < 3; canal++) { rgbA[canal] = this.MatrizCor.Matriz[x, y, canal]; rgbB[canal] = imagemB.MatrizCor.Matriz[x - r, y - r, canal]; switch (operation) { case MathOperationType.adicao: rgbA[canal] += rgbB[canal]; break; case MathOperationType.divisao: rgbA[canal] = (rgbA[canal] < 1) ? 1 : rgbA[canal]; rgbA[canal] /= rgbB[canal]; break; case MathOperationType.multiplicacao: rgbA[canal] *= rgbB[canal]; break; case MathOperationType.subtracao: rgbA[canal] -= rgbB[canal]; break; } this.MatrizCor.Matriz[x, y, canal] = rgbA[canal]; } } } } }
/// <exception cref="System.ArgumentException">Throws when operationtype not exist in enum values. /// or double numbers args are NaN or Infinity</exception> /// <exception cref="System.ArithmeticException">Throws when lazyResult is false and mathematical operation return Infinity result.</exception> public MathOperation(double leftNumber, MathOperationType operationType, double rightNumber, bool lazyResult = true) { if (!Enum.IsDefined(typeof(MathOperationType), operationType)) { throw new ArgumentException($"Bad MathOperationType={operationType}"); } if (double.IsNaN(leftNumber) || double.IsInfinity(leftNumber) || double.IsNaN(rightNumber) || double.IsInfinity(rightNumber)) { throw new ArgumentException($"Number args Error: leftNumber:{leftNumber}, rightNumber:{rightNumber}"); } this.LeftNumber = leftNumber; this.RightNumber = rightNumber; this.OperationType = operationType; this.lazyResult = lazyResult; if (lazyResult == false) { this._result = getMathResult(); } }
public static bool Has(MathOperationType operationType, params Type[] operandTypes) => !(Get(operationType, operandTypes) is null);
public static MathOperation Get(MathOperationType operationType, params Type[] operandTypes) => GetAll(operandTypes).Find(o => o.OperationType == operationType);
public IOperationCalculator Resolve(MathOperationType operationType) => resolvers.TryGetValue(operationType, out var resolver) ? resolver : throw new ArgumentException($"{nameof(IOperationCalculator)} for {operationType} is not defined");