summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJovansonlee Cesar <ivanceras@gmail.com>2018-08-03 16:09:25 +0800
committerJovansonlee Cesar <ivanceras@gmail.com>2018-08-03 16:09:25 +0800
commitfb75a9ce32d304c9334100c28ca55c352955d53f (patch)
tree9df2e2e34b70e8a331be7cb6277bf0a72240a40f
parent85b7202ad560b2877c12011e7654ea1cfb249743 (diff)
Initial implementation for merging specific case
-rw-r--r--svgbob/src/element.rs37
-rw-r--r--svgbob/src/optimizer.rs10
-rw-r--r--svgbob/src/point.rs2
3 files changed, 48 insertions, 1 deletions
diff --git a/svgbob/src/element.rs b/svgbob/src/element.rs
index f7e0a41..8c2845b 100644
--- a/svgbob/src/element.rs
+++ b/svgbob/src/element.rs
@@ -19,6 +19,7 @@ use element::{
Feature::{Arrow,ArrowStart,Circle,Square,OpenCircle,BigOpenCircle,Nothing},
};
use unicode_width::UnicodeWidthStr;
+use point;
@@ -30,6 +31,28 @@ pub enum Element {
Text(Loc, String),
}
+impl Element{
+
+ fn longer_line<'a>(&'a self, other: &'a Element) -> &'a Element {
+ match self{
+ Element::Line(s1, e1, _, _, _) => match other{
+ Element::Line(s2, e2, _, _,_) => {
+ let d1 = point::distance(s1, e1);
+ let d2 = point::distance(s2, e2);
+ if d1 > d2 {
+ self
+ }
+ else{
+ other
+ }
+ }
+ _ => panic!("only for lines"),
+ }
+ _ => panic!("only for lines"),
+ }
+ }
+}
+
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
pub enum Stroke {
Solid,
@@ -135,6 +158,7 @@ impl Element {
return Some(other.clone())
}
+ // extend 1 with 2
// line1 line2
// s-----e s2-----e2
// s----------------e2
@@ -153,6 +177,7 @@ impl Element {
));
}
}
+ // extend 1 with flip 2
// line1 line2
// s------e e2-------s2
// s-------------------s2
@@ -169,6 +194,7 @@ impl Element {
));
}
}
+ // flip1 extend 2
// line1 line2
// e------s s2------e2
// s------------------e2
@@ -185,7 +211,17 @@ impl Element {
end_feature2.clone(),
));
}
+ // TODO: need fixing
+ // same starting point, take longer line
+ // longer line
+ // s-------e
+ // s2-------------->e2
+ else {
+ println!("longer line");
+ return Some(self.longer_line(other).clone())
+ }
}
+ // extend 2 with 1
// line1 line2
// e------s e2------s2
// e---------------------s2
@@ -205,6 +241,7 @@ impl Element {
));
}
}
+
}
return None;
}
diff --git a/svgbob/src/optimizer.rs b/svgbob/src/optimizer.rs
index 6c9aeb7..ad0c625 100644
--- a/svgbob/src/optimizer.rs
+++ b/svgbob/src/optimizer.rs
@@ -141,6 +141,16 @@ impl Optimizer {
all_reduced.push(elm.clone());
}
}
+ if consumed.len() > 0 {
+ //println!("original elements: {}", elements.len());
+ //println!("total consumed: {}", consumed.len());
+ //println!("all_reduced: {}", all_reduced.len());
+ }
+ all_reduced.sort();
+ all_reduced.dedup();
+ if elements.len() != all_reduced.len(){
+ println!("reduced from {} to {}", elements.len(), all_reduced.len());
+ }
all_reduced
}
diff --git a/svgbob/src/point.rs b/svgbob/src/point.rs
index 106b251..b26ac38 100644
--- a/svgbob/src/point.rs
+++ b/svgbob/src/point.rs
@@ -32,6 +32,6 @@ pub fn collinear(a: &Point, b: &Point, c: &Point) -> bool {
a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) == 0.0
}
-pub fn _distance(a: &Point, b: &Point) -> f32{
+pub fn distance(a: &Point, b: &Point) -> f32{
((b.x - a.x ).powi(2) + (b.y - a.y).powi(2)).sqrt()
}