-
Jul 21st, 2017, 06:32 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Drawing line at an angle.
Hi,
I don't understand why this code doesn't produce an even set of lines from a given point:
Code:
Public Class Form1
Private Sub Form1_Load() Handles MyBase.Load
WindowState = 2
Button1.Text = "Exit"
Button2.Text = "Start"
End Sub
Private Sub Button1_Click() Handles Button1.Click
Me.Close()
End Sub
Private Sub Button2_Click() Handles Button2.Click
Dim graphicsObject As Drawing.Graphics
Dim pen1, pen2 As Pen
Dim Pnt(2) As Point
Dim X, Y, angle, lnth, num As Single
If Not My.Computer.FileSystem.DirectoryExists("C:\Plot") Then
My.Computer.FileSystem.CreateDirectory("C:\Plot")
End If
FileOpen(1, "C:\Plot\Coordinates.txt", OpenMode.Output, OpenAccess.Write)
graphicsObject = Me.CreateGraphics()
pen1 = New Pen(Color.Black, 1)
pen2 = New Pen(Color.Red, 1)
X = 200
Y = 250
lnth = 200
num = 24
angle = 0
X += 600
Pnt(1) = New Point(X, Y)
For i = 1 To num
angle = (360 / num) * i
Pnt(2) = New Point(X + Math.Cos(angle) * lnth, Y + Math.Sin(angle) * lnth)
PrintLine(1, i.ToString("00") & ": " & angle.ToString)
If i = num Then
graphicsObject.DrawLine(pen2, Pnt(1), Pnt(2))
Else
graphicsObject.DrawLine(pen1, Pnt(1), Pnt(2))
End If
Next
FileClose()
Process.Start("C:\Plot\Coordinates.txt")
End Sub
End Class
The list of angles progress as expected, but the angles of the lines are anything but regular.
Maybe the maths is wrong ?
Variables X, Y, angle, lnth & num started life as integers but Singles have made no difference.
Just two Buttons, (It's part of a bigger app. to make a polygon plotting tool.)
VS 2017 Community.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Jul 21st, 2017, 07:26 AM
#2
Re: Drawing line at an angle.
Without even reading your code, I'm going to make the educated guess that you didn't read the documentation and you provided your angles in degrees rather than in radians. That's usually the reason that anything to do with trigonometry doesn't work.
-
Jul 21st, 2017, 07:43 AM
#3
Thread Starter
PowerPoster
Re: Drawing line at an angle.
Originally Posted by jmcilhinney
Without even reading your code, I'm going to make the educated guess that you didn't read the documentation and you provided your angles in degrees rather than in radians. That's usually the reason that anything to do with trigonometry doesn't work.
Thanks jmcilhinney... Good guess.
Angle (in degrees) x 0.01745329 makes all the difference.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Jul 21st, 2017, 07:48 AM
#4
Re: Drawing line at an angle.
Originally Posted by Poppa Mintin
Thanks jmcilhinney... Good guess.
Angle (in degrees) x 0.01745329 makes all the difference.
Poppa.
Or, you could read the relevant documentation and learn that you should use Math.PI and how to do so.
-
Jul 21st, 2017, 10:00 AM
#5
Re: Drawing line at an angle.
Nothing wrong with precalculating a constant although rather than a literal I usually assign it to a named constant like DEG2RAD.
Seeing 180/Math.Pi and Math.Pi/180 scattered throughout the code makes me cringe.
Of course those languages without Pi predefined we used 2 * ATAN(1) to get PI.
For angles dealing with Displays the resolution of a Single should be fine (6 digits or so), but using Math.Pi to get 13 digits or so won't hurt time wise so you may as well use a Double.
Last edited by passel; Jul 21st, 2017 at 10:04 AM.
-
Jul 21st, 2017, 12:00 PM
#6
Re: [RESOLVED] Drawing line at an angle.
There's ways to suggest "better practices" without pushing people to the ground. passel seems pretty good at it.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jul 21st, 2017, 08:08 PM
#7
Re: [RESOLVED] Drawing line at an angle.
Originally Posted by Sitten Spynne
There's ways to suggest "better practices" without pushing people to the ground. passel seems pretty good at it.
Given that I referred to reading the documentation in my first reply and the OP still failed to do so, I'm not concerned about *****-footing around. Maybe if a few more people suggested using the documentation as a good way for people to help themselves then more people might do it and benefit as a result. Maybe some of those people enjoy having others dependent on them though.
-
Jul 21st, 2017, 08:10 PM
#8
Re: Drawing line at an angle.
Originally Posted by passel
Nothing wrong with precalculating a constant although rather than a literal I usually assign it to a named constant like DEG2RAD.
Seeing 180/Math.Pi and Math.Pi/180 scattered throughout the code makes me cringe.
But Math.PI is itself a constant, so you can define your own constant and use Math.PI at the same time.
-
Jul 23rd, 2017, 08:10 PM
#9
Re: Drawing line at an angle.
I agree. I use Math.PI myself when I setup the DEG2RAD and RAD2DEG constants.
But I guess I worked on enough projects in different languages over the last 40 years where we defined the named constants using the literal constants that I recognize 0.01745329 for what it is just like I recognize 3.1415926 or .3048.
Using a literal constant isn't wrong, in my opinion, although it is less clear to the general public (i.e. a "magic number") and not a recommended practice.
And you are correct in that it is evidence that most likely the documentation wasn't read since it would have suggested calculating the value using Math.Pi.
I guess I probably over reacted to the wording "should use Math.Pi" as meaning not using Math.Pi is wrong. If "should" was replaced with "could" in that sentence it probably wouldn't have elicited my response. It does seem like a minor distinction at this time.
Your main point that if the documentation about the functions were read for meaning, then the question would not have come about is valid. I guess I'll try to assume from now on that you're trying to be instructive, not condescending, but I've had a number of old guys at work over the years who were very condescending about newbies and what they should know, so I may tend to hear it in the written words where it wasn't intended.
-
Jul 23rd, 2017, 09:07 PM
#10
Re: Drawing line at an angle.
Originally Posted by passel
Your main point that if the documentation about the functions were read for meaning, then the question would not have come about is valid. I guess I'll try to assume from now on that you're trying to be instructive, not condescending, but I've had a number of old guys at work over the years who were very condescending about newbies and what they should know, so I may tend to hear it in the written words where it wasn't intended.
You can assume that I'm trying to be instructive but that may or may not mean that I'm not trying to be condescending and whether or not I'm trying doesn't necessarily mean that I'm succeeding. If I have indicated to someone that they should read the documentation and they don't then I don't necessarily think that a bit of condescension isn't warranted. My hope is that, the next time that person finds themselves in a similar situation, they will recall the sting of being condescended to and why it happened and want to do what is required to avoid that same feeling. Like any approach, it doesn't work with everyone. Some people are so sure that they couldn't be doing anything wrong that they don't even consider that there may be a reason that I said what I said the way I said it other than that I'm just a bad person. I'm not really too concerned whether those people don't appreciate my approach though. I should point out that I'm not necessarily categorising Poppa Mintin like that, as he hasn't said anything either way about what I said and how I said it. Anyway, enough about me.
-
Jul 24th, 2017, 03:55 AM
#11
Thread Starter
PowerPoster
Re: [RESOLVED] Drawing line at an angle.
Originally Posted by jmcilhinney
Given that I referred to reading the documentation in my first reply and the OP still failed to do so, I'm not concerned about *****-footing around. Maybe if a few more people suggested using the documentation as a good way for people to help themselves then more people might do it and benefit as a result. Maybe some of those people enjoy having others dependent on them though.
Yes you did suggest 'reading the documentation' in your first post.
Have you any idea how much documentation there is on this subject, and since you did not specify which documentation in particular, you're right in that respect also, I failed to read it... I'd still be reading had I not.
You gave me the answer, I used the answer, it works very well...
Thank you again.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Jul 24th, 2017, 05:24 AM
#12
Re: [RESOLVED] Drawing line at an angle.
Originally Posted by Poppa Mintin
Have you any idea how much documentation there is on this subject, and since you did not specify which documentation in particular, you're right in that respect also, I failed to read it... I'd still be reading had I not.
Um, your code uses Math.Sin and Math.Cos. There is exactly one page of documentation for each. Both of those pages contain the following statement:
The angle, a, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.
where Math.PI is a link to the documentation for that field. All you had to do was click on one of them in your code and press the F1 key. That's about 30 seconds work at the most. I don't understand how you could not know by now that you should be reading the documentation that specifically relates to the type(s) and/or member(s) that you are using in your code but now you do know for the future.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|