File: drgeo_polygon.cc

package info (click to toggle)
drgeo 1.1.0-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,340 kB
  • ctags: 2,481
  • sloc: cpp: 22,807; sh: 11,294; lisp: 1,285; makefile: 378; ansic: 343
file content (421 lines) | stat: -rw-r--r-- 11,382 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 *  Dr Geo an interactive geometry software
 * (C) Copyright Free Software Foundation 2001
 * Auhtor: hilaire@ofset.org 
 * 
 *
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public Licences as by published
 * by the Free Software Foundation; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Publis License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"
#include "drgeo_polygon.h"
#include "drgeo_point.h"
#include "drgeo_direction.h"
#include "drgeo_value.h"
#include "drgeo_repere.h"
#include "drgeo_drgeoStyle.h"
#include "drgeo_drawable.h"

polygon::polygon (liste_elem & parents, polygonType type, gboolean createdFromMacro, liste_elem * figureList):
geometricObject (createdFromMacro, figureList)
{
  gpointer item;

  style.color = preferedColor (":polygonColor");

  this->type = (gint) type;
  category = POLYGON;
  switch (type)
    {
    case POLYGON_NPTS:
      /* the object parentList will contain
         a list of POINT object */
      parents.init_lire ();
      while (item = (gpointer) parents.lire (0))
	parentList = g_list_append (parentList, gpointer (item));
      break;
    case ROTATION:
    case SCALE:
      /* the object parentList will contain in this order              
         POLYGON (point to be transformed) - POINT - VALUE */
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POLYGON));
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POINT));
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								VALUE));
      break;
    case SYMMETRY:
      /* POLYGON - POINT */
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POLYGON));
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POINT));
      break;
    case TRANSLATION:
      /* POLYGON - VECTOR */
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POLYGON));
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								VECTOR));
      break;
    case REFLEXION:
      /* POLYGON - LINE */
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								POLYGON));
      parents.init_lire ();
      parentList = g_list_append (parentList,
				  (gpointer) searchForCategory (parents,
								LINE));
      break;
    }

  if (type & TRANSFORMATION)
    numberVertex = ((polygon *) getFirst)->getNumberVertex ();
  else
    numberVertex = g_list_length (parentList);

  // The number of vertex does not change during the all life of a vertex
  vertices = new drgeoPoint[numberVertex];

  initName ();
}

polygon::polygon (xmlNodePtr tree, GHashTable * itemIdToAdress, liste_elem * figureList):
geometricObject (tree, figureList)
{
  gchar *attr;
  xmlNodePtr item;
  void *obj;

  attr = (gchar *) xmlGetProp (tree, BAD_CAST "type");
  if (!attr)
    /* FIXME: do something there,
       mandatory attribute missing */
    g_printerr ("point::point missing mandatory attribute\n");
  else
    {
      category = POLYGON;
      if (!strcmp (attr, "npts"))
	type = POLYGON_NPTS;
      else if (!strcmp (attr, "Symmetry"))
	type = SYMMETRY;
      else if (!strcmp (attr, "Reflexion"))
	type = REFLEXION;
      else if (!strcmp (attr, "Translation"))
	type = TRANSLATION;
      else if (!strcmp (attr, "Scale"))
	type = SCALE;
      else if (!strcmp (attr, "Rotation"))
	type = ROTATION;

      /* get the parent ref */
      item = xml_search_next (tree->xmlChildrenNode, "parent");
      while (item)
	{
	  if ((obj = xmlInsertParent (item, itemIdToAdress)) == NULL)
	    exit (1);		/* FIXME: implement a nicer way */
	  parentList = g_list_append (parentList, obj);
	  item = xml_search_next (item->next, "parent");
	}
    }
  g_free (attr);

  if (type & TRANSFORMATION)
    numberVertex = ((polygon *) getFirst)->getNumberVertex ();
  else
    numberVertex = g_list_length (parentList);

  // The number of vertex does not change during the all life of a vertex
  vertices = new drgeoPoint[numberVertex];

  initName ();
}

polygon::~polygon ()
{
  delete[]vertices;
}

void
polygon::update (drgeoDrawable & area)
{
  gint nb;
  drgeoPoint c, *prior;
  drgeoVector d;
  gdouble k;

  exist = parentExist ();
  if (!exist)
    return;
  if (type & TRANSFORMATION)
    {
      prior = ((polygon *) getFirst)->getVertices ();
      switch (type)
	{
	case ROTATION:
	  c = ((point *) getN (1))->getCoordinate ();
	  k = ((value *) getN (2))->getValue ();
	  for (nb = 0; nb < numberVertex; nb++)
	    vertices[nb] = prior[nb].rotatePoint (c, k);
	  break;
	case SYMMETRY:
	  c = ((point *) getN (1))->getCoordinate ();
	  for (nb = 0; nb < numberVertex; nb++)
	    vertices[nb] = 2 * c - prior[nb];
	  break;
	case REFLEXION:
	  d = ((direction *) getN (1))->getDirection ();
	  c = ((direction *) getN (1))->getOrigin ();
	  for (nb = 0; nb < numberVertex; nb++)
	    vertices[nb] = prior[nb].reflexionPoint (c, d);
	  break;
	case TRANSLATION:
	  d = ((direction *) getN (1))->getDirection ();
	  for (nb = 0; nb < numberVertex; nb++)
	    vertices[nb] = prior[nb] + d;
	  break;
	case SCALE:
	  c = ((point *) getN (1))->getCoordinate ();
	  k = ((value *) getN (2))->getValue ();
	  for (nb = 0; nb < numberVertex; nb++)
	    vertices[nb] = (prior[nb] - c) * k + c;
	  break;
	}
    }
  else
    {
      for (nb = 0; nb < numberVertex; nb++)
	vertices[nb] = ((point *) getN (nb))->getCoordinate ();
    }
}

void
polygon::move (drgeoVector & t)
{
  GList *parent;
  switch (type)
    {
    case POLYGON_NPTS:
      parent = g_list_first (parentList);
      while (parent != NULL)
	{
	  ((geometricObject *) parent->data)->move (t);
	  parent = g_list_next (parent);
	}
      break;
    }
}

void
polygon::save (xmlNodePtr tree, liste_elem & figureList)
{
  xmlNodePtr item;
  GList *parent;

  parent = g_list_first (parentList);
  item = xmlNewChild (tree, NULL, BAD_CAST "polygon", NULL);

  switch (type)
    {
    case POLYGON_NPTS:
      save_attribute (item, this, "npts");
      break;
    case ROTATION:
      save_attribute (item, this, "Rotation");
      break;
    case SCALE:
      save_attribute (item, this, "Scale");
      break;
    case SYMMETRY:
      save_attribute (item, this, "Symmetry");
      break;
    case REFLEXION:
      save_attribute (item, this, "Reflexion");
      break;
    case TRANSLATION:
      save_attribute (item, this, "Translation");
      break;
    }
  while (parent)
    {
      xmlAddParent (item, (geometricObject *) parent->data);
      parent = g_list_next (parent);
    }
}

void
polygon::updateDescription ()
{
  gint nb;
  geometricObject::updateDescription ();

  switch (type)
    {
    case POLYGON_NPTS:
      description = new gchar *[g_list_length (parentList) + 1];
      description[0] = g_strdup_printf (_("Polygon:: %s"), name);
      for (nb = 0; nb < g_list_length (parentList); nb++)
	description[nb + 1] = g_strdup_printf (_("Point %s"),
					       getNthName (nb));
      break;
    case ROTATION:
      description = new gchar *[4];
      description[0] = g_strdup_printf (_("Rotate polygon:: %s"), name);
      description[1] = g_strdup_printf (_("Transformed polygon %s"),
					getNthName (0));
      description[2] = g_strdup_printf (_("Center %s"), getNthName (1));
      description[3] = g_strdup_printf (_("Angle %s"), getNthName (2));
      break;
    case SYMMETRY:
      description = new gchar *[3];
      description[0] = g_strdup_printf (_("Symmetric polygon:: %s"), name);
      description[1] = g_strdup_printf (_("Transformed polygon %s"),
					getNthName (0));
      description[2] = g_strdup_printf (_("Center %s"), getNthName (1));
      break;
    case REFLEXION:
      description = new gchar *[3];
      description[0] = g_strdup_printf (_("Symmetric polygon:: %s"), name);
      description[1] = g_strdup_printf (_("Transformed polygon %s"),
					getNthName (0));
      description[2] = g_strdup_printf (_("Reflexion axe %s"),
					getNthName (1));
      break;
    case TRANSLATION:
      description = new gchar *[3];
      description[0] = g_strdup_printf (_("Translate polygon:: %s"), name);
      description[1] = g_strdup_printf (_("Translated polygon %s"),
					getNthName (0));
      description[2] = g_strdup_printf (_("Vector of translation %s"),
					getNthName (1));
      break;
    case SCALE:
      description = new gchar *[4];
      description[0] = g_strdup_printf (_("Scale polygon:: %s"), name);
      description[1] = g_strdup_printf (_("Scaled polygon %s"),
					getNthName (0));
      description[2] = g_strdup_printf (_("Center %s"), getNthName (1));
      description[3] = g_strdup_printf (_("Factor %s"), getNthName (2));
      break;
    }
}

void
polygon::draw (drgeoDrawable & area, char force)
{
  if ((style.mask == alway) || (style.mask == yes && force == FALSE)
      || !exist)
    return;
  area.drawPolygon (style, vertices, numberVertex);
}

gboolean
polygon::overObject (drgeoPoint & mouse, gdouble range)
{
  drgeoVector u;
  drgeoPoint origin;
  gint nb;

  if (!exist)
    return FALSE;
  for (nb = 0; nb < numberVertex - 1; nb++)
    {
      origin = vertices[nb];
      u = vertices[nb + 1] - origin;
      if (ABS (u ^ (mouse - origin)) / u.norm () <= range
	  && ((mouse - origin) * u) >= 0
	  && ((mouse - origin) * u) <= u.squareNorm ())
	return TRUE;
    }

  // Check with the segment formed by the first and last point
  origin = vertices[0];
  u = vertices[numberVertex - 1] - origin;
  if (ABS (u ^ (mouse - origin)) / u.norm () <= range
      && ((mouse - origin) * u) >= 0
      && ((mouse - origin) * u) <= u.squareNorm ())
    return TRUE;
  return FALSE;
}

void
polygon::initName ()
{
  int l, nb;
  gboolean autoName = TRUE;


  if (style.mask == alway)
    return;
  if (typeName != NULL)
    g_free (typeName);
  l = strlen (_("this polygon %1")) + 1;

  nb = g_list_length (parentList);
  while (nb-- && autoName)
    if (!getNthName (nb))
      autoName = FALSE;
    else if (strlen (getNthName (nb)) == 0)
      autoName = FALSE;
  if (autoName)
    {
      gchar *tmp;
      g_free (name);
      name = g_strdup ("");
      for (nb = 0; nb < g_list_length (parentList); nb++)
	{
	  tmp = g_strconcat (name, getNthName (nb), NULL);
	  g_free (name);
	  name = tmp;
	}
    }

  if (name)
    l += strlen (name);
  typeName = (gchar *) g_malloc (l);
  strcpy (typeName, _("this polygon %1"));
  strinsmsg (typeName, name, "%1");
}

drgeoPoint *
polygon::getVertices ()
{
  return vertices;
}

gint
polygon::getNumberVertex ()
{
  return numberVertex;
}