University stuff.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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