O
O
olezhenka2018-03-10 23:00:00
Mathematics
olezhenka, 2018-03-10 23:00:00

How to find the nearest object if you only know its degrees?

I have a circle and two objects.
uQF8NroH-ds.jpg
On click, I need to determine the nearest object using their degrees. I wrote something like:

Math.abs(A_deg - click_deg) > Math.abs(B_deg - click_deg) ? B_deg : A_deg

Here the condition is - where there are fewer degrees left, it means that this object is closer and I use it, but then an obvious problem arose that I expected and thought I would solve with some simple additional condition:
red - click degrees, the rest are objects
0_0OstLlU4I.jpg
But even I introduced different conditions and I can not correctly determine the nearest object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-03-10
@olezhenka

var diff = Math.min(Math.abs(x - y), 360 - Math.abs(x - y));

Can be rewritten as a function:
Demo .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question