Official
C - 円の描画/Drawing Circle Editorial by cn449
三平方の定理より、一般に \(2\) 次元平面上の \(2\) 点 \((x_1, y_1)\) と \((x_2, y_2)\) の距離は \(\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}\) で表されます。
したがって、点 \((i,j)\) が点 \((X,Y)\) を中心とする半径 \(R\) の円の内部あるいは周上にあることと \(\sqrt{(X-i)^2+(Y-j)^2} \leq R\) は同値です。したがって、各 \((i,j)\) に対してこの条件、あるいは両辺を \(2\) 乗した \((X-i)^2+(Y-j)^2 \leq R^2\) という条件を用いて判定していくことでこの問題を解くことができます。
実装例
#include <iostream>
using namespace std;
int main() {
int x, y, r, n;
cin >> x >> y >> r >> n;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++) {
if ((x - i) * (x - i) + (y - j) * (y - j) <= r * r) cout << '#';
else cout << '.';
if (j != n) cout << ' ';
}
cout << '\n';
}
}
posted:
last update: