Maths isn't fun!

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?

you need this

Jeff for example finding an angle using ATAN in action and HTH :slight_smile: I went through the Trig re-learning about 7 months ago writing a variation of the A* algorithm :frowning:

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. :slight_smile:

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

  w = x-me.width/2
  h = me.height/2 -y
  rotation = atan(h/w)    // correct
end if


//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

  w =me.width/2-x
  h = me.height/2-y
  rotation =3.14159*2  - atan(h/w)  // wrong 
end if

end if

Paint event:

[code] dim circ as new OvalShape
dim gr as new group2d
dim li as new CurveShape

circ = new OvalShape
circ.FillColor = &cf0f0f0
circ.BorderColor = &cb0b0b0
circ.width = g.width
circ.height = g.height
circ.x= 0//g.width/2
circ.y = 0//g.height/2
circ.border = 1

gr.Append circ

li.x= 0//g.width/2
li.y =0// g.height/2
li.x2 = g.width
li.y2 =0// li.y
gr.append li

gr.X = me.width/2
gr.y = me.height/2
gr.Rotation = rotation
g.drawobject gr[/code]


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 :wink: 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.

Good luck!

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

g.ForeColor = &c000000
g.DrawOval(centerX-radius, centerY-radius, radius2, radius2)

g.DrawLine(centerX, centerY, centerX+radiuscos(rotation), centerY-radiussin(rotation))
End Sub

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.

Depending on how you want to do this, you can modify a standard Xojo slider to be a round slider on OS X.

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.

Please read by Google translation or other web service.
I hope this talking will help you.


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.

Then grab the offset (of the value where the mouse is down compared with where it was) on mouse down and apply it while in mouse drag.

Oh. I see that you’re doing something similar in your code already.

Personally when dealing with any form of dragging, I capture the offset on mouse down. my offset becomes the current value - the valueAtXY.

Then in the drag event I set the value to the valueAtXY - the offset.