# A triangle with rounded corners in C#

For one of my projects I needed to draw triangles with rounded egdes, and since I didn’t found anything about this topic elsewhere, I had to figure it out by myself. Luckily no mathematics is needed, except the Pythagorean theorem. So here’S how I did it, complete with a sample implementation using C#.

To make things easier, I started with a right-angled triangle where the two sides forming the right angle are vertical and horizontal. This makes applying a rounded corner for the right angle easy, since it is a common problem known from drawing rounded rectangles. You’ll find examples for this all over the net. I instead will discuss, how to round the two other edges, which both form an angle of 45 degrees.

Here’s a sketch demonstrating the problem. A circle with radius r needs to be placed at point C for the two sides forming the triangle becoming its tangents. How do we calculate C? Well, we already know that the length of BC is r, so we just need to figure out the length of AB. AB itself has the same length then BD, whitch is the sum of BC and CD, where BC is r:

```
AB = BD
AB = BC + CD
AB = r + CD
```

Now the points C, D, and E form another right-angled triangle with CE = ED = r. So we can apply the Pythagorean theorem:

```
AB = r + CD
AB = r + sqr(CE * CE + ED * ED)
AB = r + sqr(r * r + r * r)
AB = r + sqr(2 * r * r)
AB = r + sqr(2) * r
AB = (1 + sqr(2)) * r
```

For drawing circles or parts of it in C#, we need to provide the surrounding rectangle R, which obviously starts at Rx = Cx - r and Ry = Cy - r.

```
Rx = Cx - r = Ax
Ry = Cy - r = Ay + (1 + sqr(2)) * r - r
Ry = Ay + (1 - 1 + sqr(2)) * r
Ry = Ay + sqr(2) * r
```

Here’s the C# code to contruct a rounded rectangle’s graphic path. The right angle is rounded with double radius, just because it looks better:

Now having the triangle, we can transform it, to point in any directions and to have any size. If for example we want to draw a triangle pointing to the right beeing 20 pixel in height, we apply the following transformation:

What is happening here? After rotating, the triangle’s bounding rectangle is higher then the original, since the hypotenuse is now vertical. The length of the hypotenuse (Lh) is:

```
Lh = sqr(width * width + width * width)
Lh = sqr(2 * width * width)
Lh = sqr(2) * width;</code>
```

We now have to move the triangle down a bit, so that its top corner is within the orignals path boundaries (This makes positioning much easier). The amount we have to move is the lengh of the hypotenuse minus the width divided by two. We also have to move the whole triangle to the left, since it was rotated at its bounding rectangle’s center, which now forms the left side of the triangle. Afterwards the triangle is scaled, since we want the hypotenuse to be 20 pixel in height.

After the transformation, we have a right angled triangle with rounded edges, pointing to the right, and 20 pixel in height. We now can apply a last transformation to place it at the coordinates (5, 10), for example: