My half remembered trig is letting me down.
I have a right angle triangle, and I know the opposite and the adjacent.
I know that
tan(x) = opp/adj
But Im failing to find x, and it can’t be this hard…
So far fruitless searches have led me to ‘maths is fun’ sites which have the gall to say ‘then, when you know what tan(x) is, just use tan-1 on your calculator’
What’s tan to the -1?
Does Xojo have a math function for that?
Jeff for example finding an angle using ATAN in action and HTH I went through the Trig re-learning about 7 months ago writing a variation of the A* algorithm
CalculateAngleDegrees(inSourceX as integer, InSourceY as integer, inDestX as integer, inDestY as integer) as Double
Dim dx, dy as double
Dim Angle as Double
Dim rAngle as Integer
Dim pi as double = 3.14159265358979323846264338327950
dx = inDestX-inSourceX
dy = inDestY-inSourceY
Angle = ATan2(dy,dx) // Radians
Angle = Abs(Angle * 180 /pi) // Convert to Degree
Return Angle
Getting there, thanks.
Im trying to make a rotating knob control.
It starts off as a circle with a line pointing to the right, and the idea is that the user can click and rotate it using the mouse.
I’ve got it working if the mouse is to the right of the middle.
Right now Im stuck on what to do on the left hand side as my brain hurts with radian measuring.
at the moment, rotation is a property of the window.
Eventually I will make this a class and add rotation as a property of the class
In the mouse drag event:
[code] dim w as integer
dim h as integer
dim t as double
if x > me.width/2 then
//right half
if y < me.height then
w = x-me.width/2
h = y-me.height/2
rotation= atan(h/w) // correct
else
//lower
w = x-me.width/2
h = me.height/2 -y
rotation = atan(h/w) // correct
end if
else
//left half
if y < me.height then
w = me.width/2 -x
h = y-me.height/2
rotation= 3.14159/2 - atan(h/w) //wrong and fifth guess
else
//lower
w =me.width/2-x
h = me.height/2-y
rotation =3.14159*2 - atan(h/w) // wrong
end if
end if
invalidate[/code]
Paint event:
[code] dim circ as new OvalShape
dim gr as new group2d
dim li as new CurveShape
Here is a test project I slapped together fast from a few functions I wrote a while back. I tried to mix our code base and of course my adds are very forum raw due to time right now. Click and hold the mouse and make a circle around the window. This test project isn’t your final goal for sure My forum code isn’t tuned to account for the mouse rotating close to the center point so to try this example make wide circles almost outside of the window. (I didn’t focus at all on your circle code, only your line). Hopefully this will help you a bit.
There’s different ways how you might want to control a knob. This is my attempt where rotating displaces instead of sets…
[code]centerX As Integer //rotation point of dial
centerY As Integer
radius As Integer //size of dial
rotation As Double //current angle
oldAngle As double //last mouse angle
Sub Open()
centerX = 50 //init dial in a 100x100 canvas
centerY = 50
radius = 50
End Sub
Function MouseDown(X As Integer, Y As Integer) As Boolean
oldAngle = ATan2(X-centerX, Y-centerY) //store angle of mouse
return true
End Function
Sub MouseDrag(X As Integer, Y As Integer)
dim a As double = ATan2(X-centerX, Y-centerY) //calc new angle of mouse
rotation = rotation + a - oldAngle //add mouse angle difference to rotation
oldAngle = a //store this mouse angle
Invalidate //redraw
End Sub
Sub Paint(g As Graphics, areas() As REALbasic.Rect)
g.ForeColor = &c808080
g.DrawRect 0, 0, g.Width, g.Height
I have written a rotation knob that works similar to music software, in that it snaps to straight line angles and moves based on how far your mouse is from the centre of the knob rather than pointing towards the mouse. Let me know if you want to use that. I may be able to upload it to Github for you.
Hi all. I recent written subject nearly this on this forum in Japanese thread.
Is this same thing? Title may be “get angle value by mouse rotating on canvas control” translated for English from Japanese.
That would have been nice, but sadly not x-platform.
Ive modified Kazuto’s code in the end. (Thanks)
And Im now displaying a pixmapimage which I apply the rotation to.
It seems everyone likes the displacement method, so I’ll bow to that, although I really wanted the mouse to say ‘this is where the end of the line should go’
Then just set Rotation to the calculated mouse angle in MouseDown and MouseDrag. What I don’t like about this technique is the angle jumps when you first click, but maybe thats better for you.