5

Is there a simple geometry library for C++?

Just classes for points and lines, and the most basic and common functions such as getting the line length via Pythagorean theorem, calculating the intersection point of 2 lines, checking if 2 lines are parallel, things like that.

Bonus points if it is just a header and source file so I can include it directly into projects without building it separately.

Old Stack Overflow question for reference: Geometry library in C++.

2 Answers 2

2

You can use the Cairo libraries,

They may not be the easiest around, but they sure are enstablished. Inkscape and many other top notch projects use them. They also provide font rendering and a lot of other stuff.

They were proposed to become part of the standard library in the next iteration of ISO C++.

Not sure if that proposal went through, but you get the idea of how interoperable and well built those libraries are.

1
  • 2
    I see Cairo is for drawing graphics. I just want to clarify that my question was about the computational part. Such a library could be used by programs that read a bunch of coordinates from a text file and store the results in a text file.
    – sashoalm
    Feb 5, 2015 at 9:14
1

Depends on what you want.

Boost has two geometry libraries, boost::geometry which is not robust but has advanced stuff like non-euclidean geometry (such as latitude/longitude on the Earth) and boost::polygon which is robust but doesn't have as many features.

CGAL is another popular one, which is also very robust but can sometimes be very slow. CGAL has a lot of algorithms but its heavy template style may not be considered "simple" depending on your use-case. Furthermore, some parts of CGAL use the more strict GPL license (as opposed to LGPL), which some people may find problematic for a library. There is a commercial version you can pay for to work around those limitations.

Wykobi is less popular but still very good. It is not robust.

Robust libraries always provide things like topological guarantees, exact predicates etc. Non-robust libraries are usually faster but may produce geometrically or topologically incorrect results in some situations.

2
  • Where did you find non-free software in CGAL? Jun 5, 2021 at 12:22
  • @MarcGlisse Thanks for pointing out that CGAL doesn't have non-free software, it is just dual licensed. I have updated my answer to clarify it. Jun 6, 2021 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.