Created
October 28, 2011 04:25
-
-
Save fhs/1321619 to your computer and use it in GitHub Desktop.
shapes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Draws a circle | |
#include <iostream> | |
using namespace std; | |
int | |
main(void) | |
{ | |
int r; //radius | |
// The circle is centered at the origin. | |
r = 20; | |
for(int y = r; y >= -r; y--){ | |
for(int x = -r; x <= r; x++){ | |
if(x*x + y*y <= r*r) | |
cout << '*'; | |
else | |
cout << ' '; | |
} | |
cout << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Draws an equilateral triangle | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
int | |
main(void) | |
{ | |
int n; // half of length of each sides | |
// The three corners of the triangle are at: | |
// (-n, 0), (n, 0), and (0, n*sqrt(3)) | |
// | |
// The line going from (-n, 0) to (0, n*sqrt(3)) is: | |
// y = sqrt(3)*x + sqrt(3)*n | |
// | |
// The line going from (n, 0) to (0, n*sqrt(3)) is: | |
// y = -sqrt(3)*x + sqrt(3)*n | |
n = 40/2; | |
for(int y = n*sqrt(3); y >= 0; y--){ | |
for(int x = -n; x <= n; x++){ | |
if(y <= sqrt(3)*x + sqrt(3)*n | |
&& y <= -sqrt(3)*x + sqrt(3)*n) | |
cout << '*'; | |
else | |
cout << ' '; | |
} | |
cout << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment