Goulib.geom

Vector, matrix and quaternion operations + line, arc, circle entities for use in 2D and 3D graphics applications.


In [1]:
from Goulib.notebook import *
from Goulib.geom import *
import sys,inspect
print('geom defines the following classes : %s'%dict(inspect.getmembers(sys.modules['Goulib.geom'], inspect.isclass)).keys())


geom defines the following classes : dict_keys(['Arc2', 'Circle', 'Ellipse', 'Geometry', 'Line2', 'Matrix3', 'Point2', 'Polygon', 'Ray2', 'Segment2', 'Surface', 'Vector2'])

In [2]:
Arc2((0,0),(0,1),(1,0)) #does not render in notebook


Out[2]:
Arc2(center=Point2(0, 0),p1=Point2(0, 1),p2=Point2(1, 0),r=1.0)

In [3]:
from Goulib.drawing import * #adds rendering (and more) to Geom
a=Arc2((0,0),(0,1),(1,0)) #same class is now rendered inline
a


Out[3]:

In [4]:
l1=Segment2((-2,.5),Vector2(4,0)) #horizontal at y=0.5
l2=Segment2((-2,-.5),Vector2(4,0)) #horizontal at y=-0.5
lines=Group([l1,l2])
lines.color='blue'
Group([lines,a])


Out[4]:

In [5]:
pts=Group([i[0] for i in lines.intersect(a)]) # list of intersection points
Group([lines,a,pts])


Out[5]:

In [6]:
c1=circle_from_3_points(*pts) # classic
a1=arc_from_3_points(*pts) # not trivial ;-)
a1.r*=.99
a1.color='blue'
Group([pts,c1,a1])


Out[6]:

Goulib.drawing

vector graphics in .dxf, .svg and .pdf formats based on Geom


In [7]:
from Goulib.drawing import *
import inspect
h('drawing adds more classes to geom : %s'%dict(inspect.getmembers(sys.modules['Goulib.drawing'], inspect.isclass)).keys())


drawing adds more classes to geom : dict_keys(['Arc2', 'BBox', 'Box', 'Chain', 'Circle', 'Drawing', 'Ellipse', 'Entity', 'Geometry', 'Group', 'Instance', 'Line2', 'Matrix3', 'Plot', 'Point2', 'Polygon', 'Ray2', 'Rect', 'Segment2', 'Spline', 'Surface', 'Text', 'Vector2', '_Group'])

In [8]:
c1=Circle(Point2(4,4),1)
#drawing Entities support colors and othe graphic attributes
c1.color='blue'
c1.width=5
c1.fill='cyan'
r1=Rect((0,0),(-1,1))
r2=Rect((1,-1),(2,2))
c2=Circle(Point2(0,2),.5)

In [9]:
from Goulib.table import Table
Table([[c1,c2],[r1,r2]]) # Entities have an HTML representation for tables


Out[9]:

Geom offers many geometric constructs


In [10]:
s1=r1.connect(r2)
s1.color='red'
s2=r2.connect(c1)
s2.color='red'
s3=c1.connect(c2)
s3.color='red'

Geom entities and others defined in Drawing can be grouped :


In [11]:
g=Group([r1,r2,c1,c2,s1,s2,s3])
g


Out[11]:

Groups can be handled as entities :


In [12]:
g2=Trans(scale=2, offset=(10,1), rotation=30)*g
g2.color='blue'
h(g.distance(g2))
Group([g,g2]) #group of groups


2.026833782163534
Out[12]:

Drawing objects can be read/saved from/to various formats including pdf, svg and dxf


In [13]:
Drawing('../tests/data/drawing.pdf')


Out[13]:

In [14]:
Drawing('../tests/data/drawing.svg')


Out[14]:

In [15]:
Drawing('../tests/data/drawing.dxf')


Out[15]:

Drawing also extends geom to allow geometric constructions


In [16]:
from Goulib.colors import color_range
d=Drawing()
for i,color in enumerate(color_range(100,'blue','orange')):
    circle=Circle((i*.1,sin(.1*i)),1)
    circle.color=color.hex
    d.append(circle)
d


Out[16]:

In [17]:
Drawing('../tests/data/switzerlandLow.svg')


Out[17]: