#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Simple Maths but can't crack it

Posts: 2,029

Hi All,
very simple problem - I need to use a circle with 8 equi-spaced small circles just inside the circle circumference to act as buttons. I need to detect the touch of each circle to be able to decide. between 8 options. Read a lot of maths tries atan() and atan2() and a few other options but the solution keeps eluding me. I was trying with two vector centre to vertical radius and centre to inner circle centre.

The circles are effectively 45deg spaced and ideally I was hoping to convert positional calcs to the integers 1 to 8 to get a selection result. Any ideas?

Tagged:

## Comments

• Posts: 8,948

@Bri_G Here’s a start.

``````viewer.mode=FULLSCREEN

function setup()
a=""
end

function draw()
background(0)
stroke(255)
strokeWidth(2)
for z=0,359,45 do
x=math.cos(math.rad(z))*100
y=math.sin(math.rad(z))*100
noFill()
ellipse(x+WIDTH/2,y+HEIGHT/2,60)
fill(255)
text(z//45+1,x+WIDTH/2,y+HEIGHT/2)
end
if a~="" then
fill(255)
text(a//45+1,WIDTH/2,HEIGHT/2)
end
end

function touched(t)
for z=0,359,45 do
x=math.cos(math.rad(z))*100+WIDTH/2
y=math.sin(math.rad(z))*100+HEIGHT/2
if t.x>x-30 and t.x<x+30 and t.y>y-30 and t.y<y+30 then
a=z
end
end
end
``````
• edited April 1 Posts: 8,948

Here’s a simple class version.

``````viewer.mode=FULLSCREEN

function setup()
b1=button(400,200,80)
b2=button(500,500,60)
end

function draw()
background(0)
b1:draw()
b2:draw()
end

function touched(t)
b1:touched(t)
b2:touched(t)
end

button=class()

function button:init(x,y,s)
self.x=x
self.y=y
self.size=s
self.val=0
end

function button:draw()
stroke(255)
strokeWidth(2)
fill(175, 224, 201)
ellipse(self.x,self.y,self.size*3)
for z=0,359,45 do
local x=math.cos(math.rad(z))*self.size
local y=math.sin(math.rad(z))*self.size
fill(137, 234, 72)
ellipse(x+self.x,y+self.y,self.size*.7)
fill(255, 0, 0)
text(z//45+1,x+self.x,y+self.y)
end
if self.val>0 then
fill(255)
text(self.val,self.x,self.y)
end
end

function button:touched(t)
if t.state==BEGAN then
for z=0,359,45 do
local x=math.cos(math.rad(z))*self.size+self.x
local y=math.sin(math.rad(z))*self.size+self.y
if t.x>x-self.size/2 and t.x<x+self.size/2 and t.y>y-self.size/2 and t.y<y+self.size/2 then
self.val=z//45+1
end
end
end
end
``````
• Posts: 2,029
@dave1707 - thanks for both, gave me the pointer I needed. Will be posting results later.
Thanks again.
Sign In or Register to comment.