Need trig & LCB help

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Need trig & LCB help

Richard Gaskin via use-livecode
In a nutshell, I am working on a widget that consists of a wheel with
buttons. Currently I've just manually calculated out where 8 buttons go,
and
it works well, but I am trying to set it up with a user definable number
of
buttons contained in array "mData."

Need help checking my math (and LCB syntax.) I took out some of the
syntax
like setting paints and some other fluff, this is just calculating the
positions of each circle.

     -- tBigRad is the radius of the main circle across the widget
     if my width > my height then
       put my height/2 into tBigRad
     else
       put my width/2 into tBigRad
     end if

     -- tLilRad is the radius of each small button around the edge
     put tBigRad/4 into tLilRad

     -- finding the coordinates of the center of the main circle
     put my width/2 into tBigX
     put my height/2 into tBigY
     put point [(my width/2),(my height/2)] into tCenter
     fill circle path centered at tCenter with radius tBigRad on this
canvas

     -- create an "inner circle" by subtracting the radius of a button
from
the radius of the main circle
     put tBigRad - tLilRad into tInnerRad
     -- calculate angle in radians from the for the center of each button
     put (the number of elements in mData)/(2 * pi()) into tButtonAngles

     repeat with tButtonNumber from 1 up to the number of elements in
mData
       put tInnerRad * (tButtonNumber-1 * tButtonAngles) into tAngle
       put tBigX * cos(tAngle * pi()) into tButtonX
       put tBigY * sin(tAngle * pi()) into tButtonY
       put point [tButtonX, tButtonY] into tButtonCenter
       fill circle path centered at tButtonCenter with radius tLilRad on
this
canvas
     end repeat

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Need trig & LCB help

Richard Gaskin via use-livecode
Replace the last part of your handler with the following.

variable tBNum as Number
variable tBCenters as List

put tBigRad - tLilRad into tInnerRad
put the number of elements in mData into tBNum
put regularPoints([tBNum,tInnerRad,tBigX,tBigY]) into tBCenters
repeat with tButtonNumber from 1 up to tBNum
   fill circle path centered at tBCenters[tButtonNumber] \
        with radius tLilRad on this canvas
end repeat

-- returns list of N regular (=evenly spaced) circle points
-- starting at high noon, turning clockwise
-- pL = [numOfPoints,radius,centerX,centerY]
handler regularPoints (in pL as List) returns List
  variable tI as Number
  variable tPi as Number
  variable tPts as List
  put 2*pi/pL[1] into tPi
  put [] into tPts
  repeat with tI from 0 up to pL[1]-1
    push point \
    [pL[3]+pL[2]*sin(tI*tPi), pL[4]-pL[2]*cos(tI*tPi)] onto tPts
  end repeat
  return tPts
end polyPoints

[Untested, right out of my head, should work.]

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Need trig & LCB help

Richard Gaskin via use-livecode
In reply to this post by Richard Gaskin via use-livecode
This works much better than what I was trying.

THANKS!
---------------------------------------------
Replace the last part of your handler with the following.

variable tBNum as Number
variable tBCenters as List

put tBigRad - tLilRad into tInnerRad
put the number of elements in mData into tBNum
put regularPoints([tBNum,tInnerRad,tBigX,tBigY]) into tBCenters
repeat with tButtonNumber from 1 up to tBNum
   fill circle path centered at tBCenters[tButtonNumber] \
        with radius tLilRad on this canvas
end repeat

-- returns list of N regular (=evenly spaced) circle points
-- starting at high noon, turning clockwise
-- pL = [numOfPoints,radius,centerX,centerY]
handler regularPoints (in pL as List) returns List
  variable tI as Number
  variable tPi as Number
  variable tPts as List
  put 2*pi/pL[1] into tPi
  put [] into tPts
  repeat with tI from 0 up to pL[1]-1
    push point \
    [pL[3]+pL[2]*sin(tI*tPi), pL[4]-pL[2]*cos(tI*tPi)] onto tPts
  end repeat
  return tPts
end polyPoints

[Untested, right out of my head, should work.]
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Need trig & LCB help

Richard Gaskin via use-livecode
In reply to this post by Richard Gaskin via use-livecode
Greg, I've been away from LCB for a while. When reading the last post
again I remembered an improvement that you could try to use:

The current version computes the pathes with every redraw (onPaint).
This is far too often. We could compute the pathes once (in onCreate)
and then update ONLY when needed (= when one of its params changes).
This will give your widget a lot of time for other things and makes
the OnPaint code shorter.

variable mBCircles as List
variable mBigRad as Number
variable mLilRad as Number
variable mInnerRad as Number
variable mBigX as Number
variable mBigY as Number
variable mBNum as Number

[1] in OnCreate write:
put the number of elements in mData into mBNum
put my width/2 into mBigX
put my height/2 into mBigY
put mBigRad-mLilRad into mInnerRad
updateCirclePaths([mBNum,mInnerRad,mLilRad,mBigX,mBigY])

[2] in OnPaint write:
repeat with tButtonNumber from 1 up to mBNum
   fill mBCircles[tButtonNumber] on this canvas
end repeat

[3] This does the job minimal often:
Whenever you change one of its params write also

updateCirclePaths([mBNum,mInnerRad,mLilRad,mBigX,mBigY])

[4] This handler computes already the small circle paths:

-- sets the list mBCircles of regularly placed circle paths,
-- turns clockwise from high noon, is centered at point [cX,cY]
-- pL = [numOfCircles,bigRadius,smallRadius,cX,cY]
handler updateCirclePaths (in pL as List) returns nothing
   variable tI as Number
   variable tPi as Number
   variable tPaths as List
   put 2*pi/pL[1] into tPi
   put [] into tPts
   repeat with tI from 0 up to pL[1]-1
      push circle path centered at point \
        [pL[4]+pL[2]*sin(tI*tPi), pL[5]-pL[2]*cos(tI*tPi)] \
          with radius pL[3] onto tPaths
  end repeat
  put tPaths into mBCircles
end handler

Hope you'll show us your shining new widget ...

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode