Browse Source

sequential now works flawlessly

master
mortie 7 years ago
parent
commit
1d6d2163af
2 changed files with 81 additions and 11 deletions
  1. 10
    0
      inf2440/hw4/Oblig4.java
  2. 71
    11
      inf2440/hw4/Sequential.java

+ 10
- 0
inf2440/hw4/Oblig4.java View File

@@ -48,6 +48,16 @@ abstract class Oblig4 {
return (a * x[p3]) + (b * y[p3]) + c;
}

// Distance between two points
double pointDist(int p1, int p2) {
int x1 = x[p1], y1 = y[p1];
int x2 = x[p2], y2 = y[p2];

int dx = x2 - x1;
int dy = y2 - y1;
return Math.abs(Math.sqrt((dx * dx) + (dy * dy)));
}

// Distance between the line and p3
double dist(double l, int a, int b) {
return l / Math.sqrt((a * a) + (b * b));

+ 71
- 11
inf2440/hw4/Sequential.java View File

@@ -10,8 +10,12 @@ class Sequential extends Oblig4 {
MAX_X = x[maxX];
MAX_Y = y[maxY];

coHull = pointsRightOf(maxX, minX);
coHull.append(pointsRightOf(minX, maxX));
IntList indexes = new IntList(n);
for (int i = 0; i < n; ++i)
indexes.add(i);

coHull = pointsRightOf(maxX, minX, indexes);
coHull.append(pointsRightOf(minX, maxX, indexes));

for (int i = 0; i < coHull.size(); ++i) {
System.out.print(coHull.get(i)+" ");
@@ -40,7 +44,57 @@ class Sequential extends Oblig4 {
}
}

IntList pointsRightOf(int p1, int p2) {
IntList addPointsOnLine(IntList indexes, int a, int b, int c, int p1) {
IntList l = new IntList();

// Add points on the line between p1 and p2
for (int i = 0; i < indexes.size(); ++i) {
int idx = indexes.get(i);

double line = lineEquation(a, b, c, idx);
if (line == 0)
l.add(idx);
}

// Calculate distances for sorting
double[] dists = new double[l.size()];
for (int i = 0; i < l.size(); ++i) {
dists[i] = pointDist(p1, l.get(i));
}

// Sort points based on distance from p1
// (Bubble sort, but it's usually really few elements)
boolean sorted;
do {
sorted = true;

// Loop through points, swap non-sorted ones
for (int i = 1; i < l.size(); ++i) {
double dist = dists[i];
double prevDist = dists[i - 1];

// Skip if already sorted
if (prevDist <= dist)
continue;

sorted = false;

// Swap indexes
int tmpi = l.data[i];
l.data[i] = l.data[i - 1];
l.data[i - 1] = tmpi;

// Swap distances
double tmpd = dists[i];
dists[i] = dists[i - 1];
dists[i - 1] = tmpd;
}
} while (!sorted);

return l;
}

IntList pointsRightOf(int p1, int p2, IntList indexes) {
int p3 = -1;
double p3dist = 0;

@@ -48,29 +102,35 @@ class Sequential extends Oblig4 {
int b = lineB(p1, p2);
int c = lineC(p1, p2);

for (int i = 0; i < n; ++i) {
if (i == p1 || i == p2)
IntList nwIndexes = new IntList();

for (int i = 0; i < indexes.size(); ++i) {
int idx = indexes.get(i);
if (idx == p1 || idx == p2)
continue;

double line = lineEquation(a, b, c, i);
double line = lineEquation(a, b, c, idx);
if (line > 0)
continue;

nwIndexes.add(idx);

double dist = Math.abs(dist(line, a, b));
if (dist > p3dist) {
p3dist = dist;
p3 = i;
p3 = idx;
}
}

if (p3 == -1) {
IntList l = new IntList(2);
l.add(p1);
IntList l = addPointsOnLine(nwIndexes, a, b, c, p1);
l.add(p2);

return l;
}

IntList l = pointsRightOf(p1, p3);
l.append(pointsRightOf(p3, p2));
IntList l = pointsRightOf(p1, p3, nwIndexes);
l.append(pointsRightOf(p3, p2, nwIndexes));
return l;
}
}

Loading…
Cancel
Save