Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Stack;
  5. import javax.ejb.Stateless;
  6.  
  7. /**
  8.  * @author Andrzej
  9.  */
  10.  
  11. @Stateless
  12. public class Solid implements ISolidRemote{
  13.  
  14.     private ArrayList<Point> listaPunktow;
  15.     private Stack<Point> stosPunktow;
  16.     private Point pierwszyPunkt;
  17.  
  18.     /**
  19.      * Oblicza powierzchnie boczna
  20.      *
  21.      * @param points lista punktow
  22.      * @return powierzchnia boczna
  23.      */
  24.     @Override
  25.     public String getSurfaceArea(ArrayList<Point> points) {
  26.         this.listaPunktow = points;
  27.         this.stosPunktow = new Stack<Point>();
  28.         //double h = getHeight();
  29.         pierwszyPunkt = findFirstPoint();
  30.         points.remove(pierwszyPunkt);
  31.         zmianaKolej();
  32.         graham();
  33.         double result = calculateConvexArea();
  34.         return String.format("%.5f", result);
  35.     }
  36.    
  37.      private double calculateConvexArea() {
  38.         double areaResult = 0.0;
  39.         int stackSize = stosPunktow.size();
  40.         int j = stackSize - 1;
  41.        
  42.         for (int i = 0; i < stackSize; i++) {
  43.             Point p1 = stosPunktow.get(i);
  44.             Point p2 = stosPunktow.get((j));
  45.              areaResult += (p1.getX() + p2.getX()) * (p1.getY() - p2.getY());  
  46.              j = i;
  47.         }
  48.         areaResult = Math.abs(areaResult / 2);
  49.  
  50.         return areaResult;
  51.     }
  52.     /**
  53.      * @return punkt poczatkowy o minimalnym y jezeli istnieje wiele punktow o
  54.      * minimalnym y wybierz taki, ktory ma minimalna wartosc x
  55.      */
  56.     private Point findFirstPoint() {
  57.         Point minimum = listaPunktow.get(0);
  58.         for (int j = 1; j < listaPunktow.size(); j++) {
  59.             minimum = getMinimum(minimum, listaPunktow.get(j));
  60.         }
  61.         return minimum;
  62.     }
  63.  
  64.     /**
  65.      * @param a pierwszy punkt
  66.      * @param b drugi punkt
  67.      * @return punkt o minimalnym y jezeli istnieje wiele punktow o minimalnym y
  68.      * wybierz taki, ktory ma minimalna wartosc x
  69.      */
  70.     private Point getMinimum(Point Aa, Point Ba) {
  71.         if (Aa.getY() < Ba.getY()) {
  72.             return Aa;
  73.         }
  74.         if (Aa.getY() > Ba.getY()) {
  75.             return Ba;
  76.         }
  77.         if (Aa.getX() < Ba.getX()) {
  78.             return Aa;
  79.         } else {
  80.             return Ba;
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * sortowanie punktow wzgledem kata
  86.      */
  87.     private void zmianaKolej() {
  88.         for (Point punkt : listaPunktow) {
  89.             double x = punkt.getX() - pierwszyPunkt.getX();
  90.             double z = punkt.getY() - pierwszyPunkt.getY();
  91.  
  92.             if (x < 0.0) {
  93.                 punkt.setKont(2 - (z / (Math.abs(x) + Math.abs(z))));
  94.             } else {
  95.                 punkt.setKont(z / (Math.abs(x) + Math.abs(z)));
  96.             }
  97.         }
  98.  
  99.         Collections.sort(listaPunktow);
  100.     }
  101.  
  102.     /**
  103.      * algorytm Grahama do wyznaczania otoczki wypuklej
  104.      */
  105.     private void graham() {
  106.         stosPunktow.push(pierwszyPunkt);
  107.         stosPunktow.push(listaPunktow.get(0));
  108.         stosPunktow.push(listaPunktow.get(1));
  109.  
  110.         for (int i = 2; i < listaPunktow.size(); i++) {
  111.             while (isRight(
  112.                     stosPunktow.get(stosPunktow.size() - 2),
  113.                     stosPunktow.get(stosPunktow.size() - 1),
  114.                     listaPunktow.get(i)
  115.             )) {
  116.                 stosPunktow.pop();
  117.             }
  118.             stosPunktow.push(listaPunktow.get(i));
  119.         }
  120.     }
  121.  
  122.     /**
  123.      * @param a pierwszy punkt
  124.      * @param b drugi punkt
  125.      * @param c trzeci punkt
  126.      * @return true - jezeli skret w prawo
  127.      */
  128.     private boolean isRight(Point Aa, Point Ba, Point Ca) {
  129.         double dir = Aa.getX() * Ba.getY()
  130.                 + Ba.getX() * Ca.getY()
  131.                 + Ca.getX() * Aa.getY()
  132.                 - Ca.getX() * Ba.getY()
  133.                 - Aa.getX() * Ca.getY()
  134.                 - Ba.getX() * Aa.getY();
  135.  
  136.         return dir < 0.0;
  137.     }
  138.    
  139.     /*private double calculateSurArea() {
  140.         double res = 0.0;
  141.         for (int j = 0; j < stosPunktow.size(); j++) {
  142.             Point aPunkt = stosPunktow.get(j);
  143.             Point bPunkt = stosPunktow.get((j + 1) % stosPunktow.size());
  144.  
  145.             double x = aPunkt.getX() - bPunkt.getX();
  146.             double z = aPunkt.getZ() - bPunkt.getZ();
  147.             res += x * z;
  148.         }
  149.         return res;
  150.     }*/
  151.  
  152.    
  153.  
  154.    
  155.  
  156.     /**
  157.      * Klasa wewnetrzna opisujaca punkt
  158.      */
  159.     public static class Point implements Comparable<Point> {
  160.  
  161.         private double Xa;
  162.         private double Ya;
  163.         private double Za;
  164.         private double kont;
  165.  
  166.         public Point(double x, double y, double z) {
  167.             this.Xa = x;
  168.             this.Ya = y;
  169.             this.Za = z;
  170.         }
  171.  
  172.         public double getX() {
  173.             return Xa;
  174.         }
  175.  
  176.         public double getY() {
  177.             return Ya;
  178.         }
  179.  
  180.         public double getZ() {
  181.             return Za;
  182.         }
  183.  
  184.         public double getKont() {
  185.             return kont;
  186.         }
  187.  
  188.         private void setKont(double kont) {
  189.             this.kont = kont;
  190.         }
  191.  
  192.         @Override
  193.         public int compareTo(Point xo) {
  194.             if (this.kont < xo.getKont()) {
  195.                 return -1;
  196.             } else if (this.kont > xo.getKont()) {
  197.                 return 1;
  198.             }
  199.             return 0;
  200.         }
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement