Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.33 KB | None | 0 0
  1. package Q1;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6. public class convexHull
  7. {
  8.     protected List<Position> domain;
  9.     protected int xMax;
  10.     protected int yMax;
  11.     private int points;
  12.     private Position a = new Position(true);
  13.     private Position b = new Position(false);
  14.    
  15.    
  16.     convexHull()
  17.     {
  18.         domain = new ArrayList<Position>();
  19.         xMax = 40;
  20.         yMax = 40;
  21.         points = 15;
  22.     }
  23.    
  24.     private void generatePoints(List<Position> domain)
  25.     {
  26.         for (int i = 0; i < points; i++){
  27.             domain.add(new Position(xMax, yMax));
  28.         }
  29.     }
  30.    
  31.     private void generateShape(List<Position> a, int xmin, int xmax, int ymin, int ymax)
  32.     {
  33.         for (int i = 0; i < points; i++)
  34.         {
  35.             a.add(new Position(xmin, xmax, ymin, ymax));
  36.             //a.get(i) = new Position(xmin, xmax, ymin, ymax);
  37.         }
  38.        
  39.     }
  40.    
  41.     private void printArr(List<Position> domain){
  42.         System.out.print("LIST:");
  43.         for (Position obj : domain)
  44.         {
  45.             System.out.print("("+obj.x + "," + obj.y + ")");
  46.         }
  47.         System.out.println("\n" + "Done");
  48.     }
  49.  
  50.     private int isLeft(Position a, Position b, Position c)
  51.     {
  52.         if ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) >= 0)
  53.         {
  54.             return 1;
  55.         }
  56.         else
  57.         {
  58.             return 0;
  59.         }
  60.     }
  61.    
  62.     public void sortByX(List<Position> domain){
  63.         Position temp;
  64.         for (int i = 0; i < domain.size()-1; i++){
  65.             for (int j = 0; j < domain.size()-1; j++){
  66.                 if (domain.get(j).x > domain.get(j+1).x){
  67.                     temp = domain.get(j);
  68.                     domain.set(j, domain.get(j+1));
  69.                     domain.set(j+1, temp);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.    
  75.     private List<Position> findConvex(List<Position> points){
  76.        
  77.         if (points.size() < 4){
  78.             formClockwise(points);
  79.             return points;
  80.         }
  81.        
  82.         else{
  83.             List<Position> L1 = points.subList(0, points.size()/2);
  84.             List<Position> L2 = points.subList(points.size()/2, points.size());
  85.             return merge(L1, L2);
  86.         }
  87.        
  88.     }
  89.    
  90.     private List<Position> merge(List<Position> L1, List<Position> L2){
  91.         L1 = findConvex(L1);
  92.         L2 = findConvex(L2);
  93.         boolean go = true;  
  94.         int p1 = highlowx(L1, true); //fins inner point for L1
  95.         int p2 = highlowx(L2, false); //find inner point for L2
  96.         int p3 = p2;
  97.         int p4 = p1;
  98.         int runlimit2 = L2.size()-1;
  99.         int runlimit1 = L1.size()-1;
  100.         bridge line = new bridge(p1,p2);
  101.         bridge line2 = new bridge(p1,p2);
  102.         while (0 <= p4 && go)
  103.         {
  104.             while (p3 <= runlimit2)
  105.             {
  106.                 if (isLeft(L2.get(line.i2),L1.get(line.i1),L2.get(p3)) == 0 ){ //ahhh
  107.                     line.sp2(p3);
  108.                 }
  109.                 p3++;
  110.             }
  111.             p4--;
  112.             if (p4 >= 0){
  113.                 if (isLeft(L1.get(line.i1),L2.get(line.i2), L1.get(p4)) == 1 ){
  114.                     line.sp1(p4);
  115.                     p3 = p2;
  116.                 }
  117.                 else{
  118.                     go = false;
  119.                 }
  120.             }
  121.         }
  122.         p3 = p2;
  123.         p4 = p1;
  124.         go = true;
  125.         while (0 <= p4 && p4 <= runlimit1 && go){
  126.             while (p3 <= runlimit2){
  127.                 if (isLeft(L2.get(line2.i2), L1.get(line2.i1), L2.get(p3)) == 1 ){ //ahhh
  128.                     line2.sp2(p3);
  129.                 }
  130.                 p3++;
  131.             }
  132.             p4++;
  133.             if (p4 >= 0 && p4 <= runlimit1){
  134.                 if (isLeft(L1.get(line2.i1),L2.get(line2.i2), L1.get(p4)) == 0 ){
  135.                     line2.sp1(p4);
  136.                     p3 = p2;
  137.                 }
  138.                 else{
  139.                     p3 = p2;
  140.                 }
  141.         }
  142.         }
  143.         List<Position> merged = new ArrayList<Position>();
  144.         int countback = line.i1;
  145.         int k = 0;
  146.         for (int i = 0; i <= countback; i++){
  147.             merged.add(L1.get(i));
  148.         }
  149.         k = 0;
  150.  
  151.         for (int i = line.i2; i <= line2.i2; i++){
  152.             k++;
  153.             merged.add(L2.get(i));
  154.         }
  155.         if (k == 0 && line.i2 > line2.i2){
  156.             merged.add(L2.get(line.i2));
  157.             merged.add(L2.get(line2.i2));
  158.         }
  159.         if (line2.i1 != 0){
  160.             for (int i = line2.i1; i <= L1.size()-1; i++){
  161.                 merged.add(L1.get(i));
  162.             }
  163.         }
  164.        
  165.         if (merged.size() == 4 && line2.i1 < line.i1){
  166.             merged.remove(0);
  167.             merged.add(L1.get(line2.i1));
  168.         }
  169.         return merged;
  170.     }
  171.        
  172.     private int highlowx(List<Position> points2, boolean b){
  173.         int bestindex = 0;
  174.         Iterator<Position> it = points2.iterator();
  175.         Position p;
  176.         int counter = 0;
  177.        
  178.         if (b){
  179.             while (it.hasNext()){
  180.                 p = it.next();
  181.                 if (p.x > points2.get(bestindex).x){
  182.                     bestindex = counter;
  183.                 }
  184.                 counter++;
  185.             }
  186.         }
  187.         else{
  188.             while (it.hasNext()){
  189.                 p = it.next();
  190.                 if (p.x < points2.get(bestindex).x){
  191.                     bestindex = counter;
  192.                 }
  193.                 counter++;
  194.             }
  195.         }
  196.         return bestindex;
  197.     }
  198.    
  199.     private List<Position> formClockwise(List<Position> points)
  200.     {
  201.         Position temp;
  202.         if (points.size() == 2){
  203.             return points;
  204.         }
  205.         else{
  206.             if (isLeft(points.get(1), points.get(0), points.get(2)) == 0){
  207.                 temp = points.get(1);
  208.                 points.set(1, points.get(2));
  209.                 points.set(2, temp);
  210.                 return points;
  211.             }
  212.         }
  213.         return points;
  214.     }
  215.    
  216.     public boolean endcheck()
  217.     {
  218.         int counter = 0;
  219.             for (Position p : domain)
  220.             {
  221.                 if (p.x == 40)
  222.                 {
  223.                     counter++;
  224.                     break;
  225.                 }
  226.         }
  227.         if (counter > 0)
  228.         {
  229.             return true;
  230.         }
  231.         else
  232.         {
  233.             return false;
  234.         }
  235.     }
  236.    
  237.     public boolean endcheck(List<Position> domain, int x1, int y1)
  238.     {
  239.         int counter = 0;
  240.             for (Position p : domain)
  241.             {
  242.                 if (p.x == x1 && p.y == y1){
  243.                     counter++;
  244.                     break;
  245.                 }
  246.         }
  247.         if (counter > 0){
  248.             return true;
  249.         }
  250.         else {
  251.             return false;
  252.         }
  253.     }
  254.    
  255.     public void execute(){
  256.         boolean check = false;
  257.         List<Position> domainCopy = new ArrayList<Position>();
  258.         while (check == false)
  259.         {
  260.             generatePoints(domain); //fills domain with points
  261.             domain.add(a); //adds starting point
  262.             domain.add(b); //adds end point
  263.             sortByX(domain); //sorts domain by x
  264.             domainCopy = domain;
  265.             domain = findConvex(domain);
  266.             check = endcheck();
  267.         }
  268.         System.out.println("Sorted by X:");
  269.         printArr(domainCopy);
  270.         System.out.println("Complete Hull:");
  271.         printArr(domain);
  272.     }
  273.  
  274.     public static void main(String [] args)
  275.     {
  276.         convexHull c = new convexHull();
  277.         c.execute(); //for question 1
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement