How to draw an arrow with DrawLine command

How to draw an arrow with DrawLine command

Graphics libraries usually do not support DrawArrow command by default beacuse it is a simple task to mimic DrawArrow command using three DrawLine commands.

In this page, we will help you to create your own DrawArrow method. To draw an arrow, we need to call DrawLine command at least three times. Two for the arrow head, one for the arrow body. If you need special effects, you may add other DrawLine calls to your method.

The following method draws an arrow using three DrawLine calls in C# programming language. 

void DrawArrow(double x1, double y1, double x2, double y2, int arrLength = 10)
    var m = x2 - x1 == 0 ? 0 : (y2 - y1) / (x2 - x1);
    var degree = Math.Atan(m);

    var toLeft = x2 > x1 ? 0 : Math.PI;

    var degree1 = degree + 5 * Math.PI / 6 + toLeft;
    var degree2 = degree + 7 * Math.PI / 6 + toLeft;

    var px1 = x2 + Math.Cos(degree1) * arrLength;
    var py1 = y2 + Math.Sin(degree1) * arrLength;

    var px2 = x2 + Math.Cos(degree2) * arrLength;
    var py2 = y2 + Math.Sin(degree2) * arrLength;

    var pen = new Pen(Color.Black);
    g.DrawLine(pen, x1, y1, x2, y2);
    g.DrawLine(pen, x2, y2, px1, py1);
    g.DrawLine(pen, x2, y2, px2, py2);


added 7 years 2 months ago

- How to make bold the text before colon character in Microsoft Word
- Generate object from xml in c#
- 24 Useful Distinct Color Codes
- How to draw an arrow with DrawLine command
- How to write Snake game in C#?
- Break Parallel Foreach Earlier
- How to find all Stored Procedures having a given text inside
- How to Create and Write Excel File in C#
- Extension Methods
- Microsoft Code Contracts
- VersaFix
- QuickFIX
- fix4net
- Converting String to Double in C#
- C# File Operations, C# Input Output (C# I/O)