We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,415 Members — Technology Publication meets Social Media

# How to Draw a Line Having Length and Angle

Hi Everyone,

I am trying to creale an application which is supposed to display some line trace.What I have is the lenght of lines and the angle
(Azimut) degree of the Line for example 45 degree.
Can you please let me know how I can do this? I also have the Start point location.

2
Contributors
5
Replies
4 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Skill Endorsements: 0

x1 = startx
y1 = starty
x2 = x1 + cos(angle) * length
y2 = y1 + sin(angle) * length

Regards,
Emil Olofsson

emilo35
Junior Poster
104 posts since Feb 2010
Reputation Points: 14
Skill Endorsements: 0

Hi Emilo,

Thanks for your reply but How I can use thses in C# ?!

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Skill Endorsements: 0

Hi again Emilo

I trired the method that you sujested byt It seems there is something wrong with angle! here is the code which I create

`````` double length = 350;
double angle = 50;
double x1 = 200;
double y1 = 200;
double x2 = x1 + (Math.Cos(angle) * length);
double y2 = y1 + (Math.Sin(angle) * length);

int intX1 = Convert.ToInt32(x1);
int intY1 = Convert.ToInt32(y1);
int intX2 = Convert.ToInt32(x2);
int intY2 = Convert.ToInt32(y2);

System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Red, 5);
graphicsObj.DrawLine(myPen, new Point(intX1, intY1),new Point(intX2, intY2));
``````

Can you please take a look at code and let me know if this correct ot not?

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Skill Endorsements: 0

Hi!

`Math.Sin` and `Math.Cos` takes angles measured in radians and not degrees. Try this:

``````double angleRadians = (Math.PI / 180.0) * angle;
``````

Then use `angleRadians` instead.

Regards,
Emil Olofsson

emilo35
Junior Poster
104 posts since Feb 2010
Reputation Points: 14
Skill Endorsements: 0

Thanks Emilo,

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8