convex_hull.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 
25 class ConvexHull : public Test
26 {
27 public:
28  enum
29  {
31  };
32 
34  {
35  Generate();
36  m_auto = false;
37  }
38 
39  void Generate()
40  {
41  b2Vec2 lowerBound(-8.0f, -8.0f);
42  b2Vec2 upperBound(8.0f, 8.0f);
43 
44  for (int32 i = 0; i < e_count; ++i)
45  {
46  float x = 10.0f * RandomFloat();
47  float y = 10.0f * RandomFloat();
48 
49  // Clamp onto a square to help create collinearities.
50  // This will stress the convex hull algorithm.
51  b2Vec2 v(x, y);
52  v = b2Clamp(v, lowerBound, upperBound);
53  m_points[i] = v;
54  }
55 
56  m_count = e_count;
57  }
58 
59  void Keyboard(int key) override
60  {
61  switch (key)
62  {
63  case GLFW_KEY_A:
64  m_auto = !m_auto;
65  break;
66 
67  case GLFW_KEY_G:
68  Generate();
69  break;
70  }
71  }
72 
73  void Step(Settings& settings) override
74  {
75  Test::Step(settings);
76 
77  b2PolygonShape shape;
78  shape.Set(m_points, m_count);
79 
80  g_debugDraw.DrawString(5, m_textLine, "Press g to generate a new random convex hull");
82 
83  g_debugDraw.DrawPolygon(shape.m_vertices, shape.m_count, b2Color(0.9f, 0.9f, 0.9f));
84 
85  for (int32 i = 0; i < m_count; ++i)
86  {
87  g_debugDraw.DrawPoint(m_points[i], 3.0f, b2Color(0.3f, 0.9f, 0.3f));
88  g_debugDraw.DrawString(m_points[i] + b2Vec2(0.05f, 0.05f), "%d", i);
89  }
90 
91  if (shape.Validate() == false)
92  {
93  m_textLine += 0;
94  }
95 
96  if (m_auto)
97  {
98  Generate();
99  }
100  }
101 
102  static Test* Create()
103  {
104  return new ConvexHull;
105  }
106 
109  bool m_auto;
110 };
111 
112 static int testIndex = RegisterTest("Geometry", "Convex Hull", ConvexHull::Create);
f
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition: draw.cpp:766
static int testIndex
int32 m_textLine
Definition: test.h:127
static Test * Create()
Definition: test.h:80
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
void DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, const b2Color &color) override
Draw a closed polygon provided in CCW order.
Definition: draw.cpp:639
signed int int32
Definition: b2_types.h:28
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
b2Vec2 m_points[b2_maxPolygonVertices]
void Keyboard(int key) override
Definition: convex_hull.cpp:59
void Generate()
Definition: convex_hull.cpp:39
b2Vec2 m_vertices[b2_maxPolygonVertices]
int32 m_textIncrement
Definition: test.h:135
#define GLFW_KEY_G
Definition: glfw3.h:384
T b2Clamp(T a, T low, T high)
Definition: b2_math.h:648
void Set(const b2Vec2 *points, int32 count)
void Step(Settings &settings) override
Definition: convex_hull.cpp:73
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
#define b2_maxPolygonVertices
Definition: b2_settings.h:53
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
bool Validate() const
DebugDraw g_debugDraw
Definition: draw.cpp:32


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19