12/16/2023 0 Comments Swift handy calculator subtraction![]() I do encourage people to go and research the topic further. But please teacher, remember this is not a math blog. For example, if the sine of an angle □ is x, the arcsine of x will be □:Ī math teacher reading this, would probably frown upon reaching this section. In addition to the three basic trigonometric functions, you also have the inverse functions (arcsine, arccosine and arctangent). I’m kidding! You do, but as I promised, to keep this as light as possible, let’s just say that these are functions you can call with swift, they receive a single numeric value (representing an angle), and return another number (representing the proportion between two sides of the triangle). Given the x, y coordinates of two points, you can get the distance between them (hypotenuse), given a distance and a direction from one CGPoint, you can obtain the second CGPoint coordinates, and so much more.Īnother application of trigonometry, is when you need some function that let you smooth in or out an effect, a distance, a color, or anything that can be expressed numerically. Given two CGPoints, you can calculate the direction (angle) from one to the other (e.g., useful for a nice view rotation effect). If you know two legs, you can get the hypotenuse and the angles, etc.Īnd why would I need that? Well, if you start thinking about the vertices of the triangle (A, B and C) as CGPoints in your View, it all comes clear. For example, if you know the hypotenuse and one of the angles, you can obtain the sizes of the legs and the other angles. How Will Trigonometry Help Me?Īs we will see next, given a right-triangle, we can derive some values from others. We need to be aware of radians, because trigonometric functions in Swift, require angles be specified in radians. If you are interested to know why, you may visit this page: Why complicate things? Aren’t degrees easier to use? Maybe for us, but mathematically, radians make sense. Print("\(b.radians) radians = \(b.degrees) degrees") Print("\(a.radians) radians = \(a.degrees) degrees") Let b = Angle(radians: 2.3456) // Create an angle using radians SwiftUI has a type named Angle, which comes with some handy initializers and computed properties: let a = Angle(degrees: 180) // Create an angle using degrees However, since this is a SwiftUI blog, we have another way. Print("degAngle degrees = \(degAngle.asRadians) radians") Print("radAngle radians= \(radAngle.asDegrees) degrees") Here’s an extension you can create, in order to convert between the two units: extension Double If you need to convert from degrees to radians, you do: Luckily, converting between these two units, is very straight forward: Degrees and radians are two different scales to measure them. But if I say, show me a 1.5708 radians angle, what would you do? Well, you should do the same. If I tell you, show me a 90 degree angle, you know immediately what to do. In the example image, leg a is adjacent to □ (beta) and leg b is adjacent to □ (alpha) Degrees vs. In the example image, leg a is opposed to □ (alpha) and leg b is opposed to □ (beta)Īdjacent Leg: Relative to one of the angles, it is the one touching it. ![]() Opposed Leg: Relative to one of the angles, it is the one not “touching” it. ![]() Leg: Any of the sides, that is not the hypotenuse. Hypotenuse: It’s the largest side in the right-triangle and the one opposing the right angle. It is a triangle, with one of its angles being 90 degrees. Right-Angled Triangle: Or simply right-triangle. The names are not relevant, and you can choose anything, but to be consistent through the article, these are the one I’m using: We’ll give them arbitrary names, to make sure we all understand each other. Trigonometry is all about the relation between the sides and angles of a right-angled triangle. No matter the case, this article aims at removing the obstacle in learning advanced SwiftUI techniques. Or maybe you had a trigonometry class before, but you forgot the essentials. I understand now that not everyone learning SwiftUI may have the background (or perhaps the age), to stumble upon trigonometry classes yet. Nothing too complex, but enough to leave some of my readers partially out of the conversation. If you are looking for something deeper, the Internet is full of such resources.Īfter I wrote the “ Advanced SwiftUI Animations” series, I realized that I find myself using trigonometry very often. It is just a compilation of some recipes you may find useful, while drawing paths, creating shapes, performing smooth transitions, etc. This post will not be a full-on math class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |