14,930,017 members
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)
};

```
Posted
Updated 10-Jun-21 3:05am

## Solution 1

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))`
Member 12539429 10-Jun-21 9:34am

Hi,

Perfect.
Thank you very much.

José Carlos
OriginalGriff 10-Jun-21 10:00am

You're welcome!

Top Experts
Last 24hrsThis month
 OriginalGriff 158 Richard MacCutchan 85 Chris Losinger 75 Patrice T 55 CPallini 40
 OriginalGriff 4,270 Richard MacCutchan 1,930 Richard Deeming 1,435 Patrice T 1,105 CPallini 930

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