void normalCase(Point start, Point end, double slope, Points &points) const { double inverseSlope = 1/slope; double b = start.y - slope*start.x; bool isLeftRight = start.x < end.x; if (!isLeftRight) { Point tmp = start; start = end; end = tmp; } double xStart = ceil(start.x); double xEnd = end.x; for (double x = xStart; x < xEnd; x += step_) points.push_back(Point(x, slope*x + b)); bool isBottomTop = start.y < end.y; if (!isBottomTop) { Point tmp = start; start = end; end = tmp; } double yStart = ceil(start.y); double yEnd = end.y; for (double y = yStart; y < yEnd; y += step_) points.push_back(Point((y - b)*inverseSlope, y)); if (isLeftRight) std::sort(points.begin(), points.end(), sort_x()); else std::sort(points.begin(), points.end(), sort_reverse_x()); }
void zeroSlope(Point start, Point end, Points &points) const { if ((start.x - end.x)==0) { bool isBottomTop = start.y < end.y; if (!isBottomTop) { Point tmp = start; start = end; end = tmp; } double yStart = ceil(start.y); double yEnd = end.y; for (double y = yStart; y < yEnd; y += step_) points.push_back(Point(start.x, y)); if (isBottomTop) std::sort(points.begin(), points.end(), sort_y()); else std::sort(points.begin(), points.end(), sort_reverse_y()); } else { bool isLeftRight = start.x < end.x; if (!isLeftRight) { Point tmp = start; start = end; end = tmp; } double xStart = ceil(start.x); double xEnd = end.x; for (double x = xStart; x < xEnd; x += step_) points.push_back(Point(x, start.y)); if (isLeftRight) std::sort(points.begin(), points.end(), sort_x()); else std::sort(points.begin(), points.end(), sort_reverse_x()); } }
//Builds an x-dimension BST from the ps of length len node* build_xtree(point* ps, int l){ int i; int len; //len is just l rounded up to the next power of 2 if ((l & (l-1)) != 0) { len = 1<<((8*sizeof(int))-__builtin_clz(l)); } else { len = l; } node** nodes = malloc(len*sizeof(node*)); int* indices = malloc(len*sizeof(int)); int* starts = malloc(len*sizeof(int)); int* ends = malloc(len*sizeof(int)); node* new_node; sort_x(ps, l); for (i=0; i<l; i++){ nodes[i] = malloc(sizeof(node)); nodes[i]->l = NULL; nodes[i]->r = NULL; nodes[i]->pivot = ps[i]; nodes[i]->leaves = 1; indices[i] = i; starts[i] = i; ends[i] = i+1; nodes[i]->d = build_ytree(ps, i, i+1); printf("(%d,%d)\n", starts[i], ends[i]); } for (;i<len;i++){ nodes[i] = malloc(sizeof(node)); nodes[i]->l = NULL; nodes[i]->r = NULL; nodes[i]->pivot.x = HUGE_VAL; nodes[i]->pivot.y = HUGE_VAL; nodes[i]->leaves = 0; indices[i] = i; starts[i] = l; ends[i] = l; printf("(%d,%d)\n", starts[i], ends[i]); } while (len>0){ //printf("%d\n", len); len = len>>1; for (i=0; i<len; i++){ new_node = malloc(sizeof(node)); nodes[2*i]->parent = new_node; nodes[2*i+1]->parent = new_node; new_node->l = nodes[2*i]; new_node->r = nodes[2*i+1]; new_node->d = build_ytree(ps, starts[2*i], ends[2*i+1]); new_node->leaves = new_node->l->leaves + new_node->r->leaves; new_node->pivot = ps[(indices[2*i]+indices[2*i+1])/2]; //printf("len=%d, i=%d, indices[]...=%d\n", len, i, (indices[2*i]+indices[2*i+1])/2); printf("start=%d, end=%d\n", starts[2*i], ends[2*i+1]); starts[i] = starts[2*i]; ends[i] = ends[2*i+1]; indices[i] = (indices[2*i]+indices[2*i+1])/2; nodes[i] = new_node; } } free(indices); return nodes[0]; }