File: polygon_generator.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (189 lines) | stat: -rw-r--r-- 8,089 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Polygon Generator. Generate various polygon shape on demand (including random one).
 *****************************************************************************/

#include <stdlib.h>
#include "graphic/polygon.h"
#include "graphic/polygon_generator.h"
#include "network/randomsync.h"
#include "tool/affine_transform.h"

const int PolygonGenerator::MIN_SPACE_BETWEEN_POINT = 50;

Polygon * PolygonGenerator::GenerateCircle(Double diameter, int nb_point)
{
  return PolygonGenerator::GenerateDentedCircle(diameter, nb_point, ZERO);
}

Polygon * PolygonGenerator::GenerateRectangle(Double width, Double height)
{
  Polygon * tmp = new Polygon();
  tmp->AddPoint(Point2d( width * ONE_HALF,  height * ONE_HALF));
  tmp->AddPoint(Point2d( width * ONE_HALF, -height * ONE_HALF));
  tmp->AddPoint(Point2d(-width * ONE_HALF, -height * ONE_HALF));
  tmp->AddPoint(Point2d(-width * ONE_HALF,  height * ONE_HALF));
  return tmp;
}

Polygon * PolygonGenerator::GenerateRectangle(const Point2d & orig, const Point2d & size)
{
  Polygon * tmp = new Polygon();
  tmp->AddPoint(Point2d(orig.x + size.x, orig.y + size.y));
  tmp->AddPoint(Point2d(orig.x + size.x, orig.y));
  tmp->AddPoint(Point2d(orig.x,          orig.y));
  tmp->AddPoint(Point2d(orig.x,          orig.y + size.y));
  return tmp;
}

Polygon * PolygonGenerator::GenerateRectangle(const Rectanglei & r)
{
  return PolygonGenerator::GenerateRectangle(r.GetPosition(), r.GetSize());
}

Polygon * PolygonGenerator::GenerateRectangle(const Point2i & orig, const Point2i & size)
{
  return PolygonGenerator::GenerateRectangle((Point2d)orig, (Point2d)size);
}

Polygon * PolygonGenerator::GenerateDentedCircle(Double diameter, int nb_point, Double rand_offset)
{
  Polygon * tmp = new Polygon();
  AffineTransform2D trans = AffineTransform2D();
  Point2d top;
  for(int i = 0; i < nb_point; i++) {
    top = Point2d(ZERO, (diameter + RandomSync().GetDouble(-rand_offset, rand_offset)) * ONE_HALF);
    trans.SetRotation((TWO_PI * -i) / nb_point);
    tmp->AddPoint(trans * top);
  }
  return tmp;
}

Polygon * PolygonGenerator::GenerateRoundedRectangle(Double width, Double height, Double edge)
{
  Polygon * tmp = new Polygon();
  Double edge_vector = edge * ONE_HALF;
  tmp->AddBezierCurve(Point2d(-width * ONE_HALF + edge, -height * ONE_HALF),
                      Point2d(-edge_vector, 0),
                      Point2d(0, -edge_vector),
                      Point2d(-width * ONE_HALF, -height * ONE_HALF + edge));
  tmp->AddBezierCurve(Point2d(-width * ONE_HALF, height * ONE_HALF - edge),
                      Point2d(0, edge_vector),
                      Point2d(-edge_vector, 0),
                      Point2d(-width * ONE_HALF + edge, height * ONE_HALF));
  tmp->AddBezierCurve(Point2d(width * ONE_HALF - edge, height * ONE_HALF),
                      Point2d(edge_vector, 0),
                      Point2d(0, edge_vector),
                      Point2d(width * ONE_HALF, height * ONE_HALF - edge));
  tmp->AddBezierCurve(Point2d(width * ONE_HALF, -height * ONE_HALF + edge),
                      Point2d(0, -edge_vector),
                      Point2d(edge_vector, 0),
                      Point2d(width * ONE_HALF - edge, -height * ONE_HALF));
  return tmp;
}

Polygon * PolygonGenerator::GenerateRandomShape()
{
  Double height = RandomSync().GetDouble(400.0, 600.0);
  Double width  = RandomSync().GetDouble(400.0, 2000.0);
  return GenerateRandomTrapeze(width, height, RandomSync().GetDouble(10.0, 15.0), RandomSync().GetDouble(10.0, 15.0),
                               RandomSync().GetSign() * RandomSync().GetDouble(0.5, 1.0));
}

Polygon * PolygonGenerator::GenerateRandomTrapeze(const Double width, const Double height,
                                                  const Double x_rand_offset, const Double y_rand_offset,
                                                  const Double coef)
{
  Double upper_width, lower_width, upper_offset, lower_offset;
  int number_of_bottom_point, number_of_side_point;
  // XXX Unused !?
  // int number_of_upper_point;
  Polygon * tmp = new Polygon();
  number_of_side_point = 1 + (int)RandomSync().GetDouble((height / FOUR) / MIN_SPACE_BETWEEN_POINT,
                                     height / MIN_SPACE_BETWEEN_POINT);
  if(coef > ZERO) {
    upper_width = width;
    lower_width = width * coef;
    upper_offset = RandomSync().GetDouble(ZERO, width - lower_width);
    lower_offset = ZERO;
  } else {
    upper_width = - width * coef;
    lower_width = width;
    upper_offset = ZERO;
    lower_offset = RandomSync().GetDouble(ZERO, width - upper_width);
  }
  // XXX Unused !?
  //number_of_upper_point = RandomSync().GetInt(1 + (int)((upper_width * 0.25) / MIN_SPACE_BETWEEN_POINT),
  //                                       (int)(upper_width / MIN_SPACE_BETWEEN_POINT));
  number_of_bottom_point = RandomSync().GetInt(1 + (int)((lower_width / FOUR) / MIN_SPACE_BETWEEN_POINT),
                                          (int)((coef * lower_width) / MIN_SPACE_BETWEEN_POINT));
  tmp->AddRandomCurve(Point2d(upper_offset, ZERO), Point2d(lower_offset, height),
                      x_rand_offset, y_rand_offset, number_of_side_point, false, false);
  tmp->AddRandomCurve(Point2d(lower_offset, height), Point2d(lower_offset + lower_width, height),
                      x_rand_offset, y_rand_offset, number_of_bottom_point, false, false);
  tmp->AddRandomCurve(Point2d(lower_offset + lower_width, height), Point2d(upper_offset + upper_width, ZERO),
                      x_rand_offset, y_rand_offset, number_of_side_point, false, false);
  tmp->AddRandomCurve(Point2d(upper_offset + upper_width, ZERO), Point2d(upper_offset, ZERO),
                      x_rand_offset, y_rand_offset, number_of_side_point, false, false);
  return tmp;
}

Polygon * PolygonGenerator::GeneratePie(Double diameter, int nb_point, Double angle, Double angle_offset)
{
  Polygon * tmp = new Polygon();
  AffineTransform2D trans = AffineTransform2D();
  Point2d top;
  for(int i = 0; i < nb_point; i++) {
    top = Point2d(ZERO, diameter * ONE_HALF);
    trans.SetRotation(angle_offset + ((i * angle) / nb_point));
    tmp->AddPoint(trans * top);
  }
  if(angle < TWO_PI)
    tmp->AddPoint(Point2d(0, 0));
  return tmp;
}

Polygon * PolygonGenerator::GeneratePartialTorus(Double diameter, Double min_diameter, int nb_point, Double angle, Double angle_offset)
{
  if(diameter < min_diameter) {
    Double tmp = diameter;
    diameter = min_diameter;
    min_diameter = tmp;
  }
  Polygon * tmp = new Polygon();
  AffineTransform2D trans = AffineTransform2D();
  Point2d top = Point2d(0, diameter*ONE_HALF);
  for(int i = 0; i < nb_point; i++) {
    trans.SetRotation(angle_offset + ((i * angle) / (nb_point - 1)));
    tmp->AddPoint(trans * top);
  }
  top = Point2d(0, min_diameter*ONE_HALF);
  for(int i = nb_point - 1; i >= 0; i--) {
    trans.SetRotation(angle_offset + ((i * angle) / (nb_point - 1)));
    tmp->AddPoint(trans * top);
  }
  return tmp;
}

DecoratedBox * PolygonGenerator::GenerateDecoratedBox(Double width, Double height)
{
  DecoratedBox * tmp = new DecoratedBox(width, height);

  return tmp;
}