Блог пользователя Abanoub_Ashraf

Автор Abanoub_Ashraf, 4 года назад, По-английски

Angular Sorting

hello , my friends

This is my first blog and I want to share some knowledge that may help other to solve some problems and

if you have another algorithm you can explain it in comments .

Let's start , you are given n points . each point is at integer coordinates (x,y) . can you sort them in clockwise order and vice versa ?

the points are here like that

Think about it for a bit and then continue with me ...

if we have slope from each point and (0,0) ?

Nice , we can get angle with x-axis and from this we can sort them !!

we can see like this

let's calculate the angle

angle = atan2(y,x) — in redius .

angle = atan2(y,x) * 180 /PI — this is in degree .

but what about negative angles ?!

we can add 360 degree to be positive .

angle+=(angle<0)*360 .

code in C++ https://ideone.com/fork/MXtcfD

Problem related to this concept https://codeforces.me/gym/103562/problem/D

In the end, I hope this blog is useful and that you like it . I will be happy with your comment :)

Thank you

Полный текст и комментарии »

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится