9 #ifndef INC_POLYMNIA_HEX_H__ 10 #define INC_POLYMNIA_HEX_H__ 39 static inline const double SQRT3_ = std::sqrt(3);
46 HexPainter(
int r,
int p0,
int q0) : r_(r), p0_(p0), q0_(q0) {}
143 p = SQRT3_ * r_ * (x + 0.5 * (y % 2)) + p0_;
144 q = (3.0 * y * r_) / 2.0 + q0_;
156 p = SQRT3_ * r_ * (x + 0.5 * (y % 2)) + p0_;
157 q = (3.0 * y * r_) / 2.0 + q0_;
173 int a = std::abs(x1 - x0 - y1_2 + y0_2);
174 int b = std::abs(y1 - y0);
175 int c = std::abs(x1 + y1 - y1_2 - x0 - y0 + y0_2);
177 return std::max({a, b, c});
193 int& p1,
int& p2,
int& q1,
int& q2,
int& q3,
int &q4,
204 p1 = p - SQRT3_ * r_ / 2.0;
205 p2 = p + SQRT3_ * r_ / 2.0;
232 getPixelPosition(p, q, x, y);
236 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
240 pict->
line(p1, q2, p, q1, col);
241 pict->
line(p1, q2, p1, q3, col);
242 pict->
line(p1, q3, p, q4, col);
244 pict->
line(p2, q2, p, q1, col);
245 pict->
line(p2, q2, p2, q3, col);
246 pict->
line(p2, q3, p, q4, col);
258 int p1, p2, q1, q2, q3, q4;
260 for (
int j = 0; j < h; j++) {
263 if ((y + j) % 2 == 0) {
264 getPixelPosition(p, q, x, y + j);
265 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
266 pict->
line(p1, q3, p, q4, col);
270 for (
int i = 0; i < w; i++) {
271 getPixelPosition(p, q, x + i, y + j);
272 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
274 pict->
line(p1, q2, p, q1, col);
275 pict->
line(p1, q2, p1, q3, col);
276 pict->
line(p2, q2, p, q1, col);
280 getPixelPosition(p, q, x + w - 1, y + j);
281 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
282 pict->
line(p2, q2, p2, q3, col);
283 if ((y + j) % 2 == 1) {
284 pict->
line(p2, q3, p, q4, col);
289 for (
int i = 0; i < w; i++) {
290 getPixelPosition(p, q, x + i, y + h - 1);
291 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
292 pict->
line(p1, q3, p, q4, col);
293 pict->
line(p2, q3, p, q4, col);
305 fillLeftHalf_(pict, x, y, col);
306 fillRightHalf_(pict, x, y, col);
317 getPixelPosition(p, q, x, y);
320 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
323 int dp = (int)p - p1;
336 int lx = (dp + 1) / 2;
337 for (
int i = 0; i < lx; i++) {
338 pict->
line(pp1, qq1, pp1, qq3, col);
339 pict->
line(pp2, qq2, pp2, qq4, col);
358 pict->
line(pp1, qq1, pp1, qq3, col);
369 getPixelPosition(p, q, x, y);
372 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
375 int dp = p2 - (int)p;
387 int lx = (dp + 1) / 2;
388 for (
int i = 0; i < lx; i++) {
389 pict->
line(pp1, qq1, pp1, qq3, col);
390 pict->
line(pp2, qq2, pp2, qq4, col);
409 pict->
line(pp1, qq1, pp1, qq3, col);
427 int pbox0, qbox0, pbox1, qbox1;
429 getPixelPosition(p, q, x, y);
430 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
434 getPixelPosition(p, q, x + w - 1, y);
435 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
438 getPixelPosition(p, q, x + w - 1, y + h - 1);
439 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
442 getPixelPosition(p, q, x, y);
443 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
447 getPixelPosition(p, q, x + w - 1, y);
448 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
451 getPixelPosition(p, q, x + w - 1, y + h - 1);
452 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
455 pict->
box(pbox0, qbox0, pbox1, qbox1, col,
true);
458 for (
int j = 0; j < h; j++) {
459 if ((y + j) % 2 == 0) {
460 fillLeftHalf_(pict, x, y + j, col);
465 for (
int j = 0; j < h; j++) {
466 if ((y + j) % 2 != 0) {
467 fillRightHalf_(pict, x + w - 1, y + j, col);
471 for (
int i = 0; i < w; i++) {
473 int pp1, pp2, qq1, qq2;
477 getPixelPosition(p, q, x + i, y);
478 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
491 for (
int i = 0; i < lx; i++) {
492 pict->
line(pp1, qq1, pp1, q2, col);
493 pict->
line(pp2, qq2, pp2, q2, col);
505 pict->
line(pp1, qq1, pp1, q2, col);
517 for (
int i = 0; i < lx; i++) {
518 pict->
line(pp1, qq1, pp1, q2, col);
519 pict->
line(pp2, qq2, pp2, q2, col);
532 pict->
line(pp1, qq1, pp1, q2, col);
536 getPixelPosition(p, q, x + i, y + h - 1);
537 calcVertex_(p1, p2, q1, q2, q3, q4, p, q);
550 for (
int i = 0; i < lx; i++) {
551 pict->
line(pp1, q3, pp1, qq1, col);
552 pict->
line(pp2, q3, pp2, qq2, col);
565 pict->
line(pp1, q3, pp1, qq1, col);
577 for (
int i = 0; i < lx; i++) {
578 pict->
line(pp1, q3, pp1, qq1, col);
579 pict->
line(pp2, q3, pp2, qq2, col);
592 pict->
line(pp1, q3, pp1, qq1, col);
619 int y0 = std::floor(qq / 3.0 / r_) * 2;
620 double dq = qq - (y0 / 2) * 3 * r_;
623 int x0 = std::floor(pp * SQRT3_ / 3.0 / r_);
624 double dp = pp - x0 * SQRT3_ * r_;
626 double u = -1 * dp * SQRT3_ / 3;
627 double v = dp * SQRT3_ / 3;
629 if ((dq < u + r_) || (dq < v)) {
630 if (dp < SQRT3_ * r_ / 2) {
637 else if ((dq >= u + 3 * r_) || (dq >= v + 2 * r_)) {
638 if (dp < SQRT3_ * r_ / 2) {
654 #endif // INC_POLYMNIA_HEX_H__ HexPainter(int r, int p0, int q0)
構築子
Definition: hex.h:46
void getOrigin(int &p, int &q) const
原點の取得
Definition: hex.h:70
void fill(polymnia::ImageBuffer< C_ > *pict, int x, int y, const C_ &col)
HEXの塗り潰し
Definition: hex.h:302
void draw(polymnia::ImageBuffer< C_ > *pict, int x, int y, const C_ &col)
HEX外周の描畫
Definition: hex.h:228
int distance(int x0, int y0, int x1, int y1)
距離の取得
Definition: hex.h:163
void resetRadius(int r)
HEXの大きさの再設定
Definition: hex.h:63
void getPixelPosition(double &p, double &q, int x, int y)
ピクセル位置の取得
Definition: hex.h:154
void line(int x1, int y1, int x2, int y2, const C_ &col)
直線を描畫
Definition: ibuf-imp_.h:222
正六角形マス描畫クラス
Definition: hex.h:18
void getHexPosition(int &x, int &y, int p, int q)
HEX位置の取得
Definition: hex.h:604
void box(int x1, int y1, int x2, int y2, const C_ &col, bool fl=false)
長方形を描畫
Definition: ibuf-imp_.h:353
int getRadius() const
HEXの大きさの取得
Definition: hex.h:79
void getPixelPosition(int &p, int &q, int x, int y)
ピクセル座標の取得
Definition: hex.h:141
畫像バッファ基底クラステンプレート
Definition: ibuf.h:23
void resetOrigin(int p0, int q0)
原點の再設定
Definition: hex.h:53