Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I want to draw a triangle with sides 3, 4 and 5 in C++, however I have some difficulty.
How I can adjust the character * to have a nice shape of triangles with exact size 3 , 4, and 5. Here, just seems the sides are 4, 4, and 5 whereas I want to show 3, 4 and 5. Is it possible?

*
**
****
*****
Posted
Updated 17-Feb-22 23:22pm

No, it isn't, not realy.
Console applications don't get any real control over the relative height and width of characters they print - that's why they are not used for significant graphics output (although ASCII art can be really impressive).

If you want to draw accurate shapes that aren't rectangles, you want to be looking at a Graphical User Interface, such as WinForms via CLR and .NET, or MFC. Which is a pretty massive learning curve! :laugh:
 
Share this answer
 
Comments
Patrice T 22-Oct-15 15:23pm    
+5
Quote:
How I can adjust the character * to have a nice shape of triangles with exact size 3 , 4, and 5. Here, just seems the sides are 4, 4, and 5 whereas I want to show 3, 4 and 5. Is it possible?
It will be complicated.
The aspect ratio (heigh/width) is different almost for each char in a font and is different for each font.
You have to find a particular char in particular font that will match your needs. But since it is char drawing, it never be nice.

I guess the only way is to switch to graphic mode.
 
Share this answer
 
As has been suggested several times, a graphical output would look much better and represent the proportions much better. In C++ this could look like this with GDI+:
C++
void MyDraw1(HDC hdc, int start_x, int start_y, double size)
{
    using namespace Gdiplus;

    Graphics gfx(hdc);

    // construct polygon (triangle: 3,4,5)
    std::vector<Point> p;
    p.push_back(Point(0, 0));
    p.push_back(Point(0, 3));
    p.push_back(Point(4, 3));

    // resize with offset
    for (size_t i = 0; i < p.size(); i++) {
        p[i].X = (INT)round(p[i].X * size) + start_x;
        p[i].Y = (INT)round(p[i].Y * size) + start_y;
    }

    Color  mycolor(0, 0, 255);
    Pen    myPen(mycolor);
    gfx.DrawPolygon(&myPen, p.data(), p.size());
}

If the triangular shape has to be realized with the character * instead of a line, that is of course also possible. You would then have to calculate the exact position for each *.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900