回答編集履歴
1
あ
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
def calc_points(x1, y1, x2, y2):
|
27
|
+
def calc_points(x1, y1, x2, y2, ax):
|
28
28
|
|
29
29
|
'''ピクセル (x1, y1) と ピクセル (x2, y2) が通るピクセルの一覧を返す
|
30
30
|
|
@@ -32,45 +32,45 @@
|
|
32
32
|
|
33
33
|
points = []
|
34
34
|
|
35
|
+
|
36
|
+
|
37
|
+
# ピクセル座標を実2次元座標に変換する。
|
38
|
+
|
39
|
+
# 例: ピクセル (1, 1) から (3, 2) へ線を引く場合、実2次元座標で点 (1.5, 1.5) から
|
40
|
+
|
41
|
+
# (3.5, 2.5) を結ぶ線とする。
|
42
|
+
|
35
43
|
x1, y1 = x1 + 0.5, y1 + 0.5 # ピクセルの中心
|
36
44
|
|
37
45
|
x2, y2 = x2 + 0.5, y2 + 0.5 # ピクセルの中心
|
38
46
|
|
47
|
+
|
48
|
+
|
49
|
+
# 初期化
|
50
|
+
|
39
51
|
x, y = x1, y1
|
40
52
|
|
53
|
+
cell_w, cell_h = 1, 1 # セルの大きさ、今回はピクセルなので (1, 1)
|
41
54
|
|
55
|
+
step_x = np.sign(x2 - x1) # (x1, y1) から (x2, y2) へ進むときの x 方向のステップ数
|
42
56
|
|
43
|
-
cell_w, cell_h = 1, 1
|
44
|
-
|
45
|
-
step_x = np.sign(x2 - x1)
|
46
|
-
|
47
|
-
step_y = np.sign(y2 - y1)
|
57
|
+
step_y = np.sign(y2 - y1) # (x1, y1) から (x2, y2) へ進むときの y 方向のステップ数
|
48
58
|
|
49
59
|
delta_x = cell_w / abs(x2 - x1)
|
50
60
|
|
51
61
|
delta_y = cell_h / abs(y2 - y1)
|
52
62
|
|
63
|
+
# a / b % 1 は a / b の計算値の小数部分 (例: 3 / 2 % 1 = 1.5 % 1 = 0.5)
|
53
64
|
|
65
|
+
max_x = delta_x * (x1 / cell_w % 1)
|
54
66
|
|
55
|
-
tmp = x1 / cell_w
|
56
|
-
|
57
|
-
max_x = delta_x * (1.0 - (tmp - int(tmp)))
|
58
|
-
|
59
|
-
tmp = y1 / cell_h
|
60
|
-
|
61
|
-
max_y = delta_y * (1
|
67
|
+
max_y = delta_y * (y1 / cell_h % 1)
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
#print('delta_x: {}, step_x: {}'.format(delta_x, step_x)) # delta_x: 0.2, step_x: 1
|
66
|
-
|
67
|
-
#print('delta_x: {}, step_x: {}'.format(delta_y, step_y)) # delta_x: 0.2, step_x: 1
|
68
68
|
|
69
69
|
|
70
70
|
|
71
71
|
points.append([x, y]) # 開始点
|
72
72
|
|
73
|
-
reached_x, reached_y = False, False
|
73
|
+
reached_x, reached_y = False, False # 到達判定用フラグ
|
74
74
|
|
75
75
|
while not (reached_x and reached_y):
|
76
76
|
|
@@ -85,6 +85,8 @@
|
|
85
85
|
max_y += delta_y
|
86
86
|
|
87
87
|
y += step_y
|
88
|
+
|
89
|
+
|
88
90
|
|
89
91
|
points.append([x, y]) # 点を追加
|
90
92
|
|
@@ -114,64 +116,6 @@
|
|
114
116
|
|
115
117
|
```python
|
116
118
|
|
117
|
-
import matplotlib.pyplot as plt
|
118
|
-
|
119
|
-
from matplotlib.lines import Line2D
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
def show_image(ax, img):
|
124
|
-
|
125
|
-
ax.imshow(img, interpolation='none')
|
126
|
-
|
127
|
-
ax.grid(which='minor', color='b', linestyle='-', linewidth=1)
|
128
|
-
|
129
|
-
# Major ticks
|
130
|
-
|
131
|
-
ax.set_xticks(np.arange(0, 10, 1))
|
132
|
-
|
133
|
-
ax.set_yticks(np.arange(0, 10, 1))
|
134
|
-
|
135
|
-
# Labels for major ticks
|
136
|
-
|
137
|
-
ax.set_xticklabels(np.arange(10))
|
138
|
-
|
139
|
-
ax.set_yticklabels(np.arange(10))
|
140
|
-
|
141
|
-
# Minor ticks
|
142
|
-
|
143
|
-
ax.set_xticks(np.arange(-.5, 10, 1), minor=True)
|
144
|
-
|
145
|
-
ax.set_yticks(np.arange(-.5, 10, 1), minor=True)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
def draw_line(img, p1, p2, color=[255, 0, 0]):
|
150
|
-
|
151
|
-
x1, y1 = p1
|
152
|
-
|
153
|
-
x2, y2 = p2
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
slope = (y2 - y1) / (x2 - x1)
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
xs = np.arange(x1, x2 + 1, 0.01)
|
162
|
-
|
163
|
-
ys = y1 + slope * (xs - x1)
|
164
|
-
|
165
|
-
pts = np.vstack([xs, ys]).T
|
166
|
-
|
167
|
-
print(pts.shape)
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
pts = np.ceil(pts).astype(int) # 離散化する。
|
172
|
-
|
173
|
-
pts = np.unique(pts, axis=0) # 重複する点を削除
|
174
|
-
|
175
119
|
|
176
120
|
|
177
121
|
# 白紙の画像を作成する。
|
@@ -182,27 +126,65 @@
|
|
182
126
|
|
183
127
|
# 点が通るピクセル一覧を計算する。
|
184
128
|
|
185
|
-
x1, y1 =
|
129
|
+
x1, y1 = 8, 5
|
186
130
|
|
187
|
-
x2, y2 =
|
131
|
+
x2, y2 = 1, 1
|
188
132
|
|
189
133
|
|
190
134
|
|
135
|
+
# 点を計算する。
|
136
|
+
|
191
|
-
points = calc_points(x1, y1, x2, y2)
|
137
|
+
points = calc_points(x1, y1, x2, y2, ax)
|
138
|
+
|
139
|
+
print(points)
|
192
140
|
|
193
141
|
for x, y in points:
|
194
142
|
|
195
143
|
img[y, x] = [255, 0, 0] # 赤
|
196
144
|
|
145
|
+
|
197
146
|
|
147
|
+
# 描画部分
|
148
|
+
|
149
|
+
##################################################
|
150
|
+
|
151
|
+
import matplotlib.pyplot as plt
|
152
|
+
|
153
|
+
from matplotlib.lines import Line2D
|
198
154
|
|
199
155
|
fig, ax = plt.subplots(figsize=(8, 8), facecolor='w')
|
200
156
|
|
157
|
+
|
158
|
+
|
159
|
+
# 画像を表示する。
|
160
|
+
|
161
|
+
ax.imshow(img, interpolation='none')
|
162
|
+
|
163
|
+
# 線を描画する。
|
164
|
+
|
201
165
|
ax.add_line(Line2D([x1, x2], [y1, y2], color='g'))
|
202
166
|
|
203
|
-
|
167
|
+
# グリッド
|
204
168
|
|
169
|
+
ax.grid(which='minor', color='b', linestyle='-', linewidth=1)
|
170
|
+
|
171
|
+
# x、y 軸の目盛りのラベル
|
172
|
+
|
173
|
+
ax.set_xticks(np.arange(0, 10, 1))
|
174
|
+
|
175
|
+
ax.set_yticks(np.arange(0, 10, 1))
|
176
|
+
|
205
|
-
|
177
|
+
ax.set_xticklabels(np.arange(10))
|
178
|
+
|
179
|
+
ax.set_yticklabels(np.arange(10))
|
180
|
+
|
181
|
+
# x、y 軸の目盛りの位置
|
182
|
+
|
183
|
+
ax.set_xticks(np.arange(-.5, 10, 1), minor=True)
|
184
|
+
|
185
|
+
ax.set_yticks(np.arange(-.5, 10, 1), minor=True)
|
186
|
+
|
187
|
+
plt.show()
|
206
188
|
|
207
189
|
```
|
208
190
|
|