summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJovansonlee Cesar <ivanceras@gmail.com>2020-11-25 17:00:45 +0800
committerJovansonlee Cesar <ivanceras@gmail.com>2020-11-25 17:00:45 +0800
commite42ecb4fed9495daad3bc819268103305b26ea0b (patch)
tree7e33f328e5f002069b32f74b69452e7d5c487356
parentdcd65f2361f88b7cbc9e81b91382f21c60114706 (diff)
get rid of can_merge_polygon, instead call on merge_polygon().is_some() instead to avoid code duplication
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment.rs4
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/line.rs48
2 files changed, 12 insertions, 40 deletions
diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs
index 7fd5cb9..ae3378a 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment.rs
@@ -203,12 +203,12 @@ impl Fragment {
Fragment::Line(line) => match other {
Fragment::Line(other) => line.is_touching(other),
Fragment::Arc(other_arc) => line.is_touching_arc(other_arc),
- Fragment::Polygon(polygon) => line.can_merge_polygon(polygon),
+ Fragment::Polygon(polygon) => line.merge_line_polygon(polygon).is_some(),
Fragment::Circle(circle) => line.is_touching_circle(circle),
_ => false,
},
Fragment::Polygon(polygon) => match other {
- Fragment::Line(other) => other.can_merge_polygon(polygon),
+ Fragment::Line(other) => other.merge_line_polygon(polygon).is_some(),
_ => false,
},
Fragment::Arc(arc) => match other {
diff --git a/svgbob/src/buffer/fragment_buffer/fragment/line.rs b/svgbob/src/buffer/fragment_buffer/fragment/line.rs
index 7fe37cf..2779b3b 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment/line.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment/line.rs
@@ -300,52 +300,24 @@ impl Line {
}
}
- /// check if this line is touching the polygon with a polygon tag
- /// This is used for checking the line to be endorsed as arrow_line
- /// if the PolygonTag can connect to the angle of the line
- pub(crate) fn can_merge_polygon(&self, polygon: &Polygon) -> bool {
- // get the line angle can go alongside the polygon tag, arrow direction
- // check also if the distance of this line end-point to the center
- // of the polygon is less than the diagonal width of the cell grid.
+ /// merge this line to the marker line
+ pub(crate) fn merge_line_polygon(&self, polygon: &Polygon) -> Option<Fragment> {
let poly_center = polygon.center();
-
- println!("poly_center: {:?}", poly_center);
let distance_end_center = self.end.distance(&poly_center);
let distance_start_center = self.start.distance(&poly_center);
- let threshold_length = dbg!(self.heading().threshold_length());
- let is_close_start_point = dbg!(distance_start_center < threshold_length);
- let is_close_end_point = dbg!(distance_end_center < threshold_length);
+ let threshold_length = self.heading().threshold_length();
+ let is_close_start_point = distance_start_center < threshold_length;
+ let is_close_end_point = distance_end_center < threshold_length;
let is_same_direction = dbg!(polygon.matched_direction(self.heading()));
let is_opposite_direction = dbg!(polygon.matched_direction(self.heading().opposite()));
- (is_same_direction || is_opposite_direction) && (is_close_start_point || is_close_end_point)
- }
-
- /// TODO: the get_marker function don't take into account the direction
- /// of the line from start to end. The direction is not followed.
- /// TODO: If the marker direction is on opposite direction of the line
- /// heading, swap the line start and end point
- ///
- /// merge this line to the marker line
- pub(crate) fn merge_line_polygon(&self, polygon: &Polygon) -> Option<Fragment> {
- if self.can_merge_polygon(polygon) {
- let marker = polygon.get_marker();
-
- let poly_center = polygon.center();
- let distance_end_center = self.end.distance(&poly_center);
- let distance_start_center = self.start.distance(&poly_center);
-
- let threshold_length = self.heading().threshold_length();
- let is_close_start_point = distance_start_center < threshold_length;
- let is_close_end_point = distance_end_center < threshold_length;
-
- let is_same_direction = dbg!(polygon.matched_direction(self.heading()));
-
- let is_opposite_direction = dbg!(polygon.matched_direction(self.heading().opposite()));
+ let can_merge = (is_same_direction || is_opposite_direction)
+ && (is_close_start_point || is_close_end_point);
+ if can_merge {
let new_line = if is_close_end_point {
Line::new_noswap(self.start, self.end, self.is_broken)
} else if is_close_start_point {
@@ -361,7 +333,7 @@ impl Line {
extended_line.end,
extended_line.is_broken,
None,
- marker,
+ polygon.get_marker(),
))
} else {
None
@@ -651,7 +623,7 @@ mod tests {
let polygon = Polygon::new(vec![p1, p2, p3], false, vec![PolygonTag::ArrowRight]);
let line = Line::new(m, end, false);
- assert!(line.can_merge_polygon(&polygon));
+ assert!(line.merge_line_polygon(&polygon).is_some());
}
#[test]