| @@ -6,8 +6,19 @@ class Main { | |||
| } | |||
| NPunkter17 points = new NPunkter17(Integer.parseInt(args[0])); | |||
| Sequential s = new Sequential(points); | |||
| s.solve(); | |||
| s.draw(); | |||
| Oblig4 seq = new Sequential(points); | |||
| Oblig4 par = new Parallel(points); | |||
| Timer ts = new Timer().start(); | |||
| seq.solve(); | |||
| ts.end(); | |||
| System.out.println("Sequential: "+ts.prettyTime()); | |||
| Timer tp = new Timer().start(); | |||
| par.solve(); | |||
| tp.end(); | |||
| System.out.println("Parallel: "+tp.prettySpeedup(ts)); | |||
| seq.draw(); | |||
| } | |||
| } | |||
| @@ -29,4 +29,27 @@ abstract class Oblig4 { | |||
| TegnUt t = new TegnUt(this, coHull, name); | |||
| t.setVisible(true); | |||
| } | |||
| int lineA(int p1, int p2) { | |||
| return y[p1] - y[p2]; | |||
| } | |||
| int lineB(int p1, int p2) { | |||
| return x[p2] - x[p1]; | |||
| } | |||
| int lineC(int p1, int p2) { | |||
| return (y[p2] * x[p1]) - (y[p1] * x[p2]); | |||
| } | |||
| // From p1 to p2. | |||
| // == 0: p3 is on the line. | |||
| // > 0: p3 is left of the line. | |||
| // < 0: p3 is right of the line. | |||
| double lineEquation(int a, int b, int c, int p3) { | |||
| return (a * x[p3]) + (b * y[p3]) + c; | |||
| } | |||
| // Distance between the line and p3 | |||
| double dist(double l, int a, int b) { | |||
| return l / Math.sqrt((a * a) + (b * b)); | |||
| } | |||
| } | |||
| @@ -4,7 +4,7 @@ class Parallel extends Oblig4 { | |||
| name = "Parallel"; | |||
| } | |||
| @Override | |||
| void solve() { | |||
| System.out.println("Solving in parallel"); | |||
| } | |||
| } | |||
| @@ -6,18 +6,20 @@ class Sequential extends Oblig4 { | |||
| @Override | |||
| void solve() { | |||
| System.out.println("Solving sequentially"); | |||
| findMinMaxX(); | |||
| findMinMaxXY(); | |||
| MAX_X = x[maxX]; | |||
| MAX_Y = y[maxY]; | |||
| coHull.add(minX); | |||
| coHull.add(maxY); | |||
| coHull.add(maxX); | |||
| coHull.add(minY); | |||
| coHull = pointsRightOf(maxX, minX); | |||
| coHull.append(pointsRightOf(minX, maxX)); | |||
| for (int i = 0; i < coHull.size(); ++i) { | |||
| System.out.print(coHull.get(i)+" "); | |||
| } | |||
| System.out.println(""); | |||
| } | |||
| void findMinMaxX() { | |||
| void findMinMaxXY() { | |||
| minX = 0; | |||
| maxX = 0; | |||
| minY = 0; | |||
| @@ -37,4 +39,38 @@ class Sequential extends Oblig4 { | |||
| maxY = i; | |||
| } | |||
| } | |||
| IntList pointsRightOf(int p1, int p2) { | |||
| int p3 = -1; | |||
| double p3dist = 0; | |||
| int a = lineA(p1, p2); | |||
| int b = lineB(p1, p2); | |||
| int c = lineC(p1, p2); | |||
| for (int i = 0; i < n; ++i) { | |||
| if (i == p1 || i == p2) | |||
| continue; | |||
| double line = lineEquation(a, b, c, i); | |||
| if (line > 0) | |||
| continue; | |||
| double dist = Math.abs(dist(line, a, b)); | |||
| if (dist > p3dist) { | |||
| p3dist = dist; | |||
| p3 = i; | |||
| } | |||
| } | |||
| if (p3 == -1) { | |||
| IntList l = new IntList(2); | |||
| l.add(p1); | |||
| return l; | |||
| } | |||
| IntList l = pointsRightOf(p1, p3); | |||
| l.append(pointsRightOf(p3, p2)); | |||
| return l; | |||
| } | |||
| } | |||
| @@ -0,0 +1,60 @@ | |||
| import java.util.Arrays; | |||
| class Timer implements Comparable<Timer> { | |||
| public long time = 0; | |||
| private long start = 0; | |||
| Timer() {} | |||
| Timer(long time) { | |||
| this.time = time; | |||
| } | |||
| public Timer start() { | |||
| start = System.nanoTime(); | |||
| return this; | |||
| } | |||
| public Timer end() { | |||
| time = System.nanoTime() - start; | |||
| return this; | |||
| } | |||
| public String prettyTime() { | |||
| if (time < 1000) | |||
| return String.format("%.2fns", time / 1f); | |||
| else if (time < 1000000) | |||
| return String.format("%.2fμs", time / 1000f); | |||
| else if (time < 1000000000) | |||
| return String.format("%.2fms", time / 1000000f); | |||
| else | |||
| return String.format("%.2fs", time / 1000000000f); | |||
| } | |||
| public String prettySpeedup(Timer base) { | |||
| double speedup = (double)base.time / (double)time; | |||
| return String.format("%s (%.2fx speedup)", | |||
| prettyTime(), speedup); | |||
| } | |||
| public static Timer median(Timer[] timers) { | |||
| Arrays.sort(timers); | |||
| Timer med = new Timer(); | |||
| int mid = timers.length / 2; | |||
| if (timers.length % 2 == 0) | |||
| med.time = (timers[mid].time + timers[mid+1].time) / 2; | |||
| else | |||
| med.time = timers[mid].time; | |||
| return med; | |||
| } | |||
| public int compareTo(Timer t) { | |||
| if (this.time < t.time) | |||
| return -1; | |||
| else if (this.time > t.time) | |||
| return 1; | |||
| else | |||
| return 0; | |||
| } | |||
| } | |||