Introduction

The scikit-geometry package contains a whole lot of geometric algorithms. For the moment, scikit-geometry derives most of it’s functionality from the mature and stable Computational Geometry Algorithms Library (CGAL) package, a collection of geometric algorithms written in C++.

Basic Geometric Types

We aim to cleanly encapsulate the important building blocks of a geometric library. As such we have implementations for the basic geometric types:

  • Point2, Point3

  • Segment2, Ray2, Line2, Segment3, Ray3, Line3

  • Vector2, Vector3

  • Circle2

  • IsoRectangle2, Bbox2

  • Plane3, Triangle3

  • Polyhedron3

All sorts of operations can be performed with these basic building blocks. For example, one can construct a new segment from two points, then test against intersection with another segment, or a line.

[1]:
import skgeom as sg
a = sg.Point2(5, 3)
b = sg.Point2(10, 8)
print(a, b)
PointC2(5, 3) PointC2(10, 8)

Now we can create a vector from two points by subtracting one from another.

[2]:
v = a - b
print(v)
VectorC2(-5, -5)

Or a segment between two points, where a is the source and b the target.

[3]:
s = sg.Segment2(a, b)
print(s)
Segment_2(PointC2(5, 3), PointC2(10, 8))

And a ray from a point and a vector which indicates the direction.

[4]:
r = sg.Ray2(a, v)
print(r)
RayC2(PointC2(5, 3), PointC2(-0, -2))

Finding intersections

It’s possible to find intersections between two geometric primitives by using the intersection function:

[5]:
a = sg.Segment2(sg.Point2(3, 5), sg.Point2(0, -2))
b = sg.Segment2(sg.Point2(5, 3), sg.Point2(-2, -2))
i = sg.intersection(a, b)
print(i)
PointC2(0.882353, 0.0588235)

Note: depending on the type of intersection, the return type could also be None or Segment2 if there is a complete overlap between the two elements.

Drawing functions

We have functionality to draw some of the geometric primitives presented here.

[6]:
from skgeom.draw import draw
[7]:
draw(s)
_images/introduction_14_0.svg
[8]:
from random import random as r
import itertools
segments = []
for i in range(10):
    segments.append(sg.Segment2(sg.Point2(r(), r()),
                                sg.Point2(r(), r())))

intersections = []
for s1, s2 in itertools.permutations(segments, 2):
    isect = sg.intersection(s1, s2)
    if isect:
        intersections.append(isect)

for s in segments:
    draw(s)
for i in intersections:
    draw(i)
_images/introduction_15_0.svg

Further functionality

Of course, scikit-geometry comes with much more functionality.

  • Compute convex hulls from points

  • Visibility polygons from two-dimensional arrangements

  • Compute the Minkowski sum of two polygons

[ ]: