mirror of https://codeberg.org/topola/topola.git
chore,docs(math): rename 'cross_product' to 'perp_dot_product', add some documentation
This commit is contained in:
parent
b4fe7006b7
commit
344d892ae0
|
|
@ -87,12 +87,12 @@ pub fn intersect_circle_segment(circle: &Circle, segment: &Line) -> Vec<Point> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn between_vectors(p: Point, from: Point, to: Point) -> bool {
|
pub fn between_vectors(p: Point, from: Point, to: Point) -> bool {
|
||||||
let cross = cross_product(from, to);
|
let cross = perp_dot_product(from, to);
|
||||||
|
|
||||||
if cross > 0.0 {
|
if cross > 0.0 {
|
||||||
cross_product(from, p) >= 0.0 && cross_product(p, to) >= 0.0
|
perp_dot_product(from, p) >= 0.0 && perp_dot_product(p, to) >= 0.0
|
||||||
} else if cross < 0.0 {
|
} else if cross < 0.0 {
|
||||||
cross_product(from, p) >= 0.0 || cross_product(p, to) >= 0.0
|
perp_dot_product(from, p) >= 0.0 || perp_dot_product(p, to) >= 0.0
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
@ -229,22 +229,24 @@ impl ops::MulAssign<f64> for NormalizedAngle {
|
||||||
pub fn angle_between(v1: Point, v2: Point) -> NormalizedAngle {
|
pub fn angle_between(v1: Point, v2: Point) -> NormalizedAngle {
|
||||||
NormalizedAngle::atan2(geo::point! {
|
NormalizedAngle::atan2(geo::point! {
|
||||||
x: dot_product(v1, v2),
|
x: dot_product(v1, v2),
|
||||||
y: cross_product(v1, v2)
|
y: perp_dot_product(v1, v2)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn seq_cross_product(start: Point, stop: Point, reference: Point) -> f64 {
|
pub fn seq_perp_dot_product(start: Point, stop: Point, reference: Point) -> f64 {
|
||||||
let dx1 = stop.x() - start.x();
|
let dx1 = stop.x() - start.x();
|
||||||
let dy1 = stop.y() - start.y();
|
let dy1 = stop.y() - start.y();
|
||||||
let dx2 = reference.x() - stop.x();
|
let dx2 = reference.x() - stop.x();
|
||||||
let dy2 = reference.y() - stop.y();
|
let dy2 = reference.y() - stop.y();
|
||||||
cross_product((dx1, dy1).into(), (dx2, dy2).into())
|
perp_dot_product((dx1, dy1).into(), (dx2, dy2).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dot_product(v1: Point, v2: Point) -> f64 {
|
pub fn dot_product(v1: Point, v2: Point) -> f64 {
|
||||||
v1.x() * v2.x() + v1.y() * v2.y()
|
v1.x() * v2.x() + v1.y() * v2.y()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cross_product(v1: Point, v2: Point) -> f64 {
|
/// This is often called [perp dot product](https://mathworld.wolfram.com/PerpDotProduct.html),
|
||||||
|
/// or "2D cross product".
|
||||||
|
pub fn perp_dot_product(v1: Point, v2: Point) -> f64 {
|
||||||
v1.x() * v2.y() - v1.y() * v2.x()
|
v1.x() * v2.y() - v1.y() * v2.x()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use geo::{geometry::Point, point, Line};
|
||||||
use specctra_core::math::Circle;
|
use specctra_core::math::Circle;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::seq_cross_product;
|
use super::seq_perp_dot_product;
|
||||||
|
|
||||||
#[derive(Error, Debug, Clone, Copy, PartialEq)]
|
#[derive(Error, Debug, Clone, Copy, PartialEq)]
|
||||||
#[error("no tangents for {0:?} and {1:?}")] // TODO add real error message
|
#[error("no tangents for {0:?} and {1:?}")] // TODO add real error message
|
||||||
|
|
@ -101,7 +101,7 @@ pub fn tangent_segments(
|
||||||
.filter_map(move |tangent_point_pair| {
|
.filter_map(move |tangent_point_pair| {
|
||||||
if let Some(cw1) = cw1 {
|
if let Some(cw1) = cw1 {
|
||||||
let cross1 =
|
let cross1 =
|
||||||
seq_cross_product(tangent_point_pair.0, tangent_point_pair.1, circle1.pos);
|
seq_perp_dot_product(tangent_point_pair.0, tangent_point_pair.1, circle1.pos);
|
||||||
|
|
||||||
if (cw1 && cross1 <= 0.0) || (!cw1 && cross1 >= 0.0) {
|
if (cw1 && cross1 <= 0.0) || (!cw1 && cross1 >= 0.0) {
|
||||||
return None;
|
return None;
|
||||||
|
|
@ -110,7 +110,7 @@ pub fn tangent_segments(
|
||||||
|
|
||||||
if let Some(cw2) = cw2 {
|
if let Some(cw2) = cw2 {
|
||||||
let cross2 =
|
let cross2 =
|
||||||
seq_cross_product(tangent_point_pair.0, tangent_point_pair.1, circle2.pos);
|
seq_perp_dot_product(tangent_point_pair.0, tangent_point_pair.1, circle2.pos);
|
||||||
|
|
||||||
if (cw2 && cross2 >= 0.0) || (!cw2 && cross2 <= 0.0) {
|
if (cw2 && cross2 >= 0.0) || (!cw2 && cross2 <= 0.0) {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue