Flutter Impeller
trig.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_TRIG_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_TRIG_H_
7 
8 #include <functional>
9 #include <vector>
10 
12 
13 namespace impeller {
14 
15 /// @brief A structure to store the sine and cosine of an angle.
16 struct Trig {
17  /// Construct a Trig object from a given angle in radians.
18  explicit Trig(Radians r)
19  : cos(std::cos(r.radians)), sin(std::sin(r.radians)) {}
20 
21  /// Construct a Trig object from the given cosine and sine values.
22  Trig(double cos, double sin) : cos(cos), sin(sin) {}
23 
24  double cos;
25  double sin;
26 
27  /// @brief Returns the vector rotated by the represented angle.
28  Vector2 operator*(const Vector2& vector) const {
29  return Vector2(static_cast<Scalar>(vector.x * cos - vector.y * sin),
30  static_cast<Scalar>(vector.x * sin + vector.y * cos));
31  }
32 
33  /// @brief Returns the Trig representing the negative version of this angle.
34  Trig operator-() const { return Trig(cos, -sin); }
35 
36  /// @brief Returns the corresponding point on a circle of a given |radius|.
37  Vector2 operator*(double radius) const {
38  return Vector2(static_cast<Scalar>(cos * radius),
39  static_cast<Scalar>(sin * radius));
40  }
41 
42  /// @brief Returns the corresponding point on an ellipse with the given size.
43  Vector2 operator*(const Size& ellipse_radii) const {
44  return Vector2(static_cast<Scalar>(cos * ellipse_radii.width),
45  static_cast<Scalar>(sin * ellipse_radii.height));
46  }
47 };
48 
49 } // namespace impeller
50 
51 #endif // FLUTTER_IMPELLER_GEOMETRY_TRIG_H_
Point Vector2
Definition: point.h:331
float Scalar
Definition: scalar.h:19
Definition: comparable.h:95
Type height
Definition: size.h:29
Type width
Definition: size.h:28
A structure to store the sine and cosine of an angle.
Definition: trig.h:16
Vector2 operator*(double radius) const
Returns the corresponding point on a circle of a given |radius|.
Definition: trig.h:37
Vector2 operator*(const Vector2 &vector) const
Returns the vector rotated by the represented angle.
Definition: trig.h:28
Trig(double cos, double sin)
Construct a Trig object from the given cosine and sine values.
Definition: trig.h:22
Trig operator-() const
Returns the Trig representing the negative version of this angle.
Definition: trig.h:34
Trig(Radians r)
Construct a Trig object from a given angle in radians.
Definition: trig.h:18
double cos
Definition: trig.h:24
Vector2 operator*(const Size &ellipse_radii) const
Returns the corresponding point on an ellipse with the given size.
Definition: trig.h:43
double sin
Definition: trig.h:25