Skip to content

Commit

Permalink
Use Line32.intersection from pointy crate
Browse files Browse the repository at this point in the history
  • Loading branch information
DougLau committed Sep 29, 2020
1 parent 7cc679b commit f3c2308
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
22 changes: 0 additions & 22 deletions src/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,6 @@ pub fn float_lerp(a: f32, b: f32, t: f32) -> f32 {
b + (a - b) * t
}

/// Calculate intersection point of two lines.
///
/// * `a0` First point on line a.
/// * `a1` Second point on line a.
/// * `b0` First point on line b.
/// * `b1` Second point on line b.
/// Returns None if the lines are colinear.
pub fn intersection(a0: Pt32, a1: Pt32, b0: Pt32, b1: Pt32) -> Option<Pt32> {
let av = a0 - a1;
let bv = b0 - b1;
let den = av * bv;
if den != 0.0 {
let ca = a0 * a1;
let cb = b0 * b1;
let xn = bv.x() * ca - av.x() * cb;
let yn = bv.y() * ca - av.y() * cb;
Some(Pt32(xn / den, yn / den))
} else {
None
}
}

impl Default for WidePt {
fn default() -> Self {
WidePt(Pt32::default(), 1.0)
Expand Down
8 changes: 5 additions & 3 deletions src/stroker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//
// Copyright (c) 2017-2020 Douglas P Lau
//
use crate::geom::{intersection, WidePt};
use crate::geom::WidePt;
use crate::path::PathOp;
use crate::vid::Vid;
use pointy::Pt32;
use pointy::{Line32, Pt32};
use std::fmt;

/// Style for stroke joins.
Expand Down Expand Up @@ -354,8 +354,10 @@ impl Stroke {
let th = (a1 - a0).angle_rel(b0 - b1);
let sm = (th / 2.0).sin().abs();
if sm >= sm_min && sm < 1.0 {
let lna = Line32::new(a0, a1);
let lnb = Line32::new(b0, b1);
// Calculate miter point
if let Some(xp) = intersection(a0, a1, b0, b1) {
if let Some(xp) = lna.intersection(lnb) {
self.stroke_point(ops, xp);
return;
}
Expand Down

0 comments on commit f3c2308

Please sign in to comment.