Skip to content

Commit

Permalink
added conversion to Vec<(f64,f64)> and Vec<(f64,f64,f64)>
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappycheese committed Sep 3, 2021
1 parent 0afc261 commit 75085fa
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nickslinetoolsrust"
version = "1.0.1"
version = "1.2.0"
authors = ["thehappycheese"]
repository = "https://github.com/thehappycheese/nicks_line_tools_rust.git"
readme = "README.MD"
Expand Down
38 changes: 37 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [2.4. `.interpolate()`](#24-interpolate)
- [2.5. `.offset_basic()`](#25-offset_basic)
- [2.6. Converting to `Vec<Vector2>`](#26-converting-to-vecvector2)
- [2.7. Converting to `Vec<(f64,f64)>`](#27-converting-to-vecf64f64)
- [2.8. Converting to `Vec<(f64,f64,f64)>`](#28-converting-to-vecf64f64f64)
- [3. Struct `Vector2`](#3-struct-vector2)

## 1. Introduction
Expand Down Expand Up @@ -99,7 +101,41 @@ assert!(
Vector2::new(1f64, 0f64),
Vector2::new(1f64, 1f64),
]
)
);
```

### 2.7. Converting to `Vec<(f64,f64)>`

Use the `into_tuples()` function to convert a multi line string into `Vec<(f64,f64)>` objects.

```rust
let into_tuples = ls.into_tuples();

assert_eq!(
into_tuples,
vec![
(0f64, 0f64),
(1f64, 0f64),
(1f64, 1f64),
]
);
```

### 2.8. Converting to `Vec<(f64,f64,f64)>`

Use the `into_tuples_measured()` function to convert a multi line string into `Vec<(f64,f64,f64)>` objects.

```rust
let into_tuples_measured = ls.into_tuples_measured(2.0, 10.0);

assert_eq!(
into_tuples_measured,
vec![
(0f64, 0f64, 2f64),
(1f64, 0f64, 6f64),
(1f64, 1f64, 10f64),
]
);
```

## 3. Struct `Vector2`
Expand Down
24 changes: 24 additions & 0 deletions src/line_string_measured.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,34 @@ impl Into<Vec<Vector2>> for &LineStringMeasured {
impl LineStringMeasured {

/// The into trait is difficult to call without a helper function. All this does is call the into trait.
#[deprecated(since="1.2.0", note="please use `into_tuples` or `into_tuples_measured` instead")]
pub fn into_vector2(&self) -> Vec<Vector2>{
self.into()
}

pub fn into_tuples(&self)-> Vec<(f64,f64)>{
let mut result:Vec<(f64,f64)> = self.segments.iter().map(|seg|(&seg.a).into()).collect();
if let Some(seg) = self.segments.last(){
result.push((&seg.b).into())
}
result
}

pub fn into_tuples_measured(&self, from_measure:f64, to_measure:f64) -> Vec<(f64,f64,f64)>{
let measure_len = to_measure-from_measure;
let scale = measure_len/self.mag;
let mut each_measure = from_measure;
let mut result:Vec<(f64,f64,f64)> = self.segments.iter().map(|seg|{
let result = (seg.a.x, seg.a.y, each_measure);
each_measure += seg.mag*scale;
result
}).collect();
if let Some(seg) = self.segments.last(){
result.push((seg.b.x,seg.b.y,to_measure))
}
result
}

pub fn magnitude(&self) -> f64 {
return self.mag;
}
Expand Down
52 changes: 51 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,54 @@ fn test_linestring_conversion_to_vec() {
Vector2::new(1.0, 3.0),
]
);
}
}


#[test]
fn test_multilinestring_conversion_to_tuples() {
let ls:LineStringMeasured = vec![
Vector2::new(0.0, 0.0),
Vector2::new(1.0, 1.0),
Vector2::new(1.5, 2.0),
Vector2::new(1.0, 3.0),
].into();
assert_eq!(
ls.into_tuples(),
vec![
(0.0, 0.0),
(1.0, 1.0),
(1.5, 2.0),
(1.0, 3.0),
]
);
}

#[test]
fn test_multilinestring_conversion_to_measured_tuples() {
let ls:LineStringMeasured = vec![
Vector2::new(0.0, 0.0),
Vector2::new(0.0, 2.0),//2
Vector2::new(4.0, 2.0),//4
Vector2::new(4.0, 10.0),//8
].into();
assert_eq!(
ls.into_tuples_measured(0.0, 14.0),
vec![
(0.0, 0.0, 0.0),
(0.0, 2.0, 2.0),
(4.0, 2.0, 6.0),
(4.0, 10.0, 14.0),
]
);

assert_eq!(
ls.into_tuples_measured(2.0, 30.0),
vec![
(0.0, 0.0, 2.0),
(0.0, 2.0, 6.0),
(4.0, 2.0, 14.0),
(4.0, 10.0, 30.0),
]
);

}
18 changes: 18 additions & 0 deletions src/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ impl Serialize for Vector2 {
}
}

impl Into<(f64,f64)> for &Vector2 {
fn into(self)->(f64,f64){
(self.x, self.y)
}
}
// impl From<&Vector2> for (f64,f64) {
// fn from(vec:&Vector2)->Self{
// (vec.x, vec.y)
// }
// }

impl Vector2 {
pub fn new(x: f64, y: f64) -> Vector2 {
Vector2 { x, y }
Expand Down Expand Up @@ -144,6 +155,13 @@ mod tests {
assert_eq!(v.y, 2.0);
}

#[test]
fn into_tuple() {
let v = Vector2::new(1.0, 2.0);
let vv:(f64,f64) = (&v).into();
assert_eq!(vv, (1.0,2.0));
}

#[test]
fn equality_derived() {
let v1 = Vector2::new(1.0, 2.0);
Expand Down

0 comments on commit 75085fa

Please sign in to comment.