# Transforming \`GeometryBasics.Ngon\` using \`CoordinateTransformations\`

**URL:** <https://discourse.julialang.org/t/transforming-geometrybasics-ngon-using-coordinatetransformations/42680>\
**Category:** General Usage\
**Created:** [July 7, 2020, 3:01pm UTC](https://discourse.julialang.org/t/transforming-geometrybasics-ngon-using-coordinatetransformations/42680 "2020-07-07T15:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lassepe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lassepe/32/10050_2.png) [@lassepe](https://discourse.julialang.org/u/lassepe)\
**Post date:** [July 7, 2020, 3:01pm UTC](https://discourse.julialang.org/t/transforming-geometrybasics-ngon-using-coordinatetransformations/42680/1 "2020-07-07T15:01:30Z")

</div>

I am trying to transform some `GeometryBasics.Line` objects using `CoordinateTransformations` but I can’t find a convenient way to transform anything other than `GeometryBasics.Point` out of the box.

For example, if I have

```julia
using GeometryBasics, CoordinateTransformations
using LinearAlgebra
l = Line(Point(0.0, 1.0), Point(1.0, 1.0))
mirror_transform = LinearMap(diagm(-1, 1))
lm = mirror_transform(l)

```

I would hope to get `lm == Line(Point(0.0, 1.0), Point(-1.0, 1.0))` (i.e. transformation of each point).  
**Are these transformations implemented for `GeomtryBasics` types somewhere?**

* * *

If this kind of transformation is not supported, what is a good strategy to extend `CoordinateTransformations` to achieve something like this? (It seems like various packages might already do something like this and there might be a best practice).  
A naive solution would be to `decompose` everything into `Point`s, apply the transformation on each of them, and then compose the polygon from these transformed points (but that seems very tedious and suboptimal).

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [July 22, 2020, 3:14pm UTC](https://discourse.julialang.org/t/transforming-geometrybasics-ngon-using-coordinatetransformations/42680/2 "2020-07-22T15:14:54Z")

</div>

The question is very specific to a Julia package, so this answer is only tangentially related.

Using [LazySets.jl](https://github.com/JuliaReach/LazySets.jl),

```julia
using LazySets, LinearAlgebra

l = Line(from=[0.0, 1.0], to=[1.0, 1.0]);
mirror_transform = diagm([-1., 1.]);

# take the concrete linear map of the line by the transfomation
lm = linear_map(mirror_transform, l)
  Line{Float64,Array{Float64,1}}([0.0, 1.0], [1.0, 0.0])

# check equivalence
(lm ⊆ l) && (l ⊆ lm)
  true

# or just
isequivalent(l, lm)
 true

# alternative using lazy set computations
X = mirror_transform * l
  LinearMap{Float64,Line{Float64,Array{Float64,1}},Float64,Array{Float64,2}}([-1.0 0.0; 0.0 1.0], Line{Float64,Array{Float64,1}}([0.0, 1.0], [-1.0, 0.0]))

isequivalent(X, l)
  true

# other example
p = rand(HPolygon, num_constraints=5)
mp = linear_map(mirror_transform, p)

using Plots

plot(p)
plot!(mp)

```

 ![Screenshot from 2020-07-22 12-13-59](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f5546b8b5be330bcf299005fa462ad6bd5129ec.png)

---

<div class="post-metadata">

**Author:** ![lassepe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lassepe/32/10050_2.png) [@lassepe](https://discourse.julialang.org/u/lassepe)\
**Post date:** [July 28, 2020, 6:22am UTC](https://discourse.julialang.org/t/transforming-geometrybasics-ngon-using-coordinatetransformations/42680/3 "2020-07-28T06:22:04Z")

</div>

Thank you @mforets for your help but I actually need to use the packages mentioned above so my question is rather package specific.

I ended up filling an [issue over at the CoordinateTransformation.jl](https://github.com/JuliaGeometry/CoordinateTransformations.jl/issues/65) repository so that this discussion is documented near the code.
