Search code examples
c++polygoncgal

Translate CGAL Polygon without looping through vertices


I just started with CGAL, hence my question since I feel you should be able to translate a CGAL::Polygon_2 more easily than looping through the vertices.

The current way I do it is this:

        //Headers and typdef
        #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
        #include <CGAL/Boolean_set_operations_2.h>
        #include <CGAL/aff_transformation_tags.h>
        typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
        typedef Kernel::Point_2                     Point_2;
        typedef CGAL::Polygon_2<Kernel>             Polygon_2;
        typedef CGAL::Aff_transformation_2<Kernel>  Transformation;

        //Declare Polygon
        Polygon_2 P;
        P.push_back(Point_2(0, 0));
        P.push_back(Point_2(5, 0));
        P.push_back(Point_2(3.5, 1.5));

        //Loop through vertices and translate
        Transformation translate(CGAL::TRANSLATION, CGAL_Vector(0.2, 0));
        typename CGAL::Polygon_2<Kernel>::Vertex_iterator vit;

        //NOTE: This is the for loop that looks inefficient to me.
        for (vit = P.vertices_begin(); vit != P.vertices_end(); ++vit) {
            *vit = translate(*vit);
        }

Can somebody give me an indication whether this is the correct way to do this or give my pointers how to do it better.


Solution

  • You can call the transform free function that will do exactly what you want.

    So in code, do:

    Transformation translate(CGAL::TRANSLATION, CGAL_Vector(0.4, 0));
    P = transform(translate, P);