Skip to content

Instantly share code, notes, and snippets.

@fhs
Created October 28, 2011 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhs/1321619 to your computer and use it in GitHub Desktop.
Save fhs/1321619 to your computer and use it in GitHub Desktop.
shapes
// 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;
}
// 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