Mercurial > repos > public > sbplib_julia
comparison src/Grids/geometry.jl @ 1970:deeb61325320 feature/grids/geometry_functions
Simplify constructors for Line and LineSegment
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Fri, 14 Feb 2025 08:28:50 +0100 |
| parents | 7f4a5146c84c |
| children | 9e89084cbba4 |
comparison
equal
deleted
inserted
replaced
| 1969:7f4a5146c84c | 1970:deeb61325320 |
|---|---|
| 4 | 4 |
| 5 Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) | 5 Line{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) |
| 6 end | 6 end |
| 7 | 7 |
| 8 function Line(p, t) | 8 function Line(p, t) |
| 9 S = length(p) | 9 p = SVector{length(p)}(p) |
| 10 T = promote_type(eltype(p), eltype(t)) | 10 t = SVector{length(t)}(t) |
| 11 p, t = promote(p, t) | |
| 11 | 12 |
| 12 PT = SVector{S,T} | 13 return Line{typeof(p)}(p,t) |
| 13 return Line{PT}( | |
| 14 convert(PT, p), | |
| 15 convert(PT, t), | |
| 16 ) | |
| 17 end | |
| 18 | |
| 19 function Line(p::Tuple, t::Tuple) | |
| 20 p = promote(p...) | |
| 21 t = promote(t...) | |
| 22 | |
| 23 return Line(SVector(p), SVector(t)) | |
| 24 end | 14 end |
| 25 | 15 |
| 26 (c::Line)(s) = c.p + s*c.tangent | 16 (c::Line)(s) = c.p + s*c.tangent |
| 27 | 17 |
| 28 | 18 |
| 32 | 22 |
| 33 LineSegment{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) | 23 LineSegment{PT}(p::PT, tangent::PT) where PT = new{PT}(p,tangent) |
| 34 end | 24 end |
| 35 | 25 |
| 36 function LineSegment(a, b) | 26 function LineSegment(a, b) |
| 37 S = length(a) | 27 a = SVector{length(a)}(a) |
| 38 T = promote_type(eltype(a), eltype(b)) | 28 b = SVector{length(b)}(b) |
| 29 a, b = promote(a, b) | |
| 39 | 30 |
| 40 PT = SVector{S,T} | 31 return LineSegment{typeof(a)}(a,b) |
| 41 return LineSegment{PT}( | |
| 42 convert(PT, a), | |
| 43 convert(PT, b), | |
| 44 ) | |
| 45 end | |
| 46 | |
| 47 function LineSegment(a::Tuple, b::Tuple) | |
| 48 a = promote(a...) | |
| 49 b = promote(b...) | |
| 50 | |
| 51 return LineSegment(SVector(a), SVector(b)) | |
| 52 end | 32 end |
| 53 | 33 |
| 54 (c::LineSegment)(s) = (1-s)*c.a + s*c.b | 34 (c::LineSegment)(s) = (1-s)*c.a + s*c.b |
| 55 | 35 |
| 56 | 36 |
