[try Beta version]
Not logged in

 
Question with drawing right triangles

Feb 12, 2013 at 2:25am
I'm trying to draw a right triangle to the console using asterisks. I've found several examples of how to do this but these examples seem to assume the values for the triangle are fixed or are the same.

I've created a program that takes two values from the user and calculates the hypotenuse and area, but I'm struggling with actually drawing the triangle. This is especially the case when entering values that differ a lot like 4 and 12. Can anyone point me to a working example of this or have some code that shows the loop I need?
Feb 12, 2013 at 10:57am
this code uses the identity of similar triangles.
of course the triangle doesn't look that well but that is because you're working with asterisks and not pixel by pixel.

and now just add cin>>height and cin>>width and there you go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include<iostream>
#include<cmath>

using namespace std;

int main() {
	double height=30,width=12;

	for (double i=1;i<=height;i++) {
		for (double j=1;j<=width*(i/height);j++) {
			cout<<"*";
		}
	cout<<endl;
	}
	system("pause");
}
Last edited on Feb 12, 2013 at 11:05am
Topic archived. No new replies allowed.