parent
7bdf16c743
commit
aa0c40b262
@ -0,0 +1,123 @@
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
#include "geometry.h"
|
||||
#include "util.h"
|
||||
|
||||
/* function for creating a random set of points in unit square */
|
||||
|
||||
int generate_random_points_2d(
|
||||
int node_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates)
|
||||
{
|
||||
int rval = 0, i, j, winner, x, y;
|
||||
int **hit = 0, *hit_count = 0;
|
||||
|
||||
hit = (int **) malloc(grid_size * sizeof(int *));
|
||||
ABORT_IF (!hit, "out of memory for hit\n");
|
||||
|
||||
for (i = 0; i < grid_size; i++)
|
||||
hit[i] = 0;
|
||||
|
||||
hit_count = (int *) malloc(grid_size * sizeof(int));
|
||||
ABORT_IF(!hit_count, "out of memory for hit_count\n");
|
||||
|
||||
for (i = 0; i < grid_size; i++)
|
||||
hit_count[i] = 0;
|
||||
|
||||
for (i = 0; i < node_count; i++)
|
||||
{
|
||||
winner = 0;
|
||||
do
|
||||
{
|
||||
x = rand() % grid_size;
|
||||
y = rand() % grid_size;
|
||||
|
||||
/* check to see if (x,y) is a duplicate point */
|
||||
for (j = 0; j < hit_count[x]; j++)
|
||||
if (hit[x][j] == y) break;
|
||||
|
||||
if (j == hit_count[x])
|
||||
{
|
||||
void *tmp_ptr = (void *) hit[x];
|
||||
tmp_ptr = realloc(tmp_ptr, (hit_count[x] + 1) * sizeof(int));
|
||||
ABORT_IF (!tmp_ptr, "could not reallocate hit_count\n");
|
||||
|
||||
hit[x] = (int *) tmp_ptr;
|
||||
hit[x][hit_count[x]] = y;
|
||||
hit_count[x]++;
|
||||
winner = 1;
|
||||
}
|
||||
} while (!winner);
|
||||
|
||||
x_coordinates[i] = (double) x;
|
||||
y_coordinates[i] = (double) y;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
|
||||
if (hit)
|
||||
{
|
||||
for (i = 0; i < grid_size; i++)
|
||||
if (hit[i]) free(hit[i]);
|
||||
|
||||
free(hit);
|
||||
}
|
||||
if (hit_count) free(hit_count);
|
||||
return rval;
|
||||
}
|
||||
|
||||
int generate_random_clusters_2d(
|
||||
int node_count,
|
||||
int cluster_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int *clusters)
|
||||
{
|
||||
int rval = 0;
|
||||
|
||||
rval = generate_random_points_2d(node_count, grid_size, x_coordinates,
|
||||
y_coordinates);
|
||||
ABORT_IF(rval, "generate_random_points_2d failed");
|
||||
|
||||
for (int i = 0; i < cluster_count; i++)
|
||||
clusters[i] = i;
|
||||
|
||||
for (int i = cluster_count; i < node_count; i++)
|
||||
{
|
||||
int closest_point = 0;
|
||||
int closest_distance = INT_MAX;
|
||||
|
||||
for (int j = 0; j < cluster_count; j++)
|
||||
{
|
||||
int distance =
|
||||
get_euclidean_distance(x_coordinates, y_coordinates, i, j);
|
||||
|
||||
if (distance < closest_distance)
|
||||
{
|
||||
closest_distance = distance;
|
||||
closest_point = j;
|
||||
}
|
||||
}
|
||||
|
||||
clusters[i] = closest_point;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
return rval;
|
||||
}
|
||||
|
||||
int get_euclidean_distance(
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int p1_index,
|
||||
int p2_index)
|
||||
{
|
||||
double t1 = x_coordinates[p1_index] - x_coordinates[p2_index];
|
||||
double t2 = y_coordinates[p1_index] - y_coordinates[p2_index];
|
||||
return (int) (sqrt(t1 * t1 + t2 * t2) + 0.5);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#ifndef _PROJECT_GEOMETRY_H_
|
||||
#define _PROJECT_GEOMETRY_H_
|
||||
|
||||
int generate_random_points_2d(
|
||||
int node_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates);
|
||||
|
||||
int generate_random_clusters_2d(
|
||||
int node_count,
|
||||
int cluster_count,
|
||||
int grid_size,
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int *clusters);
|
||||
|
||||
int get_euclidean_distance(
|
||||
double *x_coordinates,
|
||||
double *y_coordinates,
|
||||
int p1_index,
|
||||
int p2_index);
|
||||
|
||||
#endif //_PROJECT_GEOMETRY_H_
|
Reference in new issue