Finding positions to paint around a circle?

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Finding positions to paint around a circle?

Post by BruceSteers »

Hi all I'm trying to Draw some things on various points around a circle.
(Making a dial control)


If I use the following code...
  Paint.LineWidth = 1
  Paint.Ellipse(0, 0, 100,100)
  Paint.Stroke
That gives me a circle.
But now i want to Paint things at various places around the circle

Say If it was a clock and i wanted to place 60 dots around it.

360 degrees divided by 60 places around the circumference

But what commands would i use to get those positions?

Any help appreciated :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Finding positions to paint around a circle?

Post by stevedee »

BruceSteers wrote: Sunday 2nd January 2022 6:51am
...360 degrees divided by 60 places around the circumference

But what commands would i use to get those positions?
A rotating vector can be generated using trigonometry, so I think you use the Cosine of the angle to find the x & y points to draw your dots/marks/mini-circles.

I'll try and generate an example when I have time later today, unless Charlie comes up with an oven-ready solution in the meantime.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Finding positions to paint around a circle?

Post by stevedee »

OK, this simple code demonstrates positioning a circle marker in one quadrant of a larger circle.
It just uses a Form, DrawingArea and a Timer;
Public intAngle As Integer

Public Sub Form_Open()

End

Public Sub dwgArea_Draw()
Dim intRadius As Integer
Dim fltXCosTheta As Float
Dim fltYCosTheta As Float

  If intAngle > 90 Then 
    intAngle = 0
  Endif
  intRadius = dwgArea.Width * 0.4
  fltXCosTheta = intRadius * Cos(Rad(intAngle))
  fltYCosTheta = intRadius * Cos(Rad(90 - intAngle))
  Draw.Circle(dwgArea.Width / 2, dwgArea.Height / 2, dwgArea.Width * 0.45)
  Draw.Circle(dwgArea.Width / 2 + fltXCosTheta, dwgArea.Height / 2 + fltYCosTheta, 10)
  
End

Public Sub Timer1_Timer()

  dwgArea.Refresh(intAngle)
  intAngle += 10

End
By changing line 19 to this, you get coverage of a second quadrant;
  Draw.Circle(dwgArea.Width / 2 + fltXCosTheta, dwgArea.Height / 2 - fltYCosTheta, 10)

...and similar sign changes for the other 2 quadrants.

Obviously this is not the full solution to your problem, but I hope it sets you on the right path.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Finding positions to paint around a circle?

Post by cogier »

I'll try and generate an example when I have time later today, unless Charlie comes up with an oven-ready solution in the meantime.
That made the wife and me laugh this morning. In true cooking programming style, here is one I prepared earlier. Have a look at 'Analogue Clock' which is on the Gambas Farm. The maths was done by my friend Matt.

Image
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Finding positions to paint around a circle?

Post by BruceSteers »

Not Matt Crick by any chance?
I have an old friend lives on your rock, Lucy Crick and her fella Mathew.


Thanks guys , i should be able to work stuff out from there :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Finding positions to paint around a circle?

Post by cogier »

BruceSteers wrote: Sunday 2nd January 2022 5:46pm Not Matt Crick by any chance?
I have an old friend lives on your rock, Lucy Crick and her fella Mathew.
No not that Mathew, this one.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Finding positions to paint around a circle?

Post by cogier »

I have done a little more work on this. Does this help?
Run the code in a new graphical program.

Image
' Gambas class file

Panel1 As Panel
DA1 As DrawingArea
SliderValue As Slider

Public Sub Form_Open()

  With Me
    .Height = 500
    .Width = 500
    .Padding = 5
    .Arrangement = Arrange.Vertical
    .Center
    .Text = "Go faster dial >>>>>>"
  End With

  With Panel1 = New Panel(Me) As "Panel1"
    .Expand = True
  End With

  With DA1 = New DrawingArea(Panel1) As "DA1"
    .Cached = True
    .x = 0
    .y = 0
  End With

  With SliderValue = New Slider(Me) As "SliderValue"
    .Height = 28
    .MaxValue = 360
    .Mark = True
  End With

End

Public Sub Form_Resize()

  Dim iLoop, iCol As Integer
  Dim iLen As Integer

  DA1.Height = Min(Panel1.Width, Panel1.Height)
  DA1.Width = Min(Panel1.Width, Panel1.Height)
  DA1.Clear

  ''Adds an outer circle
  ' With Paint
  '   .Begin(DA1)
  '   .LineWidth = 1
  '   .Arc(DA1.W / 2, DA1.H / 2, DA1.H / 2)
  '   .Stroke
  '   .End
  ' End With

  ''Draws the lines around the outside of the circle

  For iLoop = 0 To 359 'Step 6 ''For time
    With Paint
      .Begin(DA1)
      Paint.Brush = Paint.Color(Color.Black)
      .LineCap = .LineCapRound
      .LineWidth = 2
      .Translate(DA1.W / 2, DA1.H / 2)
      .Rotate(Rad(360 - iLoop))
      .Translate(-(DA1.W / 2), -(DA1.H / 2))
      'If iLoop Mod 5 = 0 Then iLen = 15 Else iLen = 25 ''For time
      If iLoop Mod 10 = 0 Then iLen = 15 Else iLen = 25
      .MoveTo(DA1.W / 2, DA1.H / iLen)
      .LineTo(DA1.W / 2, iLen)
      .Stroke
      .End
    End With
  Next

  ''Paints the pointer

  If SliderValue.Value > 90 Then iCol = Color.Green
  If SliderValue.Value > 180 Then iCol = Color.Yellow
  If SliderValue.Value > 270 Then iCol = Color.Red

  With Paint
    .Begin(DA1)
    Paint.Brush = Paint.Color(iCol)
    .LineCap = .LineCapRound
    .LineWidth = 10
    .Translate(DA1.W / 2, DA1.H / 2)
    .Rotate(Rad(360 - SliderValue.Value))
    .Translate(-(DA1.W / 2), -(DA1.H / 2))
    .MoveTo(DA1.W / 2, DA1.H / 2)
    .LineTo(DA1.W / 2, 40)
    .Stroke

    ''Paints the center circle and fills it in
    Paint.Brush = Paint.Color(Color.Blue)
    .Arc(DA1.W / 2, DA1.H / 2, 20)
    .Fill
    .Stroke

    .End
  End With

End

Public Sub SliderValue_Change()

  Form_Resize()

End
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Finding positions to paint around a circle?

Post by BruceSteers »

cogier wrote: Thursday 6th January 2022 4:25pm I have done a little more work on this. Does this help?
Run the code in a new graphical program.

That's awesome m8 thank you :)
minimalistic so easier to get my head around :)

To be honest, as much as I really appreciated the previous help it did give me a headache so i resorted to using images and the image.Rotate command.

But with that example i think i could get on it with a hand drawn object :)

My objective was to make a cool slider like object that spins like a dial.

What i've made is something i call ImageSpinner.class



You can use custom images for the background and for the button.

it's wip at mo and needs a lot of work on the mouse controls

With the above code example i think i can possibly add a custom background making property to add the ticks like on the dial of your example :)

Nice one fella, cheers :)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Finding positions to paint around a circle?

Post by cogier »

Hi Bruce, I had a look at your example and wondered if you were aware of the Dial available in Gambas. You need gb.qt5.ext for this: -

Image
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Finding positions to paint around a circle?

Post by BruceSteers »

cogier wrote: Friday 7th January 2022 3:29pm Hi Bruce, I had a look at your example and wondered if you were aware of the Dial available in Gambas. You need gb.qt5.ext for this: -

Image
Haha , no i was not ,, that's exactly what i was aiming for. but not qt dependant.

I'm hoping to make mine highly customisable.

currently it has 2 modes, spinner and switch ,
spinner for a dial that goes round and round
and switch for a positional switch
If at first you don't succeed , try doing something differently.
BruceS
Post Reply