How to print a triangle using user-defined coordinates?

I have to write a program that takes three sets of user-given coordinates (x1, y1), (x2, y2), (x3, y3) and prints a right triangle using these coordinates on a grid. Right now I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x1, y1, x2, y2, x3, y3;

cout << "Please enter the triangle's coordinates: ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

base = x2 - x1;
height = y3 - y2;

for(int i = height; i >= 1; ++i)
    {
        for(int j = base; j >= 1; ++j)
        {
            cout << j << " ";
        }
        cout << "\n";
    }


This results in a bombardment of increasing numbers when I run the program. Can someone help point me in the right direction? I know how to print out a basic triangle using for loops but I am struggling with getting this to work with user defined coordinates.
Last edited on
closed account (48T7M4Gy)
Why not show us the whole program if it's not too long? And some sample output.

line 1: doesn't make any sense if you are declaring the variable
lines 6 and 7: base and height aren't declared
Oh wow, I can't believe I typed line 1 like that. That's not how it is in my actual program. I fixed it. Here's my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main(){

int x1, y1, x2, y2, x3, y3;

cout << "Please enter the triangle's coordinates: ";
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

int base = x2 - x1;
int height = y2 - y1;

for(int i = height; i >= 1; ++i)
    {
        for(int j = base; j >= 1; ++j)
        {
            cout << j << " ";
        }
        cout << "\n";
    }
return 0;
}


My logic is, I'm sure, way off. I hope there's enough here to work with.
What I really need help with is the for loops. I can fix my math for finding base and height, but once I do, I need a better understanding of how to work with those variables in the for loops to print out a triangle properly.
closed account (48T7M4Gy)
OK that's better. So to save us a lot of time what's say you hard code the values for the coordinates and edit that post. Just comment out the cin on line 10 - don't delete it and add a couple of lines x1 = whatever etc
Here's the updated code with hardcoded variables:


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
#include <iostream>

using namespace std;

int main(){

int x1 = 0, 
     y1 = 0, 
     x2 = 4, 
     y2 = 0, 
     x3 = 4, 
     y3 = 4;

//cout << "Please enter the triangle's coordinates: ";
// cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

int base = x2 - x1;
int height = y2 - y1;

for(int i = height; i >= 1; ++i)
    {
        for(int j = base; j >= 1; ++j)
        {
            cout << j << " ";
        }
        cout << "\n";
    }
return 0;
}
Last edited on
closed account (48T7M4Gy)
Good
closed account (48T7M4Gy)
I'm still trying to work out what you're supposed to be doing but here's a start. Look carefully at what of changed. > vs < , the values of base and height are suss too so print them out is a good move or use a debugger.

Printing blank spaces is a bad move because you don't get feedback while you're testing.

It's starting to make sense though.

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
#include <iostream>

using namespace std;

int main(){
    
    int x1 = 0,
    y1 = 0,
    x2 = 4,
    y2 = 0,
    x3 = 4,
    y3 = 4;
    
    //cout << "Please enter the triangle's coordinates: ";
    // cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    
    int base = x2 - x1;
    int height = y2 - y1;
    
    for(int i = 0; i < 5; ++i)
    {
        for(int j = 0; j < 5; ++j)
        {
            cout << i << "*";
        }
        cout << "\n";
    }
    return 0;
}
closed account (48T7M4Gy)
So far, height = 0 !!
int height = y3 - y2; !!
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main(){
    
    int x1 = 0,
    y1 = 0,
    x2 = 4,
    y2 = 0,
    x3 = 3,
    y3 = 4;
    
    //cout << "Please enter the triangle's coordinates: ";
    // cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
    
    int base = x2 - x1;
    int height = y3 - y2;
    
    cout << "base " << base << " height " << height << '\n';
    
    for(int i = 0; i < base; ++i)
    {
        for(int j = i; j < height; ++j)
        {
            cout << i << "*";
        }
        cout << "\n";
    }
    return 0;
}


base 4 height 4
0*0*0*0*
1*1*1*
2*2*
3*
Program ended with exit code: 0


This probably gives you enough to play arround with. :)
Thank you so much for your help! This has given me a great start. I will play around with the code :)
closed account (48T7M4Gy)
y1 and x3 aren't used - they would be used if the base and height aren't aligned with the x,y axes. In which case the nested loop would need to be substantially modified - best check that you are/are not required to program for that.
user-given coordinates (x1, y1), (x2, y2), (x3, y3) and prints a right triangle using these coordinates on a grid.

I'm a user. I give coordinates: 42 42 42 42 42 42
I'm disappointed, I did expect to see a triangle on the quintillion * quintillion grid.

In other words, what do we know about the grid? Barely large enough to fit the triangle?
(For my input, a 42*42 would be enough.)

Where is the (0,0)? Top left, bottom left, center? Is (0,1) above or below (0,0)?

I enter three points. What if I change the order of the points? Should the image change?

What to do about aliasing?


The task (as written) gives the user "freedom". That in turn means that the program/programmer must have more answers.
Topic archived. No new replies allowed.