University stuff.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Oblig4.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. DrawThread t = new DrawThread(this);
  40. t.start();
  41. return t;
  42. }
  43. /*
  44. * The rest is utility methods which any implementation will use.
  45. */
  46. // a, b, and c will be used in the line equation.
  47. // It doesn't make sense to recalculate them all the time,
  48. // as they only depend on p1 and p2.
  49. int lineA(int p1, int p2) {
  50. return y[p1] - y[p2];
  51. }
  52. int lineB(int p1, int p2) {
  53. return x[p2] - x[p1];
  54. }
  55. int lineC(int p1, int p2) {
  56. return (y[p2] * x[p1]) - (y[p1] * x[p2]);
  57. }
  58. // From p1 to p2.
  59. // == 0: p3 is on the line.
  60. // > 0: p3 is left of the line.
  61. // < 0: p3 is right of the line.
  62. double lineEquation(int a, int b, int c, int p3) {
  63. return (a * x[p3]) + (b * y[p3]) + c;
  64. }
  65. // Distance between two points
  66. double pointDist(int p1, int p2) {
  67. int x1 = x[p1], y1 = y[p1];
  68. int x2 = x[p2], y2 = y[p2];
  69. int dx = x2 - x1;
  70. int dy = y2 - y1;
  71. return Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
  72. }
  73. // Distance between the line and p3
  74. double dist(double l, int a, int b) {
  75. return l / Math.sqrt((a * a) + (b * b));
  76. }
  77. // Create a list of points, in sorted order, on the line
  78. // given by (a, b, c, p1)
  79. IntList addPointsOnLine(IntList indexes, int a, int b, int c, int p1) {
  80. IntList l = new IntList();
  81. // Add points on the line between p1 and p2
  82. for (int i = 0; i < indexes.size(); ++i) {
  83. int idx = indexes.get(i);
  84. double line = lineEquation(a, b, c, idx);
  85. if (line == 0)
  86. l.add(idx);
  87. }
  88. // Calculate distances for sorting
  89. double[] dists = new double[l.size()];
  90. for (int i = 0; i < l.size(); ++i) {
  91. dists[i] = pointDist(p1, l.get(i));
  92. }
  93. // Sort points based on distance from p1
  94. // (Bubble sort, but it's usually really few elements)
  95. boolean sorted;
  96. do {
  97. sorted = true;
  98. // Loop through points, swap non-sorted ones
  99. for (int i = 1; i < l.size(); ++i) {
  100. double dist = dists[i];
  101. double prevDist = dists[i - 1];
  102. // Skip if already sorted
  103. if (prevDist <= dist)
  104. continue;
  105. sorted = false;
  106. // Swap indexes
  107. int tmpi = l.data[i];
  108. l.data[i] = l.data[i - 1];
  109. l.data[i - 1] = tmpi;
  110. // Swap distances
  111. double tmpd = dists[i];
  112. dists[i] = dists[i - 1];
  113. dists[i - 1] = tmpd;
  114. }
  115. } while (!sorted);
  116. return l;
  117. }
  118. }