Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any way to draw a line when starting point , angle and length are given
Posted
Comments
Homero Rivera 4-Mar-14 1:37am    
Yes. I think your question is more related to math tricks. Please see answer
Homero Rivera 4-Mar-14 1:58am    
I made a mistake in the formula, please review. Sin and Cos should be multiplied by the desired length, not the coords :p

1 solution

Using some code from msdn
http://msdn.microsoft.com/es-es/library/f956fzw1(v=vs.110).aspx[^]

Starting point needs x y coords. Lets say you have them. And you have the angle in radians, and length:


C#
int x = 500;
int y = 600;
decimal angle = 1.5;
int length = 400;


public void DrawLinePoint(PaintEventArgs e)
{

    //cosA = x/h
    //AND sinX = y/h


    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);

    // Create points that define line.
    Point point1 = new Point(x, y);
    Point point2 = new Point(x + Math.Cos(angle)*length, y + Math.Sin(angle)*length);

    // Draw line to screen.
    e.Graphics.DrawLine(blackPen, point1, point2);

    //PLEASE NOTE THAT YOU MAY NEED ADDITIONAL ROUNDING OPERATIONS, SINCE X Y COORDS ARE MEANT TO BE INTs
}
 
Share this answer
 
v2

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