-
Angles for a line
Yo all,
Here's what I'm wanting to do... I have a form with a picture box, text box and a button. I wanted to be able to enter a degree into the text box and then press the button, which would draw a line in the picture box facing that degree. Unfortunately, it didn't quite turn out that way. My lack of knowledge led me to creating a really lame piece of code that should be on a stand-up comedians jokes list. Now, I'm gonna' show you my code but please...before you start rolling over on the floor in hysterics...remember, I'm an idiot :D
Code:
Const PI As Single = 3.14159
Private Function DtoR(degrees As Double) As Double
Dim rad As Double
rad = degrees / PI
DtoR = rad * 180
End Function
Private Sub cmdGo_Click()
Dim degrees As Double
Dim x2 As Double
Dim y2 As Double
face.Cls
degrees = Val(txtDegrees.Text)
x2 = Sin(DtoR(degrees))
y2 = -(Cos(DtoR(degrees)))
face.Line (face.Width / 2, face.Height / 2)-(x2 * 700, y2 * 700)
End Sub
If somebody could please have a look and correct it for me (after you've stopped laughing) I would be very grateful :)
-
Not too sure, but I think your problem lies in your DtoR function. I believe it should be:
DtoR = degrees / (180 / pi)
Once again, not too sure.
-
Thanks for trying Sastraxi, but unfortunately, that's not it :confused:
-
I'm thinking it might have to do with your last couple parameters on the .Line method ...
Remember that the way you're calc'ing x2 & y2 makes them relative to your origin. And since your origin isn't the upper-left corner (0,0), then you need to do a bit more calc'ing ... :cool:
Code:
OriginX = face.Width \ 2
OriginY = face.Height \ 2
face.Line (OriginX, OriginY) - (OriginX + (x2 * 700), OriginY + (y2 * 700))
... or something like that. As an aside, notice that I used integer divide ("\") instead of the standard divide ("/") because it's faster and it results in an integer, which you'll need for your pixel coordinates anyway.
Hope that helps.
-Bryk
-
I did that Brykovian and the results did change! Unfortunately it's still not working as it should. After making the change you suggested I only needed to go through numbers 1 to 10 for the line to do a complete revolution. It is a start but still, more to do :D Any more suggestions anybody? :eek:
-
Code:
x2 = Cos(DtoR(degrees))
y2 = Sin(DtoR(degrees))
Z.
-
Thanks for trying but that makes it worse :eek:
-
Code:
Private Sub cmdGo_Click()
Dim rad As Double
face.Cls
rad = Val(txtDegrees.Text) * 1.74532925199433E-02 'pi/180
face.Line (face.Width / 2, face.Height / 2)-Step(Cos(rad) * 700, Sin(rad) * 700)
End Sub
-
Yah, hypnos ... I think Kedaman's code should do it ...
You did have your DtoR function wrong though ... you had the 180 and Pi thing flip-flopped ... shoulda been:
Code:
Private Function DtoR(degrees As Double) As Double
Dim rad As Double
rad = degrees / 180
DtoR = rad * PI
End Function
Ked's code takes this into effect, plus puts all of the intermediate steps onto fewer lines.
-Bryk
p.s. I'd forgot about the "step" feature on the .Line method ...:o
-
I'd like to thank all you guys for the help. You was right Brykovian, it was my Degrees to Radians function that was wrong. So, I changed it to the code you mentioned:
Code:
Private Function DtoR(degrees As Double) As Double
Dim rad As Double
rad = degrees / 180
DtoR = rad * PI
End Function
Now it's working perfectly! Once again, thank you all for the help!