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());
    }
  }
Beispiel #2
0
//Builds a y-dimension BST from the sub array ps_[start,end)
node* build_ytree(point* ps_, int start, int end){
    printf("Building ytree from %d to %d\n", start, end);
    int i;
    int len;
    int l = end-start;
    point* ps = malloc((l)*sizeof(point));

    memcpy(ps, &ps_[start], (l)*sizeof(point));

    //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;
    }

    printf("len=%d, l=%d\n",len,l);

    node** nodes = malloc(len*sizeof(node*));
    int* indices = malloc(len*sizeof(int));
    node* new_node;

    sort_y(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;
    }

    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;
    }

    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 = NULL;
            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);
            indices[i] = (indices[2*i]+indices[2*i+1])/2;
            nodes[i] = new_node;
        }
    }

    free(indices);

    return nodes[0];
}