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
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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.

Thanks for your time in Advanced

2
Contributors
5
Replies
4 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
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
Solved Threads: 12
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
Solved Threads: 2
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?

Thanks for your time

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
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
Solved Threads: 12
Skill Endorsements: 0

Thanks Emilo,

You were very helpful

Behseini
Junior Poster
148 posts since Feb 2011
Reputation Points: 8
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by emilo35

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0754 seconds using 2.7MB