Hexagon Cell  v.1.1.1
Marker.cpp
1 #include "Marker.h"
2 #include "Cell.h"
3 
4 namespace HexagonCells
5 {
6  void CMarker::markGrid(Mat &img, double R, CvScalar color)
7  {
8  int x, y;
9  double X, Y;
10  double X0;
11 
12  double r = sqrtf(3) * 0.5 * R;
13  double dx = 2 * r;
14  double dy = 1.5 * R;
15  double S = dx * dy;
16 
17  //printf("S = %.2f (pixels);\n", S);
18 
19  if (img.channels() == 1) cvtColor(img, img, CV_GRAY2RGB);
20 
21  X = 0; Y = 0;
22  for (y = 0; Y < img.rows; y++) {
23  if (y % 2 == 0) X0 = r;
24  else X0 = 0;
25  X = 0;
26  for (x = 0; X < img.cols; x++) {
27  X = X0 + x * dx;
28  Y = R / 2 + y * dy;
29  //circle(img, cvPoint(static_cast<int>(X), static_cast<int>(Y)), 1, color);
30 
31  line(img, Point2d(X, Y - R), Point2d(X + r, Y - (R / 2)), color, 1, CV_AA);
32  line(img, Point2d(X + r, Y - (R / 2)), Point2d(X + r, Y + (R / 2)), color, 1, CV_AA);
33  line(img, Point2d(X, Y + R), Point2d(X + r, Y + (R / 2)), color, 1, CV_AA);
34  } // x
35  } // y
36  rectangle(img, Point(0, 0), Point(img.cols - 1, img.rows - 1), color, 1);
37  }
38 
39  void CMarker::markHexagon(Mat &img, double R, int idx, CvScalar color)
40  {
41  // Assertions
42  //if (img.channels() != 3) return;
43 
44  // cell coordinates in image
45  CvPoint2D64f C = CCell::idx2d(idx, R, img.size());
46  Point ic[6];
47  for (int i = 0; i < 6; i++) {
48  CvPoint2D64f c = CCell::getBoundaryPoint(C, i, R);
49  ic[i].x = static_cast<int>(0.5 + c.x);
50  ic[i].y = static_cast<int>(0.5 + c.y);
51  }
52 
53  const Point * countours[1] = { &ic[0] };
54  int npt[] = { 6 };
55  fillPoly(img, countours, npt, 1, color);
56  }
57 }
void markGrid(Mat &img, double R, CvScalar color)
Draws the hexagonical grid on the image.
Definition: Marker.cpp:6
void markHexagon(Mat &img, double R, int idx, CvScalar color)
Draws a single filled hexagon.
Definition: Marker.cpp:39