Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ficha5;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- /**
- * Breve descrição do código
- *
- * @sid 2004
- * @aid 5.15
- */
- public class Ficha5Exercicio15_V1 {
- public static void main(String[] args) {
- //begin_inputs
- double x1=0;
- double y1=4;
- double x2=3;
- double y2=0;
- double x3=6;
- double y3=4;
- double x4=3;
- double y4=6;
- //end_inputs
- boolean paralelogramo=false; //faces opostas iguais e paralelas, diagonais intersentam-se a meio
- boolean retangulo=false; //Paralelogramo + angulos entre arestas =90º
- boolean quadrado=false; //Retangulo faces TODAS iguais
- boolean losango=false; //Paralelogramo com LADOS iguais e diagonais a 90º
- boolean kite=false; //Tem diagonais a 90º, 2 faces iguais adjacentes
- paralelogramo=verificarParalelogramo(x1,y1,x2,y2,x3,y3,x4,y4);
- if(paralelogramo) {
- retangulo=verificarRetangulo(x1,y1,x2,y2,x3,y3,x4,y4);
- losango=verificarLosango(x1,y1,x2,y2,x3,y3,x4,y4);
- //Um quadrado é um rectangulo (lados perpendiculares e diagonais perpendiculares)
- if(retangulo && losango) {
- quadrado=true;
- }
- }else{
- kite=verificarKite(x1,y1,x2,y2,x3,y3,x4,y4);
- if(kite) {
- kite=true;
- }
- }
- System.out.println("A forma gerada é:");
- if(paralelogramo) {
- System.out.println("->Paralelogramo");
- if(retangulo) {
- System.out.println("->Retangulo");
- }
- if(losango) {
- System.out.println("->Losango");
- }
- if(quadrado) {
- System.out.println("->Quadrado");
- }
- }else if(kite) {
- System.out.println("->Kite (papagaio)");
- }else {
- System.out.println("->Inregular");
- }
- }
- /**
- *
- * @param x1 (double)
- * @param y1 (double)
- * @param x2 (double)
- * @param y2 (double)
- * @return (double) distancia entre ponto 1 e 2
- */
- private static double distanciaPontos(double x1, double y1, double x2, double y2){
- return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
- }
- /**
- *
- * @param componteX (double)
- * @param componteY (double)
- * @return (double) modulo (distancia) do vetor
- */
- private static double moduloVetor(double componteX, double componteY){
- return Math.sqrt(Math.pow(componteX, 2)+Math.pow(componteY, 2));
- }
- /**
- *
- * @param xVetor1 (double)
- * @param yVetor1 (double)
- * @param xVetor2 (double)
- * @param yVetor2 (double)
- * @return (double) O produto escalar entre dois vetores. Se este produto=0 Os vetores são ortogonais
- */
- private static double produtoEscalarVetores(double xVetor1, double yVetor1, double xVetor2, double yVetor2) {
- return (xVetor1*xVetor2+yVetor1*yVetor2);
- }
- /**
- *
- * @param xVetor1 (double)
- * @param yVetor1 (double)
- * @param xVetor2 (double)
- * @param yVetor2 (double)
- * @return (double) devolve o angulo entre o vetor 1 e 2 (em graus)
- */
- private static double anguloEntreVetores(double xVetor1, double yVetor1, double xVetor2, double yVetor2) {
- double angulo= Math.toDegrees(Math.acos((produtoEscalarVetores(xVetor1,yVetor1,xVetor2,yVetor2))/(moduloVetor(xVetor1,yVetor1)*moduloVetor(xVetor2,yVetor2))));
- return BigDecimal.valueOf(angulo).setScale(8, RoundingMode.HALF_UP).doubleValue();
- }
- /**
- * Paralelogramo:
- * Lados opostos mesmo comprimento
- * Lados opostos paralelos
- *
- * @param x1 (double)
- * @param y1 (double)
- * @param x2 (double)
- * @param y2 (double)
- * @param x3 (double)
- * @param y3 (double)
- * @param x4 (double)
- * @param y4 (double)
- * @return (boolean) True se 4 pontos formam um paralelogramo
- */
- private static boolean verificarParalelogramo(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
- boolean paralelogramo=false;
- double xComprimento1=0;
- double yComprimento1=0;
- double xComprimento2=0;
- double yComprimento2=0;
- double xLargura1=0;
- double yLargura1=0;
- double xLargura2=0;
- double yLargura2=0;
- //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
- if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
- //Comrimento: 1a3 e 4a2;
- xComprimento1=x3-x1;
- yComprimento1=y3-y1;
- xComprimento2=x4-x2;
- yComprimento2=y4-y2;
- //Largura: 1a4 e 2a3;
- xLargura1=x4-x1;
- yLargura1=y4-y1;
- xLargura2=x3-x2;
- yLargura2=y3-y2;
- }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
- //Comrimento: 1a4 e 2a3;
- xComprimento1=x4-x1;
- yComprimento1=y4-y1;
- xComprimento2=x3-x2;
- yComprimento2=y3-y2;
- //Largura: 1a2 e 3a4;
- xLargura1=x2-x1;
- yLargura1=y2-y1;
- xLargura2=x4-x3;
- yLargura2=y4-y3;
- }else {
- //Comrimento: 1a2 e 3a4;
- xComprimento1=x2-x1;
- yComprimento1=y2-y1;
- xComprimento2=x4-x3;
- yComprimento2=y4-y3;
- //Largura: 1a3 e 2a4;
- xLargura1=x3-x1;
- yLargura1=y3-y1;
- xLargura2=x4-x2;
- yLargura2=y4-y2;
- }
- //Verificação se lados opostos tem tamanhos iguais
- if(moduloVetor(xComprimento1,yComprimento1)==moduloVetor(xComprimento2,yComprimento2) && moduloVetor(xLargura1,yLargura1)==moduloVetor(xLargura2,yLargura2)) {
- //Verificar se lados são paralelos
- if((anguloEntreVetores(xComprimento1,yComprimento1,xComprimento2,yComprimento2)==0 || anguloEntreVetores(xComprimento1,yComprimento1,xComprimento2,yComprimento2)==180)
- &&(anguloEntreVetores(xLargura1,yLargura1,xLargura2,yLargura2)==0 || anguloEntreVetores(xLargura1,yLargura1,xLargura2,yLargura2)==180)) {
- paralelogramo=true;
- }
- }
- return paralelogramo;
- }
- /**
- * Rectagulo
- * Paralelogramo
- * Lados são perpendiculares
- *
- * @param x1 (double)
- * @param y1 (double)
- * @param x2 (double)
- * @param y2 (double)
- * @param x3 (double)
- * @param y3 (double)
- * @param x4 (double)
- * @param y4 (double)
- * @return (double) True se 4 pontos formam um rectangulo
- */
- private static boolean verificarRetangulo(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
- boolean retangulo=false;
- double xComprimento1=0;
- double yComprimento1=0;
- double xComprimento2=0;
- double yComprimento2=0;
- double xLargura1=0;
- double yLargura1=0;
- double xLargura2=0;
- double yLargura2=0;
- //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
- if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
- //Comrimento: 1a3 e 4a2;
- xComprimento1=x3-x1;
- yComprimento1=y3-y1;
- xComprimento2=x4-x2;
- yComprimento2=y4-y2;
- //Largura: 1a4 e 2a3;
- xLargura1=x4-x1;
- yLargura1=y4-y1;
- xLargura2=x3-x2;
- yLargura2=y3-y2;
- }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
- //Comrimento: 1a4 e 2a3;
- xComprimento1=x4-x1;
- yComprimento1=y4-y1;
- xComprimento2=x3-x2;
- yComprimento2=y3-y2;
- //Largura: 1a2 e 3a4;
- xLargura1=x2-x1;
- yLargura1=y2-y1;
- xLargura2=x4-x3;
- yLargura2=y4-y3;
- }else {
- //Comrimento: 1a2 e 3a4;
- xComprimento1=x2-x1;
- yComprimento1=y2-y1;
- xComprimento2=x4-x3;
- yComprimento2=y4-y3;
- //Largura: 1a3 e 2a4;
- xLargura1=x3-x1;
- yLargura1=y3-y1;
- xLargura2=x4-x2;
- yLargura2=y4-y2;
- }
- //Verificar se largura e comprimentos fazem 90º;
- if(produtoEscalarVetores(xComprimento1, yComprimento1, xLargura1, yLargura1)==0 &&
- produtoEscalarVetores(xComprimento1, yComprimento1, xLargura2, yLargura2)==0 &&
- produtoEscalarVetores(xComprimento2, yComprimento2, xLargura1, yLargura1)==0 &&
- produtoEscalarVetores(xComprimento2, yComprimento2, xLargura2, yLargura2)==0) {
- retangulo=true;
- }
- return retangulo;
- }
- /**
- * Lousangulo
- * Paralelogramo
- * Diagonais são perpendiculares
- *
- * @param x1 (double)
- * @param y1 (double)
- * @param x2 (double)
- * @param y2 (double)
- * @param x3 (double)
- * @param y3 (double)
- * @param x4 (double)
- * @param y4 (double)
- * @return (boolean) true se for um paralelogramo lousangulo
- */
- private static boolean verificarLosango(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
- boolean losango=false;
- double xDiagonal1=0;
- double yDiagonal1=0;
- double xDiagonal2=0;
- double yDiagonal2=0;
- //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
- if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
- //Diagonal: 1a2 e 3a4;
- xDiagonal1=x2-x1;
- yDiagonal1=y2-y1;
- xDiagonal2=x4-x3;
- yDiagonal2=y4-y3;
- }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
- //Diagonal: 1a3 e 2a4;
- xDiagonal1=x3-x1;
- yDiagonal1=y3-y1;
- xDiagonal2=x4-x2;
- yDiagonal2=y4-y2;
- }else {
- //Diagonal: 1a4 e 2a3;
- xDiagonal1=x4-x1;
- yDiagonal1=y4-y1;
- xDiagonal2=x3-x2;
- yDiagonal2=y3-y2;
- }
- //Verificar se diagonais são perpendiculares
- if(produtoEscalarVetores(xDiagonal1, yDiagonal1, xDiagonal2, yDiagonal2)==0) {
- losango=true;
- }
- return losango;
- }
- private static boolean verificarKite(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
- boolean kite=false;
- double xDiagonal1=0;
- double yDiagonal1=0;
- double xDiagonal2=0;
- double yDiagonal2=0;
- double xComprimento1=0;
- double yComprimento1=0;
- double xComprimento2=0;
- double yComprimento2=0;
- double xLargura1=0;
- double yLargura1=0;
- double xLargura2=0;
- double yLargura2=0;
- //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
- if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
- //Diagonal: 1a2 e 3a4;
- xDiagonal1=x2-x1;
- yDiagonal1=y2-y1;
- xDiagonal2=x4-x3;
- yDiagonal2=y4-y3;
- //Comrimento: 1a3 e 4a2;
- xComprimento1=x3-x1;
- yComprimento1=y3-y1;
- xComprimento2=x4-x2;
- yComprimento2=y4-y2;
- //Largura: 1a4 e 2a3;
- xLargura1=x4-x1;
- yLargura1=y4-y1;
- xLargura2=x3-x2;
- yLargura2=y3-y2;
- }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
- //Diagonal: 1a3 e 2a4;
- xDiagonal1=x3-x1;
- yDiagonal1=y3-y1;
- xDiagonal2=x4-x2;
- yDiagonal2=y4-y2;
- //Comrimento: 1a4 e 2a3;
- xComprimento1=x4-x1;
- yComprimento1=y4-y1;
- xComprimento2=x3-x2;
- yComprimento2=y3-y2;
- //Largura: 1a2 e 3a4;
- xLargura1=x2-x1;
- yLargura1=y2-y1;
- xLargura2=x4-x3;
- yLargura2=y4-y3;
- }else {
- //Diagonal: 1a4 e 2a3;
- xDiagonal1=x4-x1;
- yDiagonal1=y4-y1;
- xDiagonal2=x3-x2;
- yDiagonal2=y3-y2;
- //Comrimento: 1a2 e 3a4;
- xComprimento1=x2-x1;
- yComprimento1=y2-y1;
- xComprimento2=x4-x3;
- yComprimento2=y4-y3;
- //Largura: 1a3 e 2a4;
- xLargura1=x3-x1;
- yLargura1=y3-y1;
- xLargura2=x4-x2;
- yLargura2=y4-y2;
- }
- //Verificar ortogonalidade das diagonais
- if(produtoEscalarVetores(xDiagonal1, yDiagonal1, xDiagonal2, yDiagonal2)==0) {
- //Verificar dimensões tem de ter 2 lados ajacentes iguais
- if((moduloVetor(xComprimento1, yComprimento1)==moduloVetor(xLargura1, yLargura1)&&moduloVetor(xComprimento2, yComprimento2)==moduloVetor(xLargura2, yLargura2))
- ||(moduloVetor(xComprimento1, yComprimento1)==moduloVetor(xLargura2, yLargura2)&&moduloVetor(xComprimento2, yComprimento2)==moduloVetor(xLargura1, yLargura1))) {
- kite=true;
- }
- }
- return kite;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement