} // rotacionaImagemComDoisEixos2D() /// <summary> /// aplica a imagem [cena] um eixo X e Y dados, sendo o eixo X /// rotacionado por um ângulo [angle]; o eixo Y é calculado por este método. /// </summary> /// <param name="cena">imagem em processamento.</param> /// <param name="angle">ângulo em graus para o incremento no eixo X e no eixo Y.</param> /// <param name="eixoX_2D">eixo X calculado manualmente com marcação na imagem [cena].</param> /// <param name="dimCells">dimensões da imagem final, já processada.</param> /// <returns>retorna a imagem rotacionada.</returns> public static Bitmap rotacionaImagemComUmEixo2D(Bitmap cena, double angle, vetor2 eixoX_2D, Size dimCells) { if (cena == null) { throw new Exception("erro no metodo rotacionaImagemComUmEixo2D(), imagem currente e' nula"); } double anguloInc = angle; // prepara os eixos X e Y. // O eixo Y é conseguido rotacionando o eixo X por 90 graus. // O eixo X é conseguido rotacionando o eixo Y por -90 graus. // inicializa o eixo X, a partir do eixoX_2D, que é um eixo X aparente, conseguido com marcação na imagem. vetor2 ex = new vetor2(eixoX_2D.X, eixoX_2D.Y); // eixo Y, que foi calculado neste método como uma rotação de 90 graus sobre o eixo X [ex]. vetor2 ey = angulos.rotacionaVetor(90.0F, ex); // constrói o eixo X como uma matriz [1,2]. MATRIZES.Matriz mtEixoX = new MATRIZES.Matriz(1, 2); mtEixoX.setElemento(0, 0, ex.X); mtEixoX.setElemento(0, 1, ex.Y); // constroi a matriz de transformação (dos eixos construídos para os eixos (1,0) e (0,1). MATRIZES.Matriz mtrzTransf = new MATRIZES.Matriz(2, 2); mtrzTransf.setElemento(0, 0, ex.X); mtrzTransf.setElemento(0, 1, ex.Y); mtrzTransf.setElemento(1, 0, ey.X); mtrzTransf.setElemento(1, 1, ey.Y); // calcula a matriz inversa de transformação. MATRIZES.Matriz mtrzTransfInversa = MATRIZES.Matriz.MatrizInversa(mtrzTransf); // multiplica a matriz do eixo X pela matriz inversa de transformação. MATRIZES.Matriz mtEixoXTransformado = mtEixoX * mtrzTransfInversa; // rotaciona o eixo X mtEixoXTransformado = angulos.rotacionaVetor(anguloInc, mtEixoXTransformado); // constroi o eixo Y como sendo uma rotação de 90.0F graus no eixo X. MATRIZES.Matriz mtEixoYTransformado = angulos.rotacionaVetor(90.0F, mtEixoXTransformado); // mutliplica os eixos X e Y pela matriz de transformação, retornando para o universo 2D inicial. mtEixoXTransformado = mtEixoXTransformado * mtrzTransf; mtEixoYTransformado = mtEixoYTransformado * mtrzTransf; // converte a matriz do eixo X para um [vetor2]. vetor2 eixoXFinal = new vetor2((double)mtEixoXTransformado.getElemento(0, 0), (double)mtEixoXTransformado.getElemento(0, 1)); // converte a matriz do eixo Y para um [vetor2]. vetor2 eixoYFinal = new vetor2((double)mtEixoYTransformado.getElemento(0, 0), (double)mtEixoYTransformado.getElemento(0, 1)); // normaliza os eixos finais. eixoXFinal = vetor2.normaliza(eixoXFinal); eixoYFinal = vetor2.normaliza(eixoYFinal); // retorna uma Imagem construida com os eixos finais. return(new Bitmap(aplicaEixos(cena, eixoXFinal, eixoYFinal, dimCells))); } // rotacionaImagemComUmEixo2D
} // Sort(). /// <summary> /// tranforma um vetor3 em uma matriz. /// </summary> /// <param name="v">vetor 3D a ser transformado em matriz.</param> /// <returns>retorna uma matriz [1,3] representando o vetor 3D.</returns> public static Matriz DeVetorParaMatriz(vetor3 v) { Matriz mr = new Matriz(1, 3); mr.setElemento(0, 0, v.X); mr.setElemento(0, 1, v.Y); mr.setElemento(0, 2, v.Z); return(mr); } // toMatriz()
} // rotacionaVetor() public static MATRIZES.Matriz rotacionaVetor(double anguloEmGraus, MATRIZES.Matriz mtv) { vetor2 vInicial = new vetor2((double)mtv.getElemento(0, 0), (double)mtv.getElemento(0, 1)); vetor2 vFinal = rotacionaVetor(anguloEmGraus, vInicial); MATRIZES.Matriz mtFinal = new MATRIZES.Matriz(1, 2); mtFinal.setElemento(0, 0, (double)vFinal.X); mtFinal.setElemento(0, 1, (double)vFinal.Y); return(mtFinal); }
} // raio() /// <summary> /// converte o tipo [Matriz] para o tipo [vetor3], se houver /// compatibilizaçao de linhas e colunas da matriz de entrada (dimensões [3,1]). /// Se não houver compatibilização de dimensões, lança uma exceção. /// </summary> /// <param name="mi">[Matriz] a ser convertida.</param> /// <returns>o vetor3 resulante da conversão.</returns> public static vetor3 deMatrizTovetor3(Matriz mi) { // se as dimensões não são compatíveis, lança uma exceção. if ((mi.qtLin != 3) && (mi.qtCol != 1)) { throw new Exception("dimensões da matriz: " + mi.ToString() + "de entrada não são compatíveis com as dimensões de vetor3"); } vetor3 v = new vetor3(mi.getElemento(0, 0), mi.getElemento(1, 0), mi.getElemento(2, 0)); return(v); } // tovetor3()
} // Matriz operator -() /// <summary> /// MULTIPLICA DUAS MATRIZES [A] E [B], DESDE QUE [A] E [B] OBEDECAM A REGRA: /// O NUMERO DE COLUNAS DE [A] DEVE SER IGUAL AO NUMERO DE COLUNAS DE [B]. /// </summary> /// <param name="A">MATRIZ MULTIPLICANDA [A].</param> /// <param name="B">MATRIZ MULTIPLICANDA [B].</param> /// <returns>RETORNA O PRODUTO DA MULTIPLICACAO DE [A] POR [B].</returns> public static Matriz operator *(Matriz A, Matriz B) { if (A.qtCol == B.qtLin) { Matriz C = Matriz.MulMatriz(A, B); return(C); } else { return(null); } } // operator *
} // Adjunta() /// <summary> /// calcula a matriz Transposta da matriz que chamou o metodo. /// </summary> /// <returns>retorna a matriz Transposta.</returns> private Matriz Transposta() { Matriz M = new Matriz(this.qtCol, this.qtLin); int lin, col; for (lin = 0; lin < this.qtLin; lin++) { for (col = 0; col < this.qtCol; col++) { M.matriz[col, lin] = this.matriz[lin, col]; } } return(M); } // Transposta()
} // Matriz() /// metodo controle para construtores e clone. Copia para /// a matriz que chamou o metodo a matriz parametro. /// <param name="M">matriz parametro a ser copiada.</param> public void initMatriz(Matriz M) { this.qtLin = M.qtLin; this.qtCol = M.qtCol; this.matriz = new double[this.qtLin, this.qtCol]; int lin, col; for (lin = 0; lin < this.qtLin; lin++) { for (col = 0; col < this.qtCol; col++) { this.matriz[lin, col] = M.matriz[lin, col]; } } } // Matriz()
} // Transposta() /// <summary> /// calcula o determinante de uma matriz. /// </summary> /// <param name="M">Matriz a ter o determinante calculado.</param> /// <returns></returns> private static double determinante(Matriz M) { if (M.qtLin == 2) { double d = M.matriz[0, 0] * M.matriz[1, 1] - M.matriz[0, 1] * M.matriz[1, 0]; return(d); } // if M.qtLin else if (M.qtLin == 3) { double d = +M.matriz[0, 0] * M.matriz[1, 1] * M.matriz[2, 2] + M.matriz[0, 1] * M.matriz[1, 2] * M.matriz[2, 0] + M.matriz[0, 2] * M.matriz[1, 0] * M.matriz[2, 1] - M.matriz[0, 2] * M.matriz[1, 1] * M.matriz[2, 0] - M.matriz[0, 0] * M.matriz[1, 2] * M.matriz[2, 1] - M.matriz[0, 1] * M.matriz[1, 0] * M.matriz[2, 2]; return(d); } else if (M.qtLin == 1) { return(M.matriz[0, 0]); } else if (M.qtLin > 3) { double d = 0.0; double sinal = 1.0; for (int col = 0; col < M.qtCol; col++) { if ((col % 2) == 0) { sinal = 1.0; } else { sinal = -1.0; } MATRIZES.Matriz N = M.ReduzMatriz(0, col); d += sinal * determinante(N); } // for col return(d); } // if M.qtLin>3 return(0.0); } // double determinante()
} // MatrizInversa() /// <summary> /// calcula a matriz Transposta da matriz que chamou o metodo. /// </summary> /// <returns>retorna a matriz Transposta da matriz que chamou o método.</returns> private Matriz Adjunta() { int lin, col; Matriz Cofatores = new Matriz(this.qtLin, this.qtCol); for (lin = 0; lin < this.qtLin; lin++) { for (col = 0; col < this.qtCol; col++) { Cofatores.matriz[lin, col] = Matriz.determinante(this.ReduzMatriz(lin, col)); if (((lin + col) % 2) == 1) { Cofatores.matriz[lin, col] = -Cofatores.matriz[lin, col]; } } // for col } return(Cofatores); } // Adjunta()
} // Matriz operator +() /// <summary> /// subtrai duas matrizes. Operador de subtração. /// </summary> /// <param name="A">Matriz Parametro.</param> /// <param name="B">Matriz Parametro.</param> /// <returns>retorna uma Matriz diferença entre as matrizes [A] e [B].</returns> public static Matriz operator -(Matriz A, Matriz B) { try { Matriz C = new Matriz(A.qtLin, A.qtCol); for (int lin = 0; lin < A.qtLin; lin++) { for (int col = 0; col < A.qtCol; col++) { C.matriz[lin, col] = A.matriz[lin, col] - B.matriz[lin, col]; } // for int col } return(C); } // try catch { return(null); } // catch } // Matriz operator -()
} // Clone() /// multiplica duas matrizes, e retorna como produto uma matriz. /// <param name="A">matriz operando 1.</param> /// <param name="B">matriz operando 2.</param> public static Matriz MulMatriz(Matriz A, Matriz B) { Matriz C = new Matriz(A.qtLin, B.qtCol); int lin, col, k; for (lin = 0; lin < C.qtLin; lin++) { for (col = 0; col < C.qtCol; col++) { C.matriz[lin, col] = 0.0; for (k = 0; k < A.qtCol; k++) { C.matriz[lin, col] += A.matriz[lin, k] * B.matriz[k, col]; } } // for col } // for lin return(C); } // MulMatriz()
} // void setElemento() public static Matriz Identidade(int dim) { Matriz I = new Matriz(dim, dim); for (int l = 0; l < dim; l++) { for (int c = 0; c < dim; c++) { if (l == c) { I.matriz[l, c] = 1.0; } else { I.matriz[l, c] = 0.0; } } // for int c } return(I); } //Identidade()
} // maxMatriz() /// <summary> /// DIVIDE QUALQUER MATRIZ [A] POR [B], DESDE QUE [A] E [B] OBEDECAM A REGRA DE /// MULTIPLICACAO DE MATRIZES: O NUMERO DE COLUNAS DE [A] DEVE SER IGUAL AO NU- /// MERO DE COLUNAS DE [B]. OU AINDA NUMERO DE LINHAS IGUAIS POR [A] E [B]. /// </summary> /// <param name="A">Matriz a ser dividida.</param> /// <param name="B">Matriz divisora.</param> /// <returns>retorna o produto da divisao.</returns> public static Matriz operator /(Matriz v3, Matriz v2) { if (v3.qtCol == v2.qtCol) { aleatorizador = new Random(100); Matriz aux = new Matriz(v2.qtCol, v2.qtLin); aux.preencheMatriz(8); Matriz aux2 = new Matriz(v2.qtLin, v2.qtLin); aux2.preencheMatriz(6); Matriz invV2AuxPlusAux2plus = Matriz.MatrizInversa((v2 * aux) + aux2); Matriz T = ((v3 * aux) * invV2AuxPlusAux2plus); return(T); } else if (v2.qtLin == v3.qtLin) { aleatorizador = new Random(100); Matriz aux = new Matriz(v2.qtCol, v2.qtLin); aux.preencheMatriz(8); Matriz aux2 = new Matriz(v2.qtCol, v2.qtCol); aux2.preencheMatriz(6); Matriz invV2AuxPlusAux2plus = Matriz.MatrizInversa((aux * v2) + aux2); Matriz T = ((aux * v3) * invV2AuxPlusAux2plus); return(T); } // if else { throw new Exception("Parametros incorretos para a divisao de matrizes"); } // else }// operator /
}// operator / /// <summary> /// calcula a matriz inversa da matriz parametro. /// </summary> /// <param name="A">matriz a ter a inversa calculada.</param> /// <returns></returns> public static Matriz MatrizInversa(Matriz A) { if ((A.qtCol == 1) && (A.qtLin == 1)) { Matriz M = new Matriz(1, 1); M.matriz[0, 0] = 1 / A.matriz[0, 0]; return(M); } // if else { Matriz Adjt = A.Adjunta(); Matriz M = new Matriz(A.qtLin, A.qtCol); int lin, col; double D = Matriz.determinante(A); for (lin = 0; lin < A.qtLin; lin++) { for (col = 0; col < A.qtCol; col++) { M.matriz[lin, col] = Adjt.matriz[lin, col] / D; } } return(M.Transposta()); } } // MatrizInversa()
/// <summary> /// rotaciona uma imagem nos eixos X e Y por um angulo, /// sendos os eixos retirados a partir de marcação numa imagem. /// </summary> /// <param name="cena">Imagem a ser rotacionada.</param> /// <param name="angle">angulo em graus para o qual os eixos X e Y serem transformados.</param> /// <param name="eixoX_2D">eixo X conseguido através de marcação na imagem.</param> /// <param name="eixoY_2D">eixo Y conseguido através de marcação na imagem.</param> /// <param name="dimCell">dimensões da imagem final, já processada.</param> /// <returns>retorna uma imagem rotacionada pelo angulo em graus [angle], aplicado em eixos X e Y, /// transformados para (1,0) e (0,1).</returns> public static Bitmap rotacionaImagemComDoisEixos2D(Bitmap cena, double[] angle, vetor2 eixoX_2D, vetor2 eixoY_2D, Size dimCell) { if (cena == null) { throw new Exception("erro no metodo rotacionaImagemComUmEixo2D(), imagem currente e' nula"); } // inicializa o eixo X, a partir do eixoX_2D, que é um eixo X aparente, conseguido com marcação na imagem. vetor2 ex = new vetor2(eixoX_2D.X, eixoX_2D.Y); // inicializa o eixo Y, a partir do eixoY_2D, que é um eixo Y aparente, conseguido com marcação na imagem. vetor2 ey = new vetor2(eixoY_2D.X, eixoY_2D.Y); // normaliza o eixo X [ex]. ex = vetor2.normaliza(ex); // normaliza o eixo Y [ey]. ey = vetor2.normaliza(ey); // constrói o eixo X como uma matriz [1,2]. MATRIZES.Matriz mtEixoX = new MATRIZES.Matriz(1, 2); mtEixoX.setElemento(0, 0, ex.X); mtEixoX.setElemento(0, 1, ex.Y); // constrói o eixo Y como uma matriz [1,2]. MATRIZES.Matriz mtEixoY = new MATRIZES.Matriz(1, 2); mtEixoY.setElemento(0, 0, ey.X); mtEixoY.setElemento(0, 1, ey.Y); // constroi a matriz de transformação (dos eixos construídos para os eixos (1,0) e (0,1). MATRIZES.Matriz mtrzTransf = new MATRIZES.Matriz(2, 2); mtrzTransf.setElemento(0, 0, ex.X); mtrzTransf.setElemento(0, 1, ex.Y); mtrzTransf.setElemento(1, 0, ey.X); mtrzTransf.setElemento(1, 1, ey.Y); // calcula a matriz inversa de transformação. MATRIZES.Matriz mtrzTransfInversa = MATRIZES.Matriz.MatrizInversa(mtrzTransf); // multiplica a matriz do eixo X pela matriz inversa de transformação. MATRIZES.Matriz mtEixoXTransformado = mtEixoX * mtrzTransfInversa; MATRIZES.Matriz mtEixoYTransformado = mtEixoY * mtrzTransfInversa; // rotaciona o eixo X transformado, com o ângulo 0. mtEixoXTransformado = angulos.rotacionaVetor(angle[0], mtEixoXTransformado); // rotaciona o eixo Y transformado, com o ângulo 1. mtEixoYTransformado = angulos.rotacionaVetor(angle[1], mtEixoYTransformado); // mutliplica os eixos X e Y pela matriz de transformação, retornando para o universo 2D inicial. mtEixoXTransformado = mtEixoXTransformado * mtrzTransf; mtEixoYTransformado = mtEixoYTransformado * mtrzTransf; // converte a matriz do eixo X para um [vetor2]. vetor2 eixoXFinal = new vetor2((double)mtEixoXTransformado.getElemento(0, 0), (double)mtEixoXTransformado.getElemento(0, 1)); // converte a matriz do eixo Y para um [vetor2]. vetor2 eixoYFinal = new vetor2((double)mtEixoYTransformado.getElemento(0, 0), (double)mtEixoYTransformado.getElemento(0, 1)); // normaliza os eixos finais. eixoXFinal = vetor2.normaliza(eixoXFinal); eixoYFinal = vetor2.normaliza(eixoYFinal); // retorna uma Imagem construida com os eixos finais. return(new Bitmap(aplicaEixos(cena, eixoXFinal, eixoYFinal, dimCell), dimCell)); } // rotacionaImagemComDoisEixos2D()