wykobi_graphics_net.hpp
Go to the documentation of this file.
1 /*
2 (***********************************************************************)
3 (* *)
4 (* Wykobi Computational Geometry Library *)
5 (* Release Version 0.0.5 *)
6 (* http://www.wykobi.com *)
7 (* Copyright (c) 2005-2017 Arash Partow, All Rights Reserved. *)
8 (* *)
9 (* The Wykobi computational geometry library and its components are *)
10 (* supplied under the terms of the open source MIT License. *)
11 (* The contents of the Wykobi computational geometry library and its *)
12 (* components may not be copied or disclosed except in accordance with *)
13 (* the terms of the MIT License. *)
14 (* *)
15 (* URL: https://opensource.org/licenses/MIT *)
16 (* *)
17 (***********************************************************************)
18 */
19 
20 #ifndef INCLUDE_WYKOBI_GRAPHICS_NET
21 #define INCLUDE_WYKOBI_GRAPHICS_NET
22 
23 #include <math.h>
24 #include <vcclr.h>
25 
26 #include <iostream>
27 #include <iterator>
28 #include <string>
29 #include <vector>
30 
31 #include "wykobi.hpp"
32 
33 using namespace System;
34 using namespace System::Drawing;
35 
36 namespace wykobi {
37 template <typename T>
39  public:
40  wykobi_graphics_net(Graphics ^ gc, const unsigned int& w,
41  const unsigned int& h)
42  : gc_(gc), width_(w), height_(h) {
43  pen_ = gcnew Pen(System::Drawing::Color::Black, 1);
44  }
45 
46  ~wykobi_graphics_net() { delete gc_; }
47 
48  inline void set_pen(Pen ^ _pen) const { pen_ = _pen; }
49 
50  inline float get_pen_width() const { return pen_->Width; }
51 
52  inline void set_pen_width(const unsigned int& w) const {
53  pen_->Width = float(w);
54  }
55 
56  inline void set_dash_mode() const {
57  array<Single> ^ temp0 = {5.0F, 5.0F, 5.0F, 5.0F};
58  pen_->DashPattern = temp0;
59  }
60 
61  inline void set_nodash_mode() const {
62  array<Single> ^ temp0 = {1.0F, 0.0001F};
63  pen_->DashPattern = temp0;
64  }
65 
66  inline void set_color(const unsigned int& color) const {
67  switch (color % 15) {
68  case 0:
69  pen_->Color = System::Drawing::Color::Aqua;
70  break;
71  case 1:
72  pen_->Color = System::Drawing::Color::Black;
73  break;
74  case 2:
75  pen_->Color = System::Drawing::Color::Blue;
76  break;
77  case 3:
78  pen_->Color = System::Drawing::Color::Brown;
79  break;
80  case 4:
81  pen_->Color = System::Drawing::Color::Cyan;
82  break;
83  case 5:
84  pen_->Color = System::Drawing::Color::Gray;
85  break;
86  case 6:
87  pen_->Color = System::Drawing::Color::Green;
88  break;
89  case 7:
90  pen_->Color = System::Drawing::Color::Indigo;
91  break;
92  case 8:
93  pen_->Color = System::Drawing::Color::LimeGreen;
94  break;
95  case 9:
96  pen_->Color = System::Drawing::Color::Magenta;
97  break;
98  case 10:
99  pen_->Color = System::Drawing::Color::Orange;
100  break;
101  case 11:
102  pen_->Color = System::Drawing::Color::Purple;
103  break;
104  case 12:
105  pen_->Color = System::Drawing::Color::Red;
106  break;
107  case 13:
108  pen_->Color = System::Drawing::Color::Violet;
109  break;
110  case 14:
111  pen_->Color = System::Drawing::Color::White;
112  break;
113  case 15:
114  pen_->Color = System::Drawing::Color::Yellow;
115  break;
116  }
117  }
118 
119  inline unsigned int width() const { return width_; }
120  inline unsigned int height() const { return height_; }
121  inline unsigned int center_x() const { return width_ * 0.5; }
122  inline unsigned int center_y() const { return height_ * 0.5; }
123 
124  inline void draw_pixel(const T& x, const T& y) const {
125  gc_->DrawLine(pen_, float(x), float(y), float(x + 1.0f), float(y + 1.0f));
126  }
127 
128  inline void draw_pixel(const point2d<T>& point) const {
129  draw_pixel(point.x, point.y);
130  }
131 
132  inline void draw_segment(const T& x1, const T& y1, const T& x2,
133  const T& y2) const {
134  gc_->DrawLine(pen_, float(x1), float(y1), float(x2), float(y2));
135  }
136 
137  inline void draw_segment(const point2d<T>& point1,
138  const point2d<T>& point2) const {
139  draw_segment(point1.x, point1.y, point2.x, point2.y);
140  }
141 
142  inline void draw_line(const T& x1, const T& y1, const T& x2,
143  const T& y2) const {
144  T dx = x2 - x1;
145  T dy = y2 - y1;
146 
147  if (dx != 0.0) {
148  T m = dy / dx;
149  T c = y1 - m * x1;
150 
151  draw_segment(0.0, c, width_, m * width_ + c);
152  } else
153  draw_segment(x1, 0, x1, height_);
154  }
155 
156  inline void draw_triangle(const T& x1, const T& y1, const T& x2, const T& y2,
157  const T& x3, const T& y3) const {
158  draw_segment(x1, y1, x2, y2);
159  draw_segment(x2, y2, x3, y3);
160  draw_segment(x3, y3, x1, y1);
161  }
162 
163  inline void draw_rectangle(const T& x1, const T& y1, const T& x2,
164  const T& y2) const {
165  gc_->DrawRectangle(pen_, float(x1), float(y1), fabs(float(x2 - x1)),
166  fabs(float(y2 - y1)));
167  }
168 
169  inline void draw_quadix(const T& x1, const T& y1, const T& x2, const T& y2,
170  const T& x3, const T& y3, const T& x4,
171  const T& y4) const {
172  draw_segment(x1, y1, x2, y2);
173  draw_segment(x2, y2, x3, y3);
174  draw_segment(x3, y3, x4, y4);
175  draw_segment(x4, y4, x1, y1);
176  }
177 
178  inline void draw_circle(const T& x, const T& y, const T& radius) const {
179  gc_->DrawEllipse(pen_, float(x - radius), float(y - radius),
180  float(2.0f * radius), float(2.0f * radius));
181  }
182 
183  inline void draw_circle(const point2d<T> center, const T& radius) const {
184  draw_circle(center.x, center.y, radius);
185  }
186 
187  inline void draw_polyline(const std::vector<point2d<T> >& point_list) const {
188  for (std::size_t i = 0; i < point_list.size() - 1; ++i) {
189  draw_segment(point_list[i], point_list[i + 1]);
190  }
191  }
192 
193  inline void draw_polyline(const std::vector<point3d<T> >& point_list) const {
194  for (std::size_t i = 0; i < point_list.size() - 1; ++i) {
195  draw_segment(point_list[i], point_list[i + 1]);
196  }
197  }
198 
199  inline void draw_crosshair(const point2d<T>& p, const T r) const {
200  draw_segment(p.x - r, p.y, p.x + r, p.y);
201  draw_segment(p.x, p.y - r, p.x, p.y + r);
202  }
203 
204  inline void clear(System::Drawing::Color color) const { gc_->Clear(color); }
205 
206  inline void clear() const { clear(System::Drawing::Color::White); }
207 
208  inline void clear(const unsigned int& color) const {
209  switch (color) {
210  case 0:
211  clear(System::Drawing::Color::Aqua);
212  break;
213  case 1:
214  clear(System::Drawing::Color::Black);
215  break;
216  case 2:
217  clear(System::Drawing::Color::Blue);
218  break;
219  case 3:
220  clear(System::Drawing::Color::Brown);
221  break;
222  case 4:
223  clear(System::Drawing::Color::Cyan);
224  break;
225  case 5:
226  clear(System::Drawing::Color::Gray);
227  break;
228  case 6:
229  clear(System::Drawing::Color::Green);
230  break;
231  case 7:
232  clear(System::Drawing::Color::Indigo);
233  break;
234  case 8:
235  clear(System::Drawing::Color::LimeGreen);
236  break;
237  case 9:
238  clear(System::Drawing::Color::Magenta);
239  break;
240  case 10:
241  clear(System::Drawing::Color::Orange);
242  break;
243  case 11:
244  clear(System::Drawing::Color::Purple);
245  break;
246  case 12:
247  clear(System::Drawing::Color::Red);
248  break;
249  case 13:
250  clear(System::Drawing::Color::Violet);
251  break;
252  case 14:
253  clear(System::Drawing::Color::White);
254  break;
255  case 15:
256  clear(System::Drawing::Color::Yellow);
257  break;
258  default:
259  clear(System::Drawing::Color::White);
260  break;
261  }
262  }
263 
264  inline void draw(const point2d<T>& point) const {
265  draw_pixel(point.x, point.y);
266  }
267 
268  inline void draw(const segment<T, 2>& segment) const {
269  draw_segment(segment[0].x, segment[0].y, segment[1].x, segment[1].y);
270  }
271 
272  inline void draw(const triangle<T, 2>& triangle) const {
273  draw_triangle(triangle[0].x, triangle[0].y, triangle[1].x, triangle[1].y,
274  triangle[2].x, triangle[2].y);
275  }
276 
277  inline void draw(const rectangle<T>& rectangle) const {
278  draw_rectangle(rectangle[0].x, rectangle[0].y, rectangle[1].x,
279  rectangle[1].y);
280  }
281 
282  inline void draw(const quadix<T, 2>& quadix) const {
283  draw_quadix(quadix[0].x, quadix[0].y, quadix[1].x, quadix[1].y, quadix[2].x,
284  quadix[2].y, quadix[3].x, quadix[3].y);
285  }
286 
287  inline void draw(const circle<T>& circle) const {
288  draw_circle(circle.x, circle.y, circle.radius);
289  }
290 
291  inline void draw(const polygon<T, 2>& polygon) const {
292  if (polygon.size() < 3) return;
293 
294  std::size_t j = polygon.size() - 1;
295 
296  for (std::size_t i = 0; i < polygon.size(); ++i) {
297  draw_segment(polygon[i], polygon[j]);
298 
299  j = i;
300  }
301  }
302 
303  inline void draw(const cubic_bezier<T, 2>& bezier,
304  const std::size_t& point_count) const {
305  std::vector<point2d<T> > point_list;
306  point_list.reserve(point_count);
307 
308  wykobi::generate_bezier(bezier, std::back_inserter(point_list),
309  point_count);
310 
311  draw_polyline(point_list);
312  }
313 
314  inline void draw(const quadratic_bezier<T, 2>& bezier,
315  const std::size_t& point_count) const {
316  std::vector<point2d<T> > point_list;
317  point_list.reserve(point_count);
318 
319  wykobi::generate_bezier(bezier, std::back_inserter(point_list),
320  point_count);
321 
322  draw_polyline(point_list);
323  }
324 
325  template <typename InputIterator>
326  inline void draw(const InputIterator begin, const InputIterator end) const {
327  for (InputIterator it = begin; it != end; ++it) {
328  draw(*it);
329  }
330  }
331 
332  const static unsigned int clAqua = 0;
333  const static unsigned int clBlack = 1;
334  const static unsigned int clBlue = 2;
335  const static unsigned int clBrown = 3;
336  const static unsigned int clCyan = 4;
337  const static unsigned int clGray = 5;
338  const static unsigned int clGreen = 6;
339  const static unsigned int clIndigo = 7;
340  const static unsigned int clLimeGreen = 8;
341  const static unsigned int clMagenta = 9;
342  const static unsigned int clOrange = 10;
343  const static unsigned int clPurple = 11;
344  const static unsigned int clRed = 12;
345  const static unsigned int clViolet = 13;
346  const static unsigned int clWhite = 14;
347  const static unsigned int clYellow = 15;
348 
349  private:
350  unsigned int width_;
351  unsigned int height_;
352 
353  gcroot<Graphics ^> gc_;
354  gcroot<Pen ^> pen_;
355 };
356 
357 } // namespace wykobi
358 
359 #endif
wykobi::segment
Definition: wykobi.hpp:263
wykobi::wykobi_graphics_net::width
unsigned int width() const
Definition: wykobi_graphics_net.hpp:119
wykobi::circle::radius
T radius
Definition: wykobi.hpp:428
wykobi::quadratic_bezier
Definition: wykobi.hpp:468
wykobi::wykobi_graphics_net::draw
void draw(const quadix< T, 2 > &quadix) const
Definition: wykobi_graphics_net.hpp:282
wykobi::wykobi_graphics_net::draw
void draw(const triangle< T, 2 > &triangle) const
Definition: wykobi_graphics_net.hpp:272
wykobi::wykobi_graphics_net::draw_pixel
void draw_pixel(const point2d< T > &point) const
Definition: wykobi_graphics_net.hpp:128
wykobi::circle
Definition: wykobi.hpp:426
wykobi::polygon
Definition: wykobi.hpp:383
wykobi::wykobi_graphics_net::~wykobi_graphics_net
~wykobi_graphics_net()
Definition: wykobi_graphics_net.hpp:46
wykobi::wykobi_graphics_net::draw
void draw(const point2d< T > &point) const
Definition: wykobi_graphics_net.hpp:264
wykobi::wykobi_graphics_net::draw_polyline
void draw_polyline(const std::vector< point3d< T > > &point_list) const
Definition: wykobi_graphics_net.hpp:193
wykobi::rectangle
Definition: wykobi.hpp:335
wykobi::wykobi_graphics_net::get_pen_width
float get_pen_width() const
Definition: wykobi_graphics_net.hpp:50
wykobi::polygon::size
std::size_t size() const
Definition: wykobi.hpp:412
wykobi::wykobi_graphics_net
Definition: wykobi_graphics_net.hpp:38
wykobi::wykobi_graphics_net::center_x
unsigned int center_x() const
Definition: wykobi_graphics_net.hpp:121
wykobi::wykobi_graphics_net::set_nodash_mode
void set_nodash_mode() const
Definition: wykobi_graphics_net.hpp:61
wykobi::point2d::y
T y
Definition: wykobi.hpp:104
wykobi::wykobi_graphics_net::wykobi_graphics_net
wykobi_graphics_net(Graphics ^ gc, const unsigned int &w, const unsigned int &h)
Definition: wykobi_graphics_net.hpp:40
wykobi::wykobi_graphics_net::draw
void draw(const InputIterator begin, const InputIterator end) const
Definition: wykobi_graphics_net.hpp:326
wykobi::wykobi_graphics_net::clear
void clear(System::Drawing::Color color) const
Definition: wykobi_graphics_net.hpp:204
wykobi::wykobi_graphics_net::height
unsigned int height() const
Definition: wykobi_graphics_net.hpp:120
wykobi::circle::x
T x
Definition: wykobi.hpp:428
wykobi::wykobi_graphics_net::draw
void draw(const segment< T, 2 > &segment) const
Definition: wykobi_graphics_net.hpp:268
wykobi::point2d
Definition: wykobi.hpp:74
wykobi::wykobi_graphics_net::set_color
void set_color(const unsigned int &color) const
Definition: wykobi_graphics_net.hpp:66
wykobi::circle::y
T y
Definition: wykobi.hpp:428
wykobi::wykobi_graphics_net::draw_polyline
void draw_polyline(const std::vector< point2d< T > > &point_list) const
Definition: wykobi_graphics_net.hpp:187
wykobi::wykobi_graphics_net::draw_circle
void draw_circle(const T &x, const T &y, const T &radius) const
Definition: wykobi_graphics_net.hpp:178
wykobi::wykobi_graphics_net::set_dash_mode
void set_dash_mode() const
Definition: wykobi_graphics_net.hpp:56
wykobi.hpp
wykobi::wykobi_graphics_net::center_y
unsigned int center_y() const
Definition: wykobi_graphics_net.hpp:122
wykobi::wykobi_graphics_net::draw
void draw(const polygon< T, 2 > &polygon) const
Definition: wykobi_graphics_net.hpp:291
wykobi::quadix
Definition: wykobi.hpp:359
wykobi::triangle
Definition: wykobi.hpp:311
wykobi::wykobi_graphics_net::set_pen_width
void set_pen_width(const unsigned int &w) const
Definition: wykobi_graphics_net.hpp:52
wykobi::wykobi_graphics_net::draw
void draw(const quadratic_bezier< T, 2 > &bezier, const std::size_t &point_count) const
Definition: wykobi_graphics_net.hpp:314
wykobi::wykobi_graphics_net::draw_quadix
void draw_quadix(const T &x1, const T &y1, const T &x2, const T &y2, const T &x3, const T &y3, const T &x4, const T &y4) const
Definition: wykobi_graphics_net.hpp:169
wykobi::wykobi_graphics_net::draw_circle
void draw_circle(const point2d< T > center, const T &radius) const
Definition: wykobi_graphics_net.hpp:183
wykobi::wykobi_graphics_net::draw
void draw(const circle< T > &circle) const
Definition: wykobi_graphics_net.hpp:287
wykobi
Definition: wykobi.hpp:32
wykobi::wykobi_graphics_net::draw
void draw(const rectangle< T > &rectangle) const
Definition: wykobi_graphics_net.hpp:277
wykobi::point2d::x
T x
Definition: wykobi.hpp:104
wykobi::cubic_bezier
Definition: wykobi.hpp:493
wykobi::wykobi_graphics_net::draw_line
void draw_line(const T &x1, const T &y1, const T &x2, const T &y2) const
Definition: wykobi_graphics_net.hpp:142
wykobi::wykobi_graphics_net::draw_triangle
void draw_triangle(const T &x1, const T &y1, const T &x2, const T &y2, const T &x3, const T &y3) const
Definition: wykobi_graphics_net.hpp:156
wykobi::wykobi_graphics_net::clear
void clear(const unsigned int &color) const
Definition: wykobi_graphics_net.hpp:208
wykobi::wykobi_graphics_net::draw_pixel
void draw_pixel(const T &x, const T &y) const
Definition: wykobi_graphics_net.hpp:124
wykobi::wykobi_graphics_net::draw_crosshair
void draw_crosshair(const point2d< T > &p, const T r) const
Definition: wykobi_graphics_net.hpp:199
wykobi::generate_bezier
void generate_bezier(const quadratic_bezier< T, 2 > &bezier, OutputIterator out, const std::size_t &point_count=1000)
wykobi::wykobi_graphics_net::set_pen
void set_pen(Pen ^ _pen) const
Definition: wykobi_graphics_net.hpp:48
wykobi::wykobi_graphics_net::draw_segment
void draw_segment(const T &x1, const T &y1, const T &x2, const T &y2) const
Definition: wykobi_graphics_net.hpp:132
wykobi::point3d
Definition: wykobi.hpp:108
wykobi::wykobi_graphics_net::draw
void draw(const cubic_bezier< T, 2 > &bezier, const std::size_t &point_count) const
Definition: wykobi_graphics_net.hpp:303
wykobi::wykobi_graphics_net::clear
void clear() const
Definition: wykobi_graphics_net.hpp:206
wykobi::wykobi_graphics_net::draw_rectangle
void draw_rectangle(const T &x1, const T &y1, const T &x2, const T &y2) const
Definition: wykobi_graphics_net.hpp:163
wykobi::wykobi_graphics_net::draw_segment
void draw_segment(const point2d< T > &point1, const point2d< T > &point2) const
Definition: wykobi_graphics_net.hpp:137