void Heightfield::buildStruct(void) { hsmap = new HighStruct[W*H]; /* * to build the first level, consider that, when we're inside some square * and consider the highest possible elevation around with R = 1. This is the * 5x5 square around that point (we need 3x3 to cover all possible squares * one can reach with radius 1, and then also extend that further with 1 to * account for the fact that the heightfield is not composed of solid blocks, but * rather a combination of two triangles. The triangles may rise up quite higher, * if the neighbouring points of the heightfield are higher; to account for them, * we include an extra layer around the 3x3, thus yielding a 5x5. */ for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) { float maxh = getHeight(x, y); for (int dy = -2; dy <= 2; dy++) for (int dx = -2; dx <= 2; dx++) maxh = max(maxh, getHeight(x + dx, y + dy)); hsmap[y * W + x].h[0] = maxh; } /* * Here's our structure-building algorithm * Consider the record for (x, y), for various values of k: * for k = 0, we have the highest texel in a 5x5 square, cenrtered in (x, y) (as explained above) * for k = 1, we have the highest texel for a square 7x7 centered in (x, y). We can get that by combining four 5x5 instances * (level 0) at offsets (-1, -1), (-1, 1), (1, -1), (1, 1). There are overlapping texels, but since we only need * the maximum, they don't really matter * for k = 2, we have the highest texel for 11x11 square at (x, y). Find that by integrating four 7x7 instances * at offsets (-2, -2), (-2, 2), (2, -2) and (2, 2). * for k = 3, the squares are 19x19 * for k = 4, the squares are 35x35 * etc, etc... * * generally, for any k, we have the highest texel for a (2^(k+1)+3)x(2^(k+1)+3) square (radius 2^k). To compute it, we get four * squares of (k-1) size (i.e. (2^k + 3)x(2^k + 3)) and compute the max of their maxes. The offsets from (x, y) are 2^(k-1). */ // maxK is the number of levels: maxK ~= log2(N) maxK = (int) (ceil(log((double) max(W, H)) / log(2.0))) + 1; // +1 to get the diagonal as well for (int k = 1; k < maxK; k++) { for (int y = 0; y < H; y++) for (int x = 0; x < W; x++) { int offset = 1 << (k - 1); float up_left = getHighest(x - offset, y - offset, k - 1); float up_right = getHighest(x + offset, y - offset, k - 1); float down_left = getHighest(x - offset, y + offset, k - 1); float down_right = getHighest(x + offset, y + offset, k - 1); hsmap[y * W + x].h[k] = max(max(up_left, up_right), max(down_left, down_right)); } } }
void GLWidget::rangeSearch(TwoDTree::Node *p, RangeQuery &rq, std::vector<QPointF> &includingPoints) { if (p != nullptr) { double l; double r; double coord; if (p->isVertical) { l = qMin(rq.p2.y(), rq.p1.y()); r = qMax(rq.p2.y(), rq.p1.y()); coord = p->value.y(); } else { l = qMin(rq.p2.x(), rq.p1.x()); r = qMax(rq.p2.x(), rq.p1.x()); coord = p->value.x(); } if (p->value.y() <= getHighest(rq) && p->value.y() >= getLowest(rq) && p->value.x() >= getLeftMost(rq) && p->value.x() <= getRightMost(rq)) { includingPoints.push_back(p->value); } if (l < coord) { rangeSearch(p->left, rq, includingPoints); } if (r > coord) { rangeSearch(p->right, rq, includingPoints); } } }
int main() { do { string fileName = getFileName(); vector<int> numberArray; initializeArray(fileName, numberArray); cout << "The lowest number is: " << getLowest(numberArray) << endl; cout << "The highest number is: " << getHighest(numberArray) << endl; cout << "The sum of all these numbers is: " << getSum(numberArray) << endl; cout << "The average of all these numbers is: " << setprecision(10) << fixed << getAverage(numberArray) << endl; } while (!wantToExit()); return 0; }
void get_best_move(int *result, int idRow, int spaces, int Xcrd, int Ycrd, int NumOfRows, int NumOfCols, int FishArray[NumOfRows][NumOfCols], int AllPengs, int PengArray[AllPengs][3]) { int i, j; int best_4_moves[4][3]; // spaces, direction, fish for(i = 0; i<4; i++) for(j=1; j <= 3; j++) { best_4_moves[i][0] = j; best_4_moves[i][1] = getHighestDir(j, Xcrd, Ycrd, NumOfRows, NumOfCols, FishArray, AllPengs, PengArray); best_4_moves[i][2] = getHighest(j, Xcrd, Ycrd, NumOfRows, NumOfCols, FishArray, AllPengs, PengArray); } result[0] = best_4_moves[0][0]; result[1] = best_4_moves[0][1]; for(i = 1; i<4; i++) { if(CheckMove(idRow, result[1], result[1], NumOfCols, FishArray, AllPengs, PengArray) == 0) { result[0] = best_4_moves[i][0]; result[1] = best_4_moves[i][1]; } } }
vector<int> CSVop::getHighestrows(vector<int> rows, int col, double precision, bool absolute) { double maxval; maxval = getHighest(col, rows); return getrowswtol(rows, col, maxval, precision, absolute); }
bool Heightfield::intersect(const Ray& ray, IntersectionInfo& info) const { double dist = bbox.closestIntersection(ray); if (dist >= INF) return false; // position p (the current point we consider) at the edge of the BBox // and then slowly move it along the heightfield Vector p = ray.start + ray.dir * (dist + 1e-6); // step briefly inside the bbox Vector step = ray.dir; double mx = 1.0 / ray.dir.x; // mx = how much to go along ray.dir until the unit distance along X is traversed double mz = 1.0 / ray.dir.z; // same as mx, for Z while (bbox.inside(p)) { int x0 = (int) floor(p.x); int z0 = (int) floor(p.z); if (x0 < 0 || x0 >= W || z0 < 0 || z0 >= H) break; // if outside the [0..W)x[0..H) rect, get out // try to use the fast structure: if (useOptimization) { int k = 0; // explanation (see below): A is the highest peak around (x0, z0) at radius 2^k // B is the minimum of the starting ray height, and the final height after going // 2^k units along the ray. I.e., A is the highest the terrain may go up to; // B is the lowest the ray can go down to. If A < B, then no intersection is possible, // and it is safe to skip 2^k along the ray, and we may even opt to skip more. // while [ A ] < [ B ] while (k < maxK && getHighest(x0, z0, k) < min(p.y, p.y + ray.dir.y * (1 << k))) k++; k--; // decrease k, because at k, the test failed; k - 1 is OK if (k > 0) { p += ray.dir * (1 << k); continue; } // if the test failed at k = 0, we're too close to the terrain - check for intersection: } // calculate how much we need to go along ray.dir until we hit the next X voxel boundary: double lx = ray.dir.x > 0 ? (ceil(p.x) - p.x) * mx : (floor(p.x) - p.x) * mx; // same as lx, for the Z direction: double lz = ray.dir.z > 0 ? (ceil(p.z) - p.z) * mz : (floor(p.z) - p.z) * mz; // advance p along ray.dir until we hit the next X or Z gridline // also, go a little more than that, to assure we're firmly inside the next voxel: Vector p_next = p + step * (min(lx, lz) + 1e-6); // "p" is position before advancement; p_next is after we take a single step. // if any of those are below the height of the nearest four voxels of the heightfield, // we need to test the current voxel for intersection: if (min(p.y, p_next.y) < maxH[z0 * W + x0]) { double l1, l2, closestDist = INF; // form ABCD - the four corners of the current voxel, whose heights are taken from the heightmap // then form triangles ABD and BCD and try to intersect the ray with each of them: Vector A = Vector(x0, getHeight(x0, z0), z0); Vector B = Vector(x0 + 1, getHeight(x0 + 1, z0), z0); Vector C = Vector(x0 + 1, getHeight(x0 + 1, z0 + 1), z0 + 1); Vector D = Vector(x0, getHeight(x0, z0 + 1), z0 + 1); Triangle T1, T2; T1.AB = B - A; T1.AC = D - A; T2.AB = C - B; T2.AC = D - B; T1.ABcrossAC = T1.AB ^ T1.AC; T2.ABcrossAC = T2.AB ^ T2.AC; if (intersectTriangleFast(ray, A, T1, l1, l2, closestDist) || intersectTriangleFast(ray, B, T2, l1, l2, closestDist)) { // intersection found: ray hits either triangle ABD or BCD. Which one exactly isn't // important, because we calculate the normals by bilinear interpolation of the // precalculated normals at the four corners: info.distance = closestDist; info.ip = ray.start + ray.dir * closestDist; if (!(ray.flags & RF_SHADOW)) { info.norm = getNormal((float) info.ip.x, (float) info.ip.z); info.u = info.ip.x / W; info.v = info.ip.z / H; info.g = this; } return true; } } p = p_next; } return false; }