-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriangle.cpp
89 lines (81 loc) · 2.18 KB
/
triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int row,column,graphHeight,graphWidth,step;
double base1,altitude1;
char triangle[130][130];
cout<<"Enter graph height: ";
cin>>graphHeight;
cout<<"Enter graph width: ";
cin>>graphWidth;
cout<<"Enter base and altitude of triangle such that base is smaller than altitude: ";
cin>>base1>>altitude1;
if(altitude1 > base1)
{
double unit = double(altitude1/(base1/2));
step = unit + .5;
}
else
{
cout<<"sorry,base is either greater than or equals to altitude.\n";
exit (0);
}
double hypotenuse1 = sqrt((altitude1/2) * (altitude1/2) + (base1/2) * (base1/2)) + 0.5;
int hypotenuse = hypotenuse1;
int altitude = altitude1;
int base = base1;
//define graph
for(row = 0; row <= graphHeight; row++)
{
for(column = 0; column <= graphWidth; column++)
{
triangle[row][column] = ' ';
}
}
// Y-axis
for(int y = 0; y <= graphHeight; y++)
{
int x = graphWidth/2;
triangle[y][x] = '|';
}
// X-axis
for(int x = 0; x <= graphWidth; x++)
{
int y = graphHeight/2;
triangle[y][x] = '_';
}
//base of triangle
int k = graphHeight/2 + altitude/2;
for(int row = (graphWidth/2 - base/2);
row < (graphWidth/2 - base/2) + base;
row++)
{
triangle[k][row] = '.';
}
//side 1 of triangle
for(int p = (graphHeight/2 - altitude/2), q = graphWidth/2;
p <= (graphHeight/2 - altitude/2) + hypotenuse, q > graphWidth/2 - base/2;
p += step, q--)
{
triangle[p][q] = '.';
}
//side 2 of triangle
for(int p = (graphHeight/2 - altitude/2), q = graphWidth/2;
p <= (graphHeight/2 - altitude/2) + hypotenuse, q < graphWidth/2 + base/2;
p += step, q++)
{
triangle[p][q] = '.';
}
// print triangle
for(row = 0; row <= graphHeight; row++)
{
for(column = 0; column <= graphWidth; column++)
{
cout<<triangle[row][column];
}
cout<<endl;
}
}