cae-tools/solidify/histo.c

53 lines
1.0 KiB
C

/*
* histo.c - Distribution of Z values
*
* Written 2010 by Werner Almesberger
* Copyright 2010 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include "util.h"
#include "array.h"
#include "histo.h"
struct histo *make_histo(struct array *a)
{
int i;
struct histo *h;
h = alloc_type(struct histo);
h->n = a->max_z-a->min_z+1;
h->b = alloc_size(h->n*sizeof(int));
for (i = 0; i != h->n; i++)
h->b[i] = 0;
for (i = 0; i != (a->max_x-a->min_x+1)*(a->max_y-a->min_y+1); i++)
if (a->data[i] != UNDEF)
h->b[a->data[i]-a->min_z]++;
return h;
}
void free_histo(struct histo *h)
{
free(h);
}
int median(const struct histo *h)
{
int tot = 0, sum = 0;
int i;
for (i = 0; i != h->n; i++)
tot += h->b[i];
for (i = 0; sum < tot/2; i++)
sum += h->b[i];
return i-1;
}