示例#1
0
void bezier_split(Bezier* b, float t, Bezier** left, Bezier** right)
{
  float u = (t - b->dom->a) / (b->dom->b - b->dom->a);

  assert(0.0f <= u && u <= 1.0f);
  // same as:
  assert(interval_inside(b->dom, t));
  
  *left = bezier_create(b->n);
  *right = bezier_create(b->n);

  float (*d)[b->n + 1] = calloc((b->n + 1) * (b->n + 1), sizeof(float));

  for(int i = 0; i <= b->n; ++i)
    d[0][i] = b->c[i];

  for(int j = 1; j <= b->n; ++j)
    for(int i = 0; i <= b->n - j; ++i)
      d[j][i] = (1.0f - u) * d[j-1][i] + u * d[j-1][i+1];

  for(int i = 0; i <= b->n; ++i)
  {
    (*left)->c[i] = d[i][0];
    (*right)->c[i] = d[b->n - i][i];
  }

  (*left)->dom = interval_create(b->dom->a, t);
  (*right)->dom = interval_create(t, b->dom->b);

  free(d);
}
示例#2
0
int interval_difference(Interval* i, Interval* j, Interval*** intervals)
{
    if(!interval_overlapps(i, j))
    {
        *intervals = malloc(sizeof(Interval*));
        (*intervals)[0] = interval_copy(i);
        return 1;
    }
    else if(!interval_subinterval(i, j))
    {
        if(i->a < j->a)
        {
            *intervals = malloc(sizeof(Interval*));
            (*intervals)[0] = interval_create(j->b, i->b);
            return 1;
        }
        else
        {
            *intervals = malloc(sizeof(Interval*));
            (*intervals)[0] = interval_create(i->a, j->a);
            return 1;
        }
    }
    else
    {
        *intervals = malloc(2 * sizeof(Interval*));
        (*intervals)[0] = interval_create(i->a, j->a);
        (*intervals)[1] = interval_create(j->b, i->b);
        return 2;
    }
}
示例#3
0
文件: tcp_rr.c 项目: furen62682/neper
/**
 * The function expects @fd_listen is in a "ready" state in the @epfd
 * epoll set, and directly calls accept() on @fd_listen. The readiness
 * should guarantee that the accept() doesn't block.
 *
 * After a client socket fd is obtained, a new flow is created as part
 * of the thread @t.  The state of the flow is set to "waiting for a
 * request".
 */
static void server_accept(int fd_listen, int epfd, struct thread *t)
{
        struct options *opts = t->opts;
        struct callbacks *cb = t->cb;
        struct sockaddr_storage cli_addr;
        struct flow *flow;
        socklen_t cli_len;
        int client;

        cli_len = sizeof(cli_addr);
        client = accept(fd_listen, (struct sockaddr *)&cli_addr, &cli_len);
        if (client == -1) {
                if (errno == EINTR || errno == ECONNABORTED)
                        return;
                PLOG_ERROR(cb, "accept");
                return;
        }
        flow = addflow(t->index, epfd, client, t->next_flow_id++,
                       EPOLLIN, opts, cb);
        flow->bytes_to_read = opts->request_size;
        flow->itv = interval_create(opts->interval, t);
}
示例#4
0
文件: tcp_rr.c 项目: furen62682/neper
static void client_connect(int i, int epfd, struct thread *t)
{
        struct options *opts = t->opts;
        struct callbacks *cb = t->cb;
        struct addrinfo *ai = t->ai;
        struct flow *flow;
        int fd;

        fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
        if (fd == -1)
                PLOG_FATAL(cb, "socket");
        if (opts->min_rto)
                set_min_rto(fd, opts->min_rto, cb);
        if (opts->debug)
                set_debug(fd, 1, cb);
        if (opts->local_host)
                set_local_host(fd, opts, cb);
        if (do_connect(fd, ai->ai_addr, ai->ai_addrlen))
                PLOG_FATAL(cb, "do_connect");

        flow = addflow(t->index, epfd, fd, i, EPOLLOUT, opts, cb);
        flow->bytes_to_write = opts->request_size;
        flow->itv = interval_create(opts->interval, t);
}
示例#5
0
int main(int argc, char *argv[]) {
  int ret;
  char *x0 = NULL, *x1 = NULL;
  char *epsilon = EPSILON, *func = NULL;
  char *max_iter = MAXITER, *method = "secant";
  long double r;

  /* parse command line */
  while ((ret = getopt(argc, argv, "vm:e:i:a:b:")) != -1) {
    switch (ret) {
      case 'e':
        epsilon = optarg; /* optarg is just a pointer to argv */
        break;
      case 'i':
        max_iter = optarg;
        break;
      case 'a':
        x0 = optarg;
        break;
      case 'b':
        x1 = optarg;
        break;
      case 'm':
        method = optarg;
        break;
      case 'v':
        root_search_verbose = 1;
        break;
      default:
        return 1;
    }
  }
  if (optind >= argc || !x0) {
    fprintf(stderr, "usage: %s [-v] [-e <epsilon>] [-i <max-iter>] [-m <method>]"
            "-a <x0> [-b <x1>] <function eg: cos(x)-x^3>\n", argv[0]);
    return 1;
  } else {
    func = argv[optind];
  }

  function_t *f = function_create(func);
  interval_t *i = interval_create(atof(x0), x1 ? atof(x1) : (atof(x0) + 1.0));
  stop_cond_t *s = stop_cond_create(atof(epsilon), atoi(max_iter));

  if (!strcmp(method, "secant"))
    ret = root_secant(f, i, s, &r);
  else if (!strcmp(method, "bisection"))
    ret = root_bisection(f, i, s, &r);
  else if (!strcmp(method, "regulafalsi"))
    ret = root_regulafalsi(f, i, s, &r);
  else if (!strcmp(method, "newton"))
    ret = root_newton(f, atof(x0), s, &r);
  else {
    fprintf(stderr, "Method not recognized: %s\n", method);
    return 1;
  }

  if (ret) {
    fprintf(stderr, "%s method failed (ret: %d)\n", method, ret);
  } else {
    printf("%.15Lg\n", r);
  }

  function_destroy(f);
  interval_destroy(i);
  stop_cond_destroy(s);

  return ret;
}
示例#6
0
Interval* interval_copy(Interval* i)
{
    return interval_create(i->a, i->b);
}
示例#7
0
Interval* interval_intersection(Interval* i, Interval* j)
{
    return interval_create(max(i->a, j->a), min(i->b, j->b));
}