Skip to main content

Line clipping: Cohen-Sutherland

Project description

Actions Status

PyPi versions PyPi Download stats

Line clipping

  • lineclipping.f90 Cohen-Sutherland line clipping algorithm for massively parallel coarray modern Fortran. Input scalars or arrays, output intersections or NaN if no intersection.
  • Cohen-Sutherland line clipping algorithm for Python. Input scalars, output intersection length, or None if no intersection.

Julia line clipping is at


To install the latest release:

pip install pylineclip


If you want to use the Fortran Cohen-Sutherland line clipping modules directly (optional):

meson build

meson test -C build


The main difference with textbook implementations is that I return a sentinel value (NaN, None, nothing) if there's no intersection of line with box.


import pylineclip.lineclipping as lc

x3,y3,x4,y4 = lc.cohensutherland((xmin, ymax, xmax, ymin, x1, y1, x2, y2)

If no intersection, (None, None, None, None) is returned.


lineclipping.f90 has two subroutines. Pick Ccohensutherland if you're calling from C/C++/Python, which cannot tolerate assummed-shape arrays. It's a slim wrapper to cohensutherland which is elemental (can handle scalar or any rank array).

Fortran programs will simply use

use lineclipping
call cohensutherland(xmin,ymax,xmax,ymin,x1,y1,x2,y2)

The arguments are:

xmin,ymax,xmax,ymin:  upper left and lower right corners of box (pixel coordinates)

in - endpoints of line
out - intersection points with box. If no intersection, all NaN

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylineclip-1.0.0.tar.gz (4.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page