Root/solidify/histo.c

1/*
2 * histo.c - Distribution of Z values
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include "util.h"
15#include "array.h"
16#include "histo.h"
17
18
19struct histo *make_histo(struct array *a)
20{
21    int i;
22    struct histo *h;
23
24    h = alloc_type(struct histo);
25    h->n = a->max_z-a->min_z+1;
26    h->b = alloc_size(h->n*sizeof(int));
27    for (i = 0; i != h->n; i++)
28        h->b[i] = 0;
29    for (i = 0; i != (a->max_x-a->min_x+1)*(a->max_y-a->min_y+1); i++)
30        if (a->data[i] != UNDEF)
31            h->b[a->data[i]-a->min_z]++;
32    return h;
33}
34
35
36void free_histo(struct histo *h)
37{
38    free(h);
39}
40
41
42int median(const struct histo *h)
43{
44    int tot = 0, sum = 0;
45    int i;
46
47    for (i = 0; i != h->n; i++)
48        tot += h->b[i];
49    for (i = 0; sum < tot/2; i++)
50        sum += h->b[i];
51    return i-1;
52}
53

Archive Download this file

Branches:
master



interactive