The previous articles covered how to solve limits, derivatives, integrals and solving systems of linear equations. Now I will explain the use of Sympy in analytic geometry.

The articles on analytic geometry are based on an article in English, Computational Geomtry in Python.

This article explains how to work with segments.

Start ipython notebook (if you want to use the file for this article you can download it from the following link (broken link)).

Below is the script code:

#!/usr/bin/env python



from sympy.geometry import *



#Definicion de puntos

P1 = Point(0, 0)

P2 = Point(3, 4)

P3 = Point(2, -1)

P4 = Point(-1, 5)



#Definicion de segmentos

S1 = Segment(P1, P2)

S2 = Segment(P3, P4)



print Point.is_collinear(P1, P2, P3); #Los puntos son colineales?



print S1.length ; #longitud del segmento 1



print S2.midpoint ;#punto medio del segmento 2



print S1.slope; #Slope del segmento 1



print S1.intersection(S2) ; #Intercepcion entre el Segmento 1 y 2



print Segment.angle_between(S1, S2);#Angulo entre el segmento 1 y 2



print S1.contains(P3);#El segmento  1 contiene a P3?

The result of running the script is shown below:

False

5

Point(1/2, 2)

4/3

[Point(9/10, 6/5)]

acos(-sqrt(5)/5)

False

The following image shows the notebook running:

Make a donation! If you liked this article you can donate with Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV

Or by scanning the QR code from your wallet:

17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV