write.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "hull.h"
  2. /*
  3. * Outputs the points that comprises the convex hull as a single closed line
  4. * and the hull baricenter as the label points (as it is a linear combination
  5. * of points on the hull is guaranteed to be inside the hull, follow from the
  6. * definition of convex polygon)
  7. */
  8. int outputHull(struct Map_info *Map, struct Point *P, int *hull,
  9. int numPoints)
  10. {
  11. struct line_pnts *Points;
  12. struct line_cats *Cats;
  13. double *tmpx, *tmpy;
  14. int i, pointIdx;
  15. double xc, yc;
  16. tmpx = (double *)G_malloc((numPoints + 1) * sizeof(double));
  17. tmpy = (double *)G_malloc((numPoints + 1) * sizeof(double));
  18. xc = yc = 0;
  19. for (i = 0; i < numPoints; i++) {
  20. pointIdx = hull[i];
  21. tmpx[i] = P[pointIdx].x;
  22. tmpy[i] = P[pointIdx].y;
  23. /* average coordinates calculation... may introduce a little
  24. numerical error but guaratees that no overflow will occur */
  25. xc = xc + tmpx[i] / numPoints;
  26. yc = yc + tmpy[i] / numPoints;
  27. }
  28. tmpx[numPoints] = P[hull[0]].x;
  29. tmpy[numPoints] = P[hull[0]].y;
  30. Points = Vect_new_line_struct();
  31. Cats = Vect_new_cats_struct();
  32. Vect_copy_xyz_to_pnts(Points, tmpx, tmpy, 0, numPoints + 1);
  33. G_free(tmpx);
  34. G_free(tmpy);
  35. /* write out convex hull */
  36. Vect_write_line(Map, GV_BOUNDARY, Points, Cats);
  37. /* find and add centroid */
  38. Vect_reset_line(Points);
  39. Vect_append_point(Points, xc, yc, 0.0);
  40. Vect_cat_set(Cats, 1, 1);
  41. Vect_write_line(Map, GV_CENTROID, Points, Cats);
  42. Vect_destroy_line_struct(Points);
  43. return 0;
  44. }