University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Oblig4.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. abstract class Oblig4 {
  2. // Required for TegnUt
  3. int x[];
  4. int y[];
  5. int n;
  6. int MAX_X;
  7. int MAX_Y;
  8. IntList coHull;
  9. NPunkter17 points;
  10. String name = "Oblig4";
  11. Oblig4(int[] x, int[] y, int n) {
  12. this.x = x;
  13. this.y = y;
  14. this.n = n;
  15. coHull = new IntList();
  16. }
  17. // This method should be overwritten by each implementation.
  18. // It's responsible for filling in MAX_X, MAX_Y, and coHull
  19. abstract void solve();
  20. // We don't want to draw on the main thread.
  21. class DrawThread extends Thread {
  22. Oblig4 d;
  23. DrawThread(Oblig4 d) {
  24. this.d = d;
  25. }
  26. public void run() {
  27. try {
  28. TegnUt t = new TegnUt(d, coHull, name);
  29. t.setVisible(true);
  30. } catch (Exception ex) {
  31. System.out.println(name+": Couldn't draw window.");
  32. }
  33. }
  34. }
  35. // Draw points using TegnUt.
  36. // Requires x, y, n, MAX_X, MAX_Y, and coHull
  37. // to be filled out.
  38. Thread draw() {
  39. if (n > 20000) {
  40. System.out.println(name+": Too many dots to draw.");
  41. return null;
  42. }
  43. System.out.println(name+" coHull: ");
  44. for (int i = 0; i < coHull.size(); ++i) {
  45. if (i == 0)
  46. System.out.print(coHull.get(i));
  47. else
  48. System.out.print(", "+coHull.get(i));
  49. }
  50. System.out.println("");
  51. DrawThread t = new DrawThread(this);
  52. t.start();
  53. return t;
  54. }
  55. /*
  56. * The rest is utility methods which any implementation will use.
  57. */
  58. // a, b, and c will be used in the line equation.
  59. // It doesn't make sense to recalculate them all the time,
  60. // as they only depend on p1 and p2.
  61. int lineA(int p1, int p2) {
  62. return y[p1] - y[p2];
  63. }
  64. int lineB(int p1, int p2) {
  65. return x[p2] - x[p1];
  66. }
  67. int lineC(int p1, int p2) {
  68. return (y[p2] * x[p1]) - (y[p1] * x[p2]);
  69. }
  70. // From p1 to p2.
  71. // == 0: p3 is on the line.
  72. // > 0: p3 is left of the line.
  73. // < 0: p3 is right of the line.
  74. double lineEquation(int a, int b, int c, int p3) {
  75. return (a * x[p3]) + (b * y[p3]) + c;
  76. }
  77. // Distance between two points
  78. double pointDist(int p1, int p2) {
  79. int x1 = x[p1], y1 = y[p1];
  80. int x2 = x[p2], y2 = y[p2];
  81. int dx = x2 - x1;
  82. int dy = y2 - y1;
  83. return Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
  84. }
  85. // Distance between the line and p3
  86. double dist(double l, int a, int b) {
  87. return l / Math.sqrt((a * a) + (b * b));
  88. }
  89. // Create a list of points, in sorted order, on the line
  90. // given by (a, b, c, p1)
  91. IntList addPointsOnLine(IntList indexes, int a, int b, int c, int p1) {
  92. IntList l = new IntList();
  93. // Add points on the line between p1 and p2
  94. for (int i = 0; i < indexes.size(); ++i) {
  95. int idx = indexes.get(i);
  96. double line = lineEquation(a, b, c, idx);
  97. if (line == 0)
  98. l.add(idx);
  99. }
  100. // Calculate distances for sorting
  101. double[] dists = new double[l.size()];
  102. for (int i = 0; i < l.size(); ++i) {
  103. dists[i] = pointDist(p1, l.get(i));
  104. }
  105. // Sort points based on distance from p1
  106. // (Bubble sort, but it's usually really few elements)
  107. boolean sorted;
  108. do {
  109. sorted = true;
  110. // Loop through points, swap non-sorted ones
  111. for (int i = 1; i < l.size(); ++i) {
  112. double dist = dists[i];
  113. double prevDist = dists[i - 1];
  114. // Skip if already sorted
  115. if (prevDist <= dist)
  116. continue;
  117. sorted = false;
  118. // Swap indexes
  119. int tmpi = l.data[i];
  120. l.data[i] = l.data[i - 1];
  121. l.data[i - 1] = tmpi;
  122. // Swap distances
  123. double tmpd = dists[i];
  124. dists[i] = dists[i - 1];
  125. dists[i - 1] = tmpd;
  126. }
  127. } while (!sorted);
  128. return l;
  129. }
  130. }