MicroMouse Maze Library  3703225
公開メンバ関数 | 静的公開メンバ関数 | 公開変数類 | 静的公開変数類 | フレンド | 全メンバ一覧
MazeLib::Position 構造体

迷路の区画の位置(座標)を定義。 [詳解]

#include <Maze.h>

公開メンバ関数

constexpr Position ()
 ゼロ初期化のデフォルトコンストラクタ [詳解]
 
constexpr Position (const int8_t x, const int8_t y)
 コンストラクタ [詳解]
 
uint16_t getIndex () const
 迷路内の区画の一意な通し番号となるIDを取得する [詳解]
 
Position operator+ (const Position p) const
 加法 [詳解]
 
Position operator- (const Position p) const
 減法 [詳解]
 
bool operator== (const Position p) const
 等号 [詳解]
 
bool operator!= (const Position p) const
 等号否定 [詳解]
 
Position next (const Direction d) const
 自分の引数方向に隣接した区画の Position を返す [詳解]
 
bool isInsideOfField () const
 フィールド内かどうかを判定する関数 [詳解]
 
Position rotate (const Direction d) const
 座標を回転変換する [詳解]
 
Position rotate (const Direction d, const Position center) const
 座標を回転変換する [詳解]
 
const char * toString () const
 表示用文字列に変換する [詳解]
 

静的公開メンバ関数

static Position getPositionFromIndex (const uint16_t index)
 IDからPositionを作成する関数 [詳解]
 

公開変数類

union {
   struct {
      int8_t   x
 迷路区画のx座標成分 [詳解]
 
      int8_t   y
 迷路区画のy座標成分 [詳解]
 
   } 
 
   uint16_t   data
 データ全体へのアクセス用 [詳解]
 
}; 
 

静的公開変数類

static constexpr int SIZE = MAZE_SIZE_MAX * MAZE_SIZE_MAX
 フィールドの区画数。配列確保などで使える。 [詳解]
 

フレンド

std::ostream & operator<< (std::ostream &os, const Position p)
 output-stream の表示関数。 ( x, y) の形式 [詳解]
 

詳解

迷路の区画の位置(座標)を定義。

実体は 16bit の整数。 左下の区画が (0,0) の (x,y) 平面。

+--------+--------+
| (0, 1) | (1, 1) |
+--------+--------+
| (0, 0) | (1, 0) |
+--------+--------+

構築子と解体子

◆ Position() [1/2]

constexpr MazeLib::Position::Position ( )
inlineconstexpr

ゼロ初期化のデフォルトコンストラクタ

286 : data(0) {}
uint16_t data
データ全体へのアクセス用
Definition: Maze.h:279

◆ Position() [2/2]

constexpr MazeLib::Position::Position ( const int8_t  x,
const int8_t  y 
)
inlineconstexpr

コンストラクタ

引数
x,y初期化パラメータ
291 : x(x), y(y) {}
int8_t x
迷路区画のx座標成分
Definition: Maze.h:276
int8_t y
迷路区画のy座標成分
Definition: Maze.h:277

関数詳解

◆ getIndex()

uint16_t MazeLib::Position::getIndex ( ) const
inline

迷路内の区画の一意な通し番号となるIDを取得する

迷路外の区画の場合未定義動作となる。 Position::isInsideOfField() を使って迷路区画内であることを確認すること。

戻り値
uint16_t 通し番号ID
298 { return (x << MAZE_SIZE_BIT) | y; }
static constexpr int MAZE_SIZE_BIT
迷路の1辺の区画数の bit 数。bit shift などに用いる。
Definition: Maze.h:110

◆ getPositionFromIndex()

static Position MazeLib::Position::getPositionFromIndex ( const uint16_t  index)
inlinestatic

IDからPositionを作成する関数

引数
index通し番号 ID
303  {
304  return {int8_t(index >> MAZE_SIZE_BIT),
305  int8_t(index & (MAZE_SIZE_MAX - 1))};
306  }
static constexpr int MAZE_SIZE_MAX
迷路の1辺の区画数の最大値。2のbit数乗の値。
Definition: Maze.h:114

◆ isInsideOfField()

bool MazeLib::Position::isInsideOfField ( ) const
inline

フィールド内かどうかを判定する関数

戻り値
true フィールド内
false フィールド外
336  {
337  // return x >= 0 && x < MAZE_SIZE && y >= 0 && y < MAZE_SIZE;
338  /* 高速化 */
339  return (static_cast<uint8_t>(x) < MAZE_SIZE) &&
340  (static_cast<uint8_t>(y) < MAZE_SIZE);
341  }
static constexpr int MAZE_SIZE
迷路の1辺の区画数の定数。
Definition: Maze.h:106

◆ next()

Position MazeLib::Position::next ( const Direction  d) const

自分の引数方向に隣接した区画の Position を返す

引数
d隣接方向
戻り値
隣接区画の座標
22  {
23  switch (d) {
24  case Direction::East:
25  return Position(x + 1, y);
27  return Position(x + 1, y + 1);
28  case Direction::North:
29  return Position(x, y + 1);
31  return Position(x - 1, y + 1);
32  case Direction::West:
33  return Position(x - 1, y);
35  return Position(x - 1, y - 1);
36  case Direction::South:
37  return Position(x, y - 1);
39  return Position(x + 1, y - 1);
40  default:
41  MAZE_LOGE << d << std::endl;
42  return *this;
43  }
44 }
#define MAZE_LOGE
Definition: Maze.h:78
unsigned int d
壁の方向
Definition: Maze.h:609
@ SouthEast
Definition: Maze.h:158
@ North
Definition: Maze.h:153
@ South
Definition: Maze.h:157
@ NorthWest
Definition: Maze.h:154
@ NorthEast
Definition: Maze.h:152
@ East
Definition: Maze.h:151
@ West
Definition: Maze.h:155
@ SouthWest
Definition: Maze.h:156
constexpr Position()
ゼロ初期化のデフォルトコンストラクタ
Definition: Maze.h:286
呼び出し関係図:

◆ operator!=()

bool MazeLib::Position::operator!= ( const Position  p) const
inline

等号否定

321  {
322  // return x != p.x || y != p.y;
323  return data != p.data; //< 高速化
324  }

◆ operator+()

Position MazeLib::Position::operator+ ( const Position  p) const
inline

加法

308  {
309  return Position(x + p.x, y + p.y);
310  }
呼び出し関係図:

◆ operator-()

Position MazeLib::Position::operator- ( const Position  p) const
inline

減法

312  {
313  return Position(x - p.x, y - p.y);
314  }
呼び出し関係図:

◆ operator==()

bool MazeLib::Position::operator== ( const Position  p) const
inline

等号

316  {
317  // return x == p.x && y == p.y;
318  return data == p.data; //< 高速化
319  }

◆ rotate() [1/2]

Position MazeLib::Position::rotate ( const Direction  d) const

座標を回転変換する

引数
d回転角度, 4方位のみ
戻り値
変換後の位置
45  {
46  switch (d) {
47  case Direction::East:
48  return Position(x, y);
49  case Direction::North:
50  return Position(-y, x);
51  case Direction::West:
52  return Position(-x, -y);
53  case Direction::South:
54  return Position(y, -x);
55  default:
56  MAZE_LOGE << d << std::endl;
57  return *this;
58  }
59 }
呼び出し関係図:

◆ rotate() [2/2]

Position MazeLib::Position::rotate ( const Direction  d,
const Position  center 
) const
inline

座標を回転変換する

引数
d回転角度, 4方位のみ
center回転中心座標
戻り値
変換後の位置
354  {
355  return center + (*this - center).rotate(d);
356  }
Position rotate(const Direction d) const
座標を回転変換する
Definition: Maze.cpp:45
呼び出し関係図:

◆ toString()

const char* MazeLib::Position::toString ( ) const
inline

表示用文字列に変換する

364  {
365  static char str[32];
366  snprintf(str, sizeof(str), "(%02d, %02d)", x, y);
367  return str;
368  }

フレンドと関連関数の詳解

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Position  p 
)
friend

output-stream の表示関数。 ( x, y) の形式

60  {
61  return os << "( " << std::setw(2) << +p.x << ", " << std::setw(2) << +p.y
62  << ")";
63 }

メンバ詳解

◆ 

union { ... }

◆ data

uint16_t MazeLib::Position::data

データ全体へのアクセス用

◆ SIZE

constexpr int MazeLib::Position::SIZE = MAZE_SIZE_MAX * MAZE_SIZE_MAX
staticconstexpr

フィールドの区画数。配列確保などで使える。

◆ x

int8_t MazeLib::Position::x

迷路区画のx座標成分

◆ y

int8_t MazeLib::Position::y

迷路区画のy座標成分


この構造体詳解は次のファイルから抽出されました: