BLOG.CSHARPHELPER.COM: Draw lines with arrowheads in C#
Draw lines with arrowheads in C#
If <vx, vy> is a vector between two points, then <-vy, vx> and <vy, -vx> are perpendicular vectors of the same length. If you add and subtract these vectors from the original one, you get new vectors that make a 45 degree angle with the original one. You can use those vectors to make arrow heads and fletchings.
The DrawArrowhead method shown in the following code draws a pair of vectors at a 45 degree angle from a vector. The DrawArrow method described next uses it to draw arrow heads and tails.
// Draw an arrowhead at the given point // in the normalized direction <nx, ny>. private void DrawArrowhead(Graphics gr, Pen pen, PointF p, float nx, float ny, float length) { float ax = length * (-ny - nx); float ay = length * (nx - ny); PointF[] points = { new PointF(p.X + ax, p.Y + ay), p, new PointF(p.X - ay, p.Y + ax) }; gr.DrawLines(pen, points); }
The DrawArrow method shown in the following code draws a line with arrow heads or fletchings on the ends. It draws the line and then finds a vector of length 1 in the direction of the line. It then uses the DrawArrowhead method to draw arrowheads and fletchings appropriately.
// Draw arrow heads or tails for the // segment from p1 to p2. private void DrawArrow(Graphics gr, Pen pen, PointF p1, PointF p2, float length, EndpointStyle style1, EndpointStyle style2) { // Draw the shaft. gr.DrawLine(pen, p1, p2);
9/1/2010 9:12 AMRod Stephens wrote:
Doh! Sorry about that. The blogging tool really didn't like those characters. I think I've got it fixed now. Thanks for pointing this out. Reply to this
You have unencoded characters, making the first sentence difficult to read.
"If is a vector between two points, then <-v2, v1> and are a perpendicular vectors of the same length"
Looking at the page source shows:
"If
Regards;
Richard Moss
Reply to this
Doh! Sorry about that. The blogging tool really didn't like those characters. I think I've got it fixed now. Thanks for pointing this out.
Reply to this