# How to find polar angle made by the line by joining the given point in cartesian plane with origin. I want a unique angle which will be from \[0,2\*pi)

**URL:** <https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899>\
**Category:** General Discussion\
**Created:** [January 22, 2024, 7:09am UTC](https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899 "2024-01-22T07:09:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaghunathBandha](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/raghunathbandha/32/1843_2.png) [@RaghunathBandha](https://community.freefem.org/u/RaghunathBandha)\
**Post date:** [January 22, 2024, 7:09am UTC](https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899/1 "2024-01-22T07:09:02Z")

</div>

Except (0,0), if I enter any point on coordinate axes I am not getting the correct answer. For example If I enter (1,-0.01) I should get an angle close to 2_pi. On (1,0), I should get pi/2. For (-1,0), (0,-1) I should get pi and 3_pi/2 respectively. Do I need to redefine atan function on the coordinate axes? Please help.

real x,y,s;  
cout\<\<"Enter x-coordinate : ";  
cin\>\>x;  
cout\<\<"Enter y-coordinate : ";  
cin\>\>y;  
s=atan(y/x);  
cout\<\<"Polar angle of the coordinate is : ";  
cout\<\<s;

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [January 22, 2024, 8:24am UTC](https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899/2 "2024-01-22T08:24:56Z")

</div>

Use the function [atan2](https://en.wikipedia.org/wiki/Atan2): `atan2(y,x)`

---

<div class="post-metadata">

**Author:** ![RaghunathBandha](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/raghunathbandha/32/1843_2.png) [@RaghunathBandha](https://community.freefem.org/u/RaghunathBandha)\
**Post date:** [January 25, 2024, 11:26am UTC](https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899/3 "2024-01-25T11:26:56Z")

</div>

Let f(x,y)=0 for (x,y)=(0,0) and f(x,y)=x^2/(x^2+y^2) for x^2\>y^2 and f(x,y)=y^2/(x^2+y^2) for x^2\<y^2. How to define a function with three cases.

---

<div class="post-metadata">

**Author:** ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)\
**Post date:** [January 25, 2024, 5:52pm UTC](https://community.freefem.org/t/how-to-find-polar-angle-made-by-the-line-by-joining-the-given-point-in-cartesian-plane-with-origin-i-want-a-unique-angle-which-will-be-from-0-2-pi/2899/4 "2024-01-25T17:52:33Z")

</div>

Use [conditionnal expression operator `?`](http://en.cppreference.com/w/c/language/operator_other#Conditional_operator). It works as:

> condition ? expression-true : expression-false

so for your question:

`func f = ( (x==0.0 && y==0.0) ? 0.0 : ( x^2-y^2 > 0.0 ? x^2/(x^2+y^2) : y^2/(x^2+y^2) ) ); `
