Skip to content

Commit

Permalink
Merge pull request #210 from VanSHOE/Sin_Cos_Opt
Browse files Browse the repository at this point in the history
Sin cos optimization
  • Loading branch information
derekdreery committed Dec 23, 2021
2 parents 1ca2944 + b2fdb9d commit b9247ed
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ impl Affine {
/// The angle, `th`, is expressed in radians.
#[inline]
pub fn rotate(th: f64) -> Affine {
let s = th.sin();
let c = th.cos();
let (s, c) = th.sin_cos();
Affine([c, s, -s, c, 0.0, 0.0])
}

Expand Down
10 changes: 6 additions & 4 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,18 @@ impl Iterator for ArcAppendIter {
/// Take the ellipse radii, how the radii are rotated, and the sweep angle, and return a point on
/// the ellipse.
fn sample_ellipse(radii: Vec2, x_rotation: f64, angle: f64) -> Vec2 {
let u = radii.x * angle.cos();
let v = radii.y * angle.sin();
let (angle_sin, angle_cos) = angle.sin_cos();
let u = radii.x * angle_cos;
let v = radii.y * angle_sin;
rotate_pt(Vec2::new(u, v), x_rotation)
}

/// Rotate `pt` about the origin by `angle` radians.
fn rotate_pt(pt: Vec2, angle: f64) -> Vec2 {
let (angle_sin, angle_cos) = angle.sin_cos();
Vec2::new(
pt.x * angle.cos() - pt.y * angle.sin(),
pt.x * angle.sin() + pt.y * angle.cos(),
pt.x * angle_cos - pt.y * angle_sin,
pt.x * angle_sin + pt.y * angle_cos,
)
}

Expand Down
13 changes: 7 additions & 6 deletions src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ impl Iterator for CirclePathIter {
} else if ix <= self.n {
let th1 = self.delta_th * (ix as f64);
let th0 = th1 - self.delta_th;
let (c0, s0) = (th0.cos(), th0.sin());
let (c1, s1) = if ix == self.n {
(1.0, 0.0)
let (s0, c0) = th0.sin_cos();
let (s1, c1) = if ix == self.n {
(0.0, 1.0)
} else {
(th1.cos(), th1.sin())
th1.sin_cos()
};
Some(PathEl::CurveTo(
Point::new(x + r * (c0 - a * s0), y + r * (s0 + a * c0)),
Expand Down Expand Up @@ -385,9 +385,10 @@ mod tests {

#[inline]
fn point_on_circle(center: Point, radius: f64, angle: f64) -> Point {
let (angle_sin, angle_cos) = angle.sin_cos();
center
+ Vec2 {
x: angle.cos() * radius,
y: angle.sin() * radius,
x: angle_cos * radius,
y: angle_sin * radius,
}
}
5 changes: 3 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ pub fn solve_cubic(c0: f64, c1: f64, c2: f64, c3: f64) -> ArrayVec<f64, 3> {
let rho = r.hypot(sq);
let th = sq.atan2(r) * (1.0 / 3.0);
let cbrho = rho.cbrt();
let c = th.cos();
let ss3 = th.sin() * 3.0f64.sqrt();
let (th_sin, th_cos) = th.sin_cos();
let c = th_cos;
let ss3 = th_sin * 3.0f64.sqrt();
result.push(2.0 * cbrho * c - x0);
result.push(-cbrho * (c + ss3) - x0);
result.push(-cbrho * (c - ss3) - x0);
Expand Down
3 changes: 1 addition & 2 deletions src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,7 @@ impl Arc {
let mut ry = arc.radii.y.abs();

let xr = arc.x_rotation % (2.0 * PI);
let cos_phi = xr.cos();
let sin_phi = xr.sin();
let (sin_phi, cos_phi) = xr.sin_cos();
let hd_x = (arc.from.x - arc.to.x) * 0.5;
let hd_y = (arc.from.y - arc.to.y) * 0.5;
let hs_x = (arc.from.x + arc.to.x) * 0.5;
Expand Down
5 changes: 3 additions & 2 deletions src/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ impl Vec2 {
/// [`Affine::rotate`]: crate::Affine::rotate
#[inline]
pub fn from_angle(th: f64) -> Vec2 {
let (th_sin, th_cos) = th.sin_cos();
Vec2 {
x: th.cos(),
y: th.sin(),
x: th_cos,
y: th_sin,
}
}

Expand Down

0 comments on commit b9247ed

Please sign in to comment.