Search code examples
cgal

Proper way of adding CGAL points api-wise


I have a triangle defined by its three vertices. The vertices are of type Point = CGAL::Point_2<K> with a Simple_cartersian<double> kernel.

I want to randomly sample this triangle and for that I use a formula (https://math.stackexchange.com/questions/18686/uniform-random-point-in-triangle-in-3d) which adds the three vertices of the triangle multiplied by some random factors.

                Point = Point(0, 0) +                                                //
                        (1 - std::sqrt(r1)) * (standardTriangle[0] - Point(0, 0)) +  //
                        (std::sqrt(r1) * (1 - r2)) * (standardTriangle[1] - Point(0, 0)) +
                        (r2 * std::sqrt(r1)) * (standardTriangle[2] - Point(0, 0)));

This looks very cumbersome, as I need to convert the points to vector by substracting Point(0,0) and then I need to add everything to a Point on the origin.

Looks more natural to just do something like the following

                Point = (1 - std::sqrt(r1)) * standardTriangle[0] +  //
                        (std::sqrt(r1) * (1 - r2)) * standardTriangle[1] +
                        (r2 * std::sqrt(r1)) * standardTriangle[2]);

Adding and removing points from the origin is really the only way to sum points, even though mathematically this is not correct?


Solution

  • You might want to use the barycenter() function.

    In case you need another sampling there is one available in CGAL. See here