Hello Coders Pirates ?☠️ Have you ever found yourself lost in the coding sea, just trying to figure out the angle using atan(), and frustrated that you can't find the right direction. you are not alone Today we are going on an adventure, where we will find the treasure called atan2() in PHP. Let's see why atan() can put you in danger, while atan2() will get you safely to port.
Let's start the journey then. ⛵
Imagine you have a treasure map. There is talk of an island, which is navigated to a point (x, y) on the map. You can use atan()—but stop! It is only part of the story. atan() can take you straight to Davy Jones' locker, or worse, to a codebase full of bugs. ? So if you want to get the right path, you need atan2(), the Captain Jack Sparrow of this story.
This atan() function can be found in almost all languages. We talk about php. atan() is mainly used to calculate angles. But here's a problem — atan() only cares about the y/x ratio, it doesn't care what quadrant you're in. If you start trusting him completely, you may find that you are going in the wrong direction. Why? Because atan() doesn't return the whole image—it only tells the angle relative to the first quadrant.
Why? Because atan() doesn't give the whole image — it only tells the angle relative to the first quadrant. That means when you are going west, you should actually be traveling east! atan2(), on the other hand, takes both x and y coordinates into account, and—like a good compass—knows exactly which quadrant you're in.
Now let's know a little more about the difference between these 2.
Now let's say it in code language:
The parameter of the atan() function is a number, which is the value of the ratio y/x, where y and x are two variables or number values.
atan(float $num): float
The parameters ofatan2() function are 2.
- $y: Y coordinate value.
- $x: X coordinate value.
atan2(float $y, float $x): float
Let's try to understand the matter with an example.
// $y এবং $x এর মান নির্ধারণ $y = 5; $x = -10; // atan() ব্যবহার - শুধুমাত্র y/x অনুপাত জানে $angle1 = atan($y / $x); echo "atan() angle: " . rad2deg($angle1) . " degrees\n"; // Outputs: -26.57 degrees // atan2() ব্যবহার - $x এবং $y উভয়ই বিবেচনায় নেয় $angle2 = atan2($y, $x); echo "atan2() angle: " . rad2deg($angle2) . " degrees\n"; // Outputs: 153.43 degrees
As you can see from the example, atan() does return an angle. But the direction is not telling. That is, it is not understood which quadrant you are in. On the other hand, the value we get with the atan2() function clearly shows that the point (x, which) is in the 2nd quadrant. Now we can easily understand which direction the treasure island is.
?Radians vs Degrees (It's not just math, it's living!)
PHP's atan() and atan2() functions give you an angle in radians. Which looks like a joke! Don't worry—it's just a different method of measuring angles. For humans and pirates to understand, I converted it to degrees using the rad2deg() function.
Let us now try to make atan() and atan2() our own, to better understand their operation.
⚠️ If you feel like this is going over your head, skip this section! ?
atan() calculates the arctangent of a number. One way to approximate this function is to use Gregory's series. Here is a commonly used series for atan(z):
which if written a little straighter stands -
Now let's write this series in code
atan(float $num): float
atan2(). The main task of this function is to identify the correct quadrant only. So why delay, let's write -
atan2(float $y, float $x): float
Now we look at the four quadrant results and explain each quadrant:
So next time you're navigating the dangerous ocean of angles and coordinates, don't just rely on atan(). Use atan2() and steer your ship in the right direction every time. It's the compass that will keep you out of the dreaded Davy Jones locker of miscalculations!
Hope your treasure journey goes well.
If you want to know more about atan() and atan2(), visit the following links:
The above is the detailed content of The Curse of atan() and the Story of atan: A Story of Disorientation in the Coding World. For more information, please follow other related articles on the PHP Chinese website!