Click here to Skip to main content
14,930,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to draw an equilateral triangle inside a PictureBox on Windows Form, with the user providing the base and height of the triangle. I would like the triangle to always be centered on the PictureBox.
I'm trying but I can't.
can anybody help me?
textBox1 keep the base and textBox2 keep the height.
The dimension of the pictureBox is 400x400 pixel.
I'm considering 1cm = 40 pixels, so the user can only enter 8cm both in height and in width.



Thanks.


José Carlos - Brazil

What I have tried:

basex = int.Parse(textBox1.Text) * 40;
altura = int.Parse(textBox2.Text) * 40;

x = (400 - basex) / 2;
y = (400 - altura) / 2;

Point[] pontos =
{
    new Point(x, y+altura/2),
    new Point(x+basex, y+altura/2),
    new Point(x+(basex/2),altura *2)
};

desenhador.DrawPolygon(lapis, pontos);
Posted
Updated 10-Jun-21 3:05am

1 solution

First off, an equilateral triangle is a triangle in which all three sides have the same length. Why does your user need to enter a height if it's irrelevant - the three sides are all base long, aren't they?

Secondly, don't use int.Parse to process user input = use int.TryParse instead, and report input problems to the user instead of your app crashing.

Then just work out the three points: for a triangle like this:
 ^
/_\
The top point X is the length of a side / 2, and it's Y is simply worked out using Pythagoras from that and the length of a side:
Y = sqrt(L * l - ( L * L / 4))
   
Comments
Member 12539429 10-Jun-21 9:34am
   
Hi,

Perfect.
Thank you very much.

José Carlos
OriginalGriff 10-Jun-21 10:00am
   
You're welcome!

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