1 /*
2 * s_centre.cc
3 * AYM 1998-11-22
4 */
5
6
7 /*
8 This file is part of Yadex.
9
10 Yadex incorporates code from DEU 5.21 that was put in the public domain in
11 1994 by Rapha�l Quinet and Brendon Wyber.
12
13 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
14
15 This program is free software; you can redistribute it and/or modify it under
16 the terms of the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any later
18 version.
19
20 This program is distributed in the hope that it will be useful, but WITHOUT
21 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License along with
25 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
26 Place, Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29
30 #include "yadex.h"
31 #include "bitvec.h"
32 #include "levels.h"
33 #include "s_centre.h"
34 #include "s_vertices.h"
35
36
37 /*
38 * centre_of_sector
39 * Return the coordinates of the centre of a sector.
40 *
41 * FIXME The algorithm is desesperatingly simple-minded and
42 * does not take into account concave sectors and sectors
43 * that are enclosed by several distinct paths of linedefs.
44 */
centre_of_sector(obj_no_t s,int * x,int * y)45 void centre_of_sector (obj_no_t s, int *x, int *y)
46 {
47 bitvec_c *vertices = bv_vertices_of_sector (s);
48 long x_sum = 0;
49 long y_sum = 0;
50 int nitems = 0;
51
52 for (size_t n = 0; n < vertices->nelements (); n++)
53 if (vertices->get (n))
54 {
55 x_sum += Vertices[n].x;
56 y_sum += Vertices[n].y;
57 nitems++;
58 }
59 if (nitems == 0)
60 {
61 *x = 0;
62 *y = 0;
63 }
64 else
65 {
66 *x = (int) (x_sum / nitems);
67 *y = (int) (y_sum / nitems);
68 }
69 delete vertices;
70 }
71
72
73 /*
74 * centre_of_sectors
75 * Return the coordinates of the centre of a group of sectors.
76 */
centre_of_sectors(SelPtr list,int * x,int * y)77 void centre_of_sectors (SelPtr list, int *x, int *y)
78 {
79 bitvec_c *vertices;
80 int nitems;
81 long x_sum;
82 long y_sum;
83 int n;
84
85 vertices = bv_vertices_of_sectors (list);
86 x_sum = 0;
87 y_sum = 0;
88 nitems = 0;
89 for (n = 0; n < NumVertices; n++)
90 if (vertices->get (n))
91 {
92 x_sum += Vertices[n].x;
93 y_sum += Vertices[n].y;
94 nitems++;
95 }
96 if (nitems == 0)
97 {
98 *x = 0;
99 *y = 0;
100 }
101 else
102 {
103 *x = (int) (x_sum / nitems);
104 *y = (int) (y_sum / nitems);
105 }
106 delete vertices;
107 }
108
109
110