1. 程式人生 > >C/C++ - 控制臺畫圓

C/C++ - 控制臺畫圓

ace char sqrt return oid params 圖片 平面 ios

  書上看到一個畫空心圓的題,於是用C++寫了一個實心圓...

#include <iostream>
#include <math.h>

using namespace std;

class DrawCircle {
public:
	void drawCircle(int r) {
		int x, y;
		int params_1 = 2;
		int params_2 = 5;

		for (y = 0; y < r; y++) {
			x = -(int)sqrt(2 * y * r - y * y) + r;
			for (int i = 0; i < params_1*x; i++)
				cout << ‘ ‘;
			cout << ‘*‘;
			for (int i = ((int)sqrt(2 * y * r - y * y) + r - x)*params_1 + params_2; i >= 0; i--)
				cout << (i % 2 == 0 ? ‘*‘ : i % 3 == 0 ? ‘$‘ : ‘%‘);
			cout << ‘*‘ << endl;
		}

		for (; y >= 0; y--) {
			x = -(int)sqrt(2 * y * r - y * y) + r;
			for (int i = 0; i < params_1*x; i++)
				cout << ‘ ‘;
			cout << ‘*‘;
			for (int i = ((int)sqrt(2 * y * r - y * y) + r - x)*params_1 + params_2; i >= 0; i--)
				cout << (i % 2 == 0 ? ‘*‘ : i % 3 == 0 ? ‘$‘ : ‘%‘);
			cout << ‘*‘ << endl;
		}
	}
};

int main()
{
	DrawCircle dc;
	int r = 12;

	dc.drawCircle(r);
	getchar();
    return 0;
}

  運行結果:

技術分享圖片

  = =...很菜,只能畫這種單調的平面實心圓了,知道圓的方程就行了。

C/C++ - 控制臺畫圓