Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.65 KB | None | 0 0
  1. package ficha5;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5.  
  6. /**  
  7.  * Breve descrição do código  
  8.  *    
  9.  * @sid 2004  
  10.  * @aid 5.15
  11.  */
  12.  
  13. public class Ficha5Exercicio15_V1 {
  14.  
  15.     public static void main(String[] args) {
  16.         //begin_inputs  
  17.         double x1=0;
  18.         double y1=4;
  19.         double x2=3;
  20.         double y2=0;
  21.         double x3=6;
  22.         double y3=4;
  23.         double x4=3;
  24.         double y4=6;
  25.         //end_inputs  
  26.        
  27.         boolean paralelogramo=false; //faces opostas iguais e paralelas, diagonais intersentam-se a meio
  28.         boolean retangulo=false;    //Paralelogramo + angulos entre arestas =90º
  29.         boolean quadrado=false;     //Retangulo faces TODAS iguais
  30.         boolean losango=false;      //Paralelogramo com LADOS iguais e diagonais a 90º
  31.        
  32.         boolean kite=false;         //Tem diagonais a 90º, 2 faces iguais adjacentes
  33.        
  34.    
  35.         paralelogramo=verificarParalelogramo(x1,y1,x2,y2,x3,y3,x4,y4);
  36.         if(paralelogramo) {
  37.             retangulo=verificarRetangulo(x1,y1,x2,y2,x3,y3,x4,y4);
  38.             losango=verificarLosango(x1,y1,x2,y2,x3,y3,x4,y4);
  39.            
  40.             //Um quadrado é um rectangulo (lados perpendiculares e diagonais perpendiculares)
  41.             if(retangulo && losango) {
  42.                 quadrado=true;
  43.             }
  44.  
  45.         }else{
  46.             kite=verificarKite(x1,y1,x2,y2,x3,y3,x4,y4);
  47.             if(kite) {
  48.                 kite=true;
  49.             }
  50.         }
  51.        
  52.        
  53.         System.out.println("A forma gerada é:");
  54.         if(paralelogramo) {
  55.             System.out.println("->Paralelogramo");
  56.             if(retangulo) {
  57.                 System.out.println("->Retangulo");
  58.             }
  59.             if(losango) {
  60.                 System.out.println("->Losango");
  61.             }
  62.             if(quadrado) {
  63.                 System.out.println("->Quadrado");
  64.             }
  65.         }else if(kite) {
  66.             System.out.println("->Kite (papagaio)");
  67.         }else {
  68.             System.out.println("->Inregular");
  69.         }
  70.     }
  71.    
  72.     /**
  73.      *
  74.      * @param x1 (double)
  75.      * @param y1 (double)
  76.      * @param x2 (double)
  77.      * @param y2 (double)
  78.      * @return (double) distancia entre ponto 1 e 2
  79.      */
  80.     private static double distanciaPontos(double x1, double y1, double x2, double y2){
  81.         return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
  82.     }
  83.  
  84.     /**
  85.      *
  86.      * @param componteX (double)
  87.      * @param componteY (double)
  88.      * @return (double) modulo (distancia) do vetor
  89.      */
  90.     private static double moduloVetor(double componteX, double componteY){
  91.         return Math.sqrt(Math.pow(componteX, 2)+Math.pow(componteY, 2));
  92.     }
  93.    
  94.     /**
  95.      *
  96.      * @param xVetor1 (double)
  97.      * @param yVetor1 (double)
  98.      * @param xVetor2 (double)
  99.      * @param yVetor2 (double)
  100.      * @return (double) O produto escalar entre dois vetores. Se este produto=0 Os vetores são ortogonais
  101.      */
  102.     private static double produtoEscalarVetores(double xVetor1, double yVetor1, double xVetor2, double yVetor2) {
  103.         return  (xVetor1*xVetor2+yVetor1*yVetor2);
  104.     }
  105.    
  106.     /**
  107.      *
  108.      * @param xVetor1 (double)
  109.      * @param yVetor1 (double)
  110.      * @param xVetor2 (double)
  111.      * @param yVetor2 (double)
  112.      * @return (double) devolve o angulo entre o vetor 1 e 2 (em graus)
  113.      */
  114.     private static double anguloEntreVetores(double xVetor1, double yVetor1, double xVetor2, double yVetor2) {
  115.         double angulo= Math.toDegrees(Math.acos((produtoEscalarVetores(xVetor1,yVetor1,xVetor2,yVetor2))/(moduloVetor(xVetor1,yVetor1)*moduloVetor(xVetor2,yVetor2))));
  116.         return BigDecimal.valueOf(angulo).setScale(8, RoundingMode.HALF_UP).doubleValue();
  117.     }
  118.    
  119.     /**
  120.      * Paralelogramo:
  121.      *  Lados opostos mesmo comprimento
  122.      *  Lados opostos paralelos
  123.      *
  124.      * @param x1 (double)
  125.      * @param y1 (double)
  126.      * @param x2 (double)
  127.      * @param y2 (double)
  128.      * @param x3 (double)
  129.      * @param y3 (double)
  130.      * @param x4 (double)
  131.      * @param y4 (double)
  132.      * @return (boolean) True se 4 pontos formam um paralelogramo
  133.      */
  134.     private static boolean verificarParalelogramo(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
  135.         boolean paralelogramo=false;
  136.        
  137.         double xComprimento1=0;
  138.         double yComprimento1=0;
  139.         double xComprimento2=0;
  140.         double yComprimento2=0;
  141.        
  142.         double xLargura1=0;
  143.         double yLargura1=0;
  144.         double xLargura2=0;
  145.         double yLargura2=0;
  146.  
  147.         //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
  148.         if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
  149.             //Comrimento: 1a3 e 4a2;
  150.             xComprimento1=x3-x1;
  151.             yComprimento1=y3-y1;
  152.             xComprimento2=x4-x2;
  153.             yComprimento2=y4-y2;
  154.             //Largura: 1a4 e 2a3;
  155.             xLargura1=x4-x1;
  156.             yLargura1=y4-y1;
  157.             xLargura2=x3-x2;
  158.             yLargura2=y3-y2;
  159.            
  160.         }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
  161.             //Comrimento: 1a4 e 2a3;
  162.             xComprimento1=x4-x1;
  163.             yComprimento1=y4-y1;
  164.             xComprimento2=x3-x2;
  165.             yComprimento2=y3-y2;
  166.             //Largura: 1a2 e 3a4;
  167.             xLargura1=x2-x1;
  168.             yLargura1=y2-y1;
  169.             xLargura2=x4-x3;
  170.             yLargura2=y4-y3;
  171.         }else {
  172.             //Comrimento: 1a2 e 3a4;
  173.             xComprimento1=x2-x1;
  174.             yComprimento1=y2-y1;
  175.             xComprimento2=x4-x3;
  176.             yComprimento2=y4-y3;
  177.             //Largura: 1a3 e 2a4;
  178.             xLargura1=x3-x1;
  179.             yLargura1=y3-y1;
  180.             xLargura2=x4-x2;
  181.             yLargura2=y4-y2;
  182.         }
  183.        
  184.         //Verificação se lados opostos tem tamanhos iguais
  185.         if(moduloVetor(xComprimento1,yComprimento1)==moduloVetor(xComprimento2,yComprimento2) && moduloVetor(xLargura1,yLargura1)==moduloVetor(xLargura2,yLargura2)) {
  186.             //Verificar se lados são paralelos
  187.             if((anguloEntreVetores(xComprimento1,yComprimento1,xComprimento2,yComprimento2)==0 || anguloEntreVetores(xComprimento1,yComprimento1,xComprimento2,yComprimento2)==180)
  188.             &&(anguloEntreVetores(xLargura1,yLargura1,xLargura2,yLargura2)==0 || anguloEntreVetores(xLargura1,yLargura1,xLargura2,yLargura2)==180)) {
  189.                 paralelogramo=true;
  190.             }
  191.         }
  192.         return paralelogramo;
  193.     }
  194.     /**
  195.      * Rectagulo
  196.      *  Paralelogramo
  197.      *  Lados são perpendiculares
  198.      *
  199.      * @param x1 (double)
  200.      * @param y1 (double)
  201.      * @param x2 (double)
  202.      * @param y2 (double)
  203.      * @param x3 (double)
  204.      * @param y3 (double)
  205.      * @param x4 (double)
  206.      * @param y4 (double)
  207.      * @return (double) True se 4 pontos formam um rectangulo
  208.      */
  209.     private static boolean verificarRetangulo(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
  210.         boolean retangulo=false;
  211.  
  212.         double xComprimento1=0;
  213.         double yComprimento1=0;
  214.         double xComprimento2=0;
  215.         double yComprimento2=0;
  216.        
  217.         double xLargura1=0;
  218.         double yLargura1=0;
  219.         double xLargura2=0;
  220.         double yLargura2=0;
  221.  
  222.         //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
  223.         if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
  224.             //Comrimento: 1a3 e 4a2;
  225.             xComprimento1=x3-x1;
  226.             yComprimento1=y3-y1;
  227.             xComprimento2=x4-x2;
  228.             yComprimento2=y4-y2;
  229.             //Largura: 1a4 e 2a3;
  230.             xLargura1=x4-x1;
  231.             yLargura1=y4-y1;
  232.             xLargura2=x3-x2;
  233.             yLargura2=y3-y2;
  234.            
  235.         }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
  236.             //Comrimento: 1a4 e 2a3;
  237.             xComprimento1=x4-x1;
  238.             yComprimento1=y4-y1;
  239.             xComprimento2=x3-x2;
  240.             yComprimento2=y3-y2;
  241.             //Largura: 1a2 e 3a4;
  242.             xLargura1=x2-x1;
  243.             yLargura1=y2-y1;
  244.             xLargura2=x4-x3;
  245.             yLargura2=y4-y3;
  246.         }else {
  247.             //Comrimento: 1a2 e 3a4;
  248.             xComprimento1=x2-x1;
  249.             yComprimento1=y2-y1;
  250.             xComprimento2=x4-x3;
  251.             yComprimento2=y4-y3;
  252.             //Largura: 1a3 e 2a4;
  253.             xLargura1=x3-x1;
  254.             yLargura1=y3-y1;
  255.             xLargura2=x4-x2;
  256.             yLargura2=y4-y2;
  257.         }
  258.        
  259.         //Verificar se largura e comprimentos fazem 90º;
  260.         if(produtoEscalarVetores(xComprimento1, yComprimento1, xLargura1, yLargura1)==0 &&
  261.             produtoEscalarVetores(xComprimento1, yComprimento1, xLargura2, yLargura2)==0 &&
  262.             produtoEscalarVetores(xComprimento2, yComprimento2, xLargura1, yLargura1)==0 &&
  263.             produtoEscalarVetores(xComprimento2, yComprimento2, xLargura2, yLargura2)==0) {
  264.             retangulo=true;
  265.         }
  266.         return retangulo;
  267.     }
  268.     /**
  269.      * Lousangulo
  270.      *  Paralelogramo
  271.      *  Diagonais são perpendiculares
  272.      *
  273.      * @param x1 (double)
  274.      * @param y1 (double)
  275.      * @param x2 (double)
  276.      * @param y2 (double)
  277.      * @param x3 (double)
  278.      * @param y3 (double)
  279.      * @param x4 (double)
  280.      * @param y4 (double)
  281.      * @return (boolean) true se for um paralelogramo lousangulo
  282.      */
  283.     private static boolean verificarLosango(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
  284.         boolean losango=false;
  285.        
  286.         double xDiagonal1=0;
  287.         double yDiagonal1=0;
  288.         double xDiagonal2=0;
  289.         double yDiagonal2=0;
  290.  
  291.         //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
  292.         if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
  293.             //Diagonal: 1a2 e 3a4;
  294.             xDiagonal1=x2-x1;
  295.             yDiagonal1=y2-y1;
  296.             xDiagonal2=x4-x3;
  297.             yDiagonal2=y4-y3;
  298.         }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
  299.             //Diagonal: 1a3 e 2a4;
  300.             xDiagonal1=x3-x1;
  301.             yDiagonal1=y3-y1;
  302.             xDiagonal2=x4-x2;
  303.             yDiagonal2=y4-y2;
  304.         }else {
  305.             //Diagonal: 1a4 e 2a3;
  306.             xDiagonal1=x4-x1;
  307.             yDiagonal1=y4-y1;
  308.             xDiagonal2=x3-x2;
  309.             yDiagonal2=y3-y2;
  310.         }
  311.        
  312.         //Verificar se diagonais são perpendiculares
  313.         if(produtoEscalarVetores(xDiagonal1, yDiagonal1, xDiagonal2, yDiagonal2)==0) {
  314.             losango=true;
  315.         }
  316.         return losango;
  317.     }
  318.    
  319.     private static boolean verificarKite(double x1, double y1, double x2, double y2,double x3, double y3, double x4, double y4) {
  320.         boolean kite=false;
  321.        
  322.         double xDiagonal1=0;
  323.         double yDiagonal1=0;
  324.         double xDiagonal2=0;
  325.         double yDiagonal2=0;
  326.        
  327.         double xComprimento1=0;
  328.         double yComprimento1=0;
  329.         double xComprimento2=0;
  330.         double yComprimento2=0;
  331.        
  332.         double xLargura1=0;
  333.         double yLargura1=0;
  334.         double xLargura2=0;
  335.         double yLargura2=0;
  336.        
  337.         //Determinar uma possiveis diagonais (Diagonias devem ter as maiores distancias)
  338.         if(distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x3,y3)&&distanciaPontos(x1,y1,x2,y2)>distanciaPontos(x1,y1,x4,y4) ) {
  339.             //Diagonal: 1a2 e 3a4;
  340.             xDiagonal1=x2-x1;
  341.             yDiagonal1=y2-y1;
  342.             xDiagonal2=x4-x3;
  343.             yDiagonal2=y4-y3;
  344.             //Comrimento: 1a3 e 4a2;
  345.             xComprimento1=x3-x1;
  346.             yComprimento1=y3-y1;
  347.             xComprimento2=x4-x2;
  348.             yComprimento2=y4-y2;
  349.             //Largura: 1a4 e 2a3;
  350.             xLargura1=x4-x1;
  351.             yLargura1=y4-y1;
  352.             xLargura2=x3-x2;
  353.             yLargura2=y3-y2;
  354.         }else if(distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x2,y2)&&distanciaPontos(x1,y1,x3,y3)>distanciaPontos(x1,y1,x4,y4)) {
  355.             //Diagonal: 1a3 e 2a4;
  356.             xDiagonal1=x3-x1;
  357.             yDiagonal1=y3-y1;
  358.             xDiagonal2=x4-x2;
  359.             yDiagonal2=y4-y2;
  360.             //Comrimento: 1a4 e 2a3;
  361.             xComprimento1=x4-x1;
  362.             yComprimento1=y4-y1;
  363.             xComprimento2=x3-x2;
  364.             yComprimento2=y3-y2;
  365.             //Largura: 1a2 e 3a4;
  366.             xLargura1=x2-x1;
  367.             yLargura1=y2-y1;
  368.             xLargura2=x4-x3;
  369.             yLargura2=y4-y3;
  370.         }else {
  371.             //Diagonal: 1a4 e 2a3;
  372.             xDiagonal1=x4-x1;
  373.             yDiagonal1=y4-y1;
  374.             xDiagonal2=x3-x2;
  375.             yDiagonal2=y3-y2;
  376.             //Comrimento: 1a2 e 3a4;
  377.             xComprimento1=x2-x1;
  378.             yComprimento1=y2-y1;
  379.             xComprimento2=x4-x3;
  380.             yComprimento2=y4-y3;
  381.             //Largura: 1a3 e 2a4;
  382.             xLargura1=x3-x1;
  383.             yLargura1=y3-y1;
  384.             xLargura2=x4-x2;
  385.             yLargura2=y4-y2;
  386.         }
  387.        
  388.         //Verificar ortogonalidade das diagonais
  389.         if(produtoEscalarVetores(xDiagonal1, yDiagonal1, xDiagonal2, yDiagonal2)==0) {
  390.             //Verificar dimensões tem de ter 2 lados ajacentes iguais
  391.             if((moduloVetor(xComprimento1, yComprimento1)==moduloVetor(xLargura1, yLargura1)&&moduloVetor(xComprimento2, yComprimento2)==moduloVetor(xLargura2, yLargura2))
  392.                 ||(moduloVetor(xComprimento1, yComprimento1)==moduloVetor(xLargura2, yLargura2)&&moduloVetor(xComprimento2, yComprimento2)==moduloVetor(xLargura1, yLargura1))) {
  393.                 kite=true;
  394.             }
  395.         }
  396.         return kite;
  397.     }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement