Browse Source

add resolve_with

eqn_relations
Alex Mikhalev 6 years ago
parent
commit
f9b1f8924c
  1. 10
      src/entity.rs
  2. 4
      src/main.rs

10
src/entity.rs

@ -34,6 +34,16 @@ impl<T: Clone, TRegion: Region<T> + Clone> Var<T, TRegion> {
&self.value &self.value
} }
pub fn resolve_with(&mut self, eqns: &Eqns) -> bool {
let resolved_constraints = self.constraints.clone().evaluate_with(eqns).simplify();
if let Some(n) = resolved_constraints.nearest(&self.value) {
self.value = n;
true
} else {
false
}
}
pub fn evaluate_with(&mut self, eqns: &Eqns) -> bool { pub fn evaluate_with(&mut self, eqns: &Eqns) -> bool {
self.constraints = self.constraints.clone().evaluate_with(eqns).simplify(); self.constraints = self.constraints.clone().evaluate_with(eqns).simplify();
if let Some(n) = self.constraints.nearest(&self.value) { if let Some(n) = self.constraints.nearest(&self.value) {

4
src/main.rs

@ -48,7 +48,7 @@ fn main() {
let c4 = relation::PointAngle::new_vertical(p1.clone(), p2.clone()); let c4 = relation::PointAngle::new_vertical(p1.clone(), p2.clone());
let c3 = relation::PointAngle::new_horizontal(p2.clone(), p3.clone()); let c3 = relation::PointAngle::new_horizontal(p2.clone(), p3.clone());
let c2 = relation::AlignedDistance::new_vertical(p1.clone(), p2.clone(), 12.); let c2 = relation::AlignedDistance::new_vertical(p1.clone(), p2.clone(), 12.);
let c5 = relation::PointAngle::new(p1.clone(), p3.clone(), Rot2::from_angle_deg(45.)); let c5 = relation::PointAngle::new(p1.clone(), p3.clone(), Rot2::from_angle_deg(0.572938698));
let mut relations: Vec<Box<dyn Relation>> = let mut relations: Vec<Box<dyn Relation>> =
vec![/*Box::new(c1),*/ Box::new(c2), Box::new(c3), Box::new(c4), Box::new(c5)]; vec![/*Box::new(c1),*/ Box::new(c2), Box::new(c3), Box::new(c4), Box::new(c5)];
let mut constrained: Vec<Box<dyn Relation>> = Vec::new(); let mut constrained: Vec<Box<dyn Relation>> = Vec::new();
@ -94,7 +94,7 @@ fn main() {
let e2 = eqn::Eqn::new(eqn::Expr::Unkn(u2), eqn::Expr::Const(1.)); let e2 = eqn::Eqn::new(eqn::Expr::Unkn(u2), eqn::Expr::Const(1.));
let eqns = eqn::Eqns(vec![e1, e2]); let eqns = eqn::Eqns(vec![e1, e2]);
for p in &mut points { for p in &mut points {
p.borrow_mut().pos.evaluate_with(&eqns); p.borrow_mut().pos.resolve_with(&eqns);
} }
print_points(&points); print_points(&points);
} }

Loading…
Cancel
Save