converted all to unix line-endings.
added doctests for geometry.py
This commit is contained in:
parent
15163e5818
commit
8cfefe8a80
133
geometry.py
133
geometry.py
@ -1,61 +1,72 @@
|
||||
"""
|
||||
Geometry functions
|
||||
"""
|
||||
import operator
|
||||
|
||||
def matrix_identity():
|
||||
"""
|
||||
>>> len(matrix_identity())
|
||||
6
|
||||
"""
|
||||
return (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
|
||||
|
||||
def matrix_multiply(m1, m2):
|
||||
"""Multiply two transformation matrices.
|
||||
"""
|
||||
d0 = m1[0] * m2[0] + m1[1] * m2[2];
|
||||
d1 = m1[0] * m2[1] + m1[1] * m2[3];
|
||||
d2 = m1[2] * m2[0] + m1[3] * m2[2];
|
||||
d3 = m1[2] * m2[1] + m1[3] * m2[3];
|
||||
d4 = m1[4] * m2[0] + m1[5] * m2[2] + m2[4];
|
||||
d5 = m1[4] * m2[1] + m1[5] * m2[3] + m2[5];
|
||||
return (d0, d1, d2, d3, d4, d5)
|
||||
|
||||
|
||||
def matrix_invert(m):
|
||||
"""
|
||||
"""
|
||||
r_det = 1.0 / (m[0] * m[3] - m[1] * m[2]);
|
||||
dst0 = m[3] * r_det;
|
||||
dst1 = -m[1] * r_det;
|
||||
dst2 = -m[2] * r_det;
|
||||
dst3 = m[0] * r_det;
|
||||
dst4 = -m[4] * dst[0] - m[5] * dst[2];
|
||||
dst5 = -m[4] * dst[1] - m[5] * dst[3];
|
||||
return (dst0, dst1, dst2, dst3, dst4, dst5)
|
||||
|
||||
def matrix_scale(m, sx, sy):
|
||||
"""
|
||||
"""
|
||||
m2 = (float(sx), 0.0, 0.0, float(sy), 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
def matrix_rotate(m, radians):
|
||||
"""
|
||||
"""
|
||||
s = math.sin(radians)
|
||||
c = math.cos(radians)
|
||||
m2 = (c, s, -s, c, 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
def matrix_shear(m, radians):
|
||||
"""
|
||||
"""
|
||||
t = math.tan(radians)
|
||||
m2 = (1.0, 0.0, t, 1.0, 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
if __name__ = '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
"""
|
||||
Geometry functions
|
||||
"""
|
||||
import operator
|
||||
|
||||
def matrix_identity():
|
||||
"""
|
||||
>>> len(matrix_identity())
|
||||
6
|
||||
"""
|
||||
return (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
|
||||
|
||||
def matrix_multiply(m1, m2):
|
||||
"""Multiply two transformation matrices.
|
||||
>>> m1 = ( 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 )
|
||||
>>> m2 = ( 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 )
|
||||
>>> matrix_multiply(m1, m2)
|
||||
(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
|
||||
>>> m1 = ( 1.2, 0.0, 0.7, 1.0, 0.0, 0.0 )
|
||||
>>> m2 = ( 1.0, 0.4, 0.0, 1.0, 8.0, 0.0 )
|
||||
>>> matrix_multiply(m1, m2)
|
||||
(1.2, 0.47999999999999998, 0.69999999999999996, 1.28, 8.0, 0.0)
|
||||
"""
|
||||
d0 = m1[0] * m2[0] + m1[1] * m2[2];
|
||||
d1 = m1[0] * m2[1] + m1[1] * m2[3];
|
||||
d2 = m1[2] * m2[0] + m1[3] * m2[2];
|
||||
d3 = m1[2] * m2[1] + m1[3] * m2[3];
|
||||
d4 = m1[4] * m2[0] + m1[5] * m2[2] + m2[4];
|
||||
d5 = m1[4] * m2[1] + m1[5] * m2[3] + m2[5];
|
||||
return (d0, d1, d2, d3, d4, d5)
|
||||
|
||||
|
||||
def matrix_invert(m):
|
||||
"""
|
||||
>>> m1 = ( 1.0, 0.4, 0.0, 1.0, 8.0, 0.0 )
|
||||
>>> matrix_invert(m1)
|
||||
(1.0, -0.40000000000000002, 0.0, 1.0, -8.0, 3.2000000000000002)
|
||||
"""
|
||||
r_det = 1.0 / (m[0] * m[3] - m[1] * m[2]);
|
||||
dst0 = m[3] * r_det;
|
||||
dst1 = -m[1] * r_det;
|
||||
dst2 = -m[2] * r_det;
|
||||
dst3 = m[0] * r_det;
|
||||
dst4 = -m[4] * dst0 - m[5] * dst2;
|
||||
dst5 = -m[4] * dst1 - m[5] * dst3;
|
||||
return (dst0, dst1, dst2, dst3, dst4, dst5)
|
||||
|
||||
def matrix_scale(m, sx, sy):
|
||||
"""
|
||||
"""
|
||||
m2 = (float(sx), 0.0, 0.0, float(sy), 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
def matrix_rotate(m, radians):
|
||||
"""
|
||||
"""
|
||||
s = math.sin(radians)
|
||||
c = math.cos(radians)
|
||||
m2 = (c, s, -s, c, 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
def matrix_shear(m, radians):
|
||||
"""
|
||||
"""
|
||||
t = math.tan(radians)
|
||||
m2 = (1.0, 0.0, t, 1.0, 0.0, 0.0)
|
||||
return matrix_multiply(m, m2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
146
item.py
146
item.py
@ -1,73 +1,73 @@
|
||||
""" Item and Handle.
|
||||
"""
|
||||
|
||||
class Handle(object):
|
||||
|
||||
def __init__(self, pos=(0,0)):
|
||||
self._pos = pos
|
||||
|
||||
pos = property(lambda s: s._pos)
|
||||
|
||||
def update(self, context):
|
||||
"""Update the handle. @context has the following attributes:
|
||||
- item: the owning item
|
||||
- matrix_i2w: Item to World transformation matrix
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Item(object):
|
||||
|
||||
def __init__(self, canvas=None):
|
||||
self._canvas = canvas
|
||||
self.dirty = True
|
||||
|
||||
def _set_canvas(self, canvas):
|
||||
assert not canvas or not self._canvas or self._canvas is canvas
|
||||
self._canvas = canvas
|
||||
|
||||
def _del_canvas(self):
|
||||
self._canvas = None
|
||||
|
||||
canvas = property(lambda s: s._canvas, _set_canvas, _del_canvas)
|
||||
|
||||
def request_update(self):
|
||||
if self._canvas:
|
||||
self._canvas.request_update(self)
|
||||
|
||||
def pre_update(self, context):
|
||||
"""Do small things that have to be done before the "real" update.
|
||||
Context has the following attributes:
|
||||
- canvas: the owning canvas
|
||||
- matrix_i2w: Item to World transformation matrix
|
||||
- ... (do I need something for text processing?)
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def update(self, context):
|
||||
"""Like pre_update(), but this is step 2.
|
||||
"""
|
||||
pass
|
||||
|
||||
def render(self, context):
|
||||
"""Render the item to a canvas view.
|
||||
Context contains the following attributes:
|
||||
- matrix_i2w: Item to World transformation matrix (no need to)
|
||||
- cairo: the Cairo Context use this one to draw.
|
||||
"""
|
||||
pass
|
||||
|
||||
def handles(self):
|
||||
"""Return an iterator for the handles owned by the item.
|
||||
"""
|
||||
return iter()
|
||||
|
||||
def point(self, context, x, y):
|
||||
"""Get the distance from a point (@x, @y) to the item.
|
||||
@x and @y are in item coordinates.
|
||||
Context contains the following attributes:
|
||||
- matrix_i2w: Item to World transformation matrix (no need to)
|
||||
- cairo: the Cairo Context use this one to draw.
|
||||
"""
|
||||
pass
|
||||
""" Item and Handle.
|
||||
"""
|
||||
|
||||
class Handle(object):
|
||||
|
||||
def __init__(self, pos=(0,0)):
|
||||
self._pos = pos
|
||||
|
||||
pos = property(lambda s: s._pos)
|
||||
|
||||
def update(self, context):
|
||||
"""Update the handle. @context has the following attributes:
|
||||
- item: the owning item
|
||||
- matrix_i2w: Item to World transformation matrix
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Item(object):
|
||||
|
||||
def __init__(self, canvas=None):
|
||||
self._canvas = canvas
|
||||
self.dirty = True
|
||||
|
||||
def _set_canvas(self, canvas):
|
||||
assert not canvas or not self._canvas or self._canvas is canvas
|
||||
self._canvas = canvas
|
||||
|
||||
def _del_canvas(self):
|
||||
self._canvas = None
|
||||
|
||||
canvas = property(lambda s: s._canvas, _set_canvas, _del_canvas)
|
||||
|
||||
def request_update(self):
|
||||
if self._canvas:
|
||||
self._canvas.request_update(self)
|
||||
|
||||
def pre_update(self, context):
|
||||
"""Do small things that have to be done before the "real" update.
|
||||
Context has the following attributes:
|
||||
- canvas: the owning canvas
|
||||
- matrix_i2w: Item to World transformation matrix
|
||||
- ... (do I need something for text processing?)
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def update(self, context):
|
||||
"""Like pre_update(), but this is step 2.
|
||||
"""
|
||||
pass
|
||||
|
||||
def render(self, context):
|
||||
"""Render the item to a canvas view.
|
||||
Context contains the following attributes:
|
||||
- matrix_i2w: Item to World transformation matrix (no need to)
|
||||
- cairo: the Cairo Context use this one to draw.
|
||||
"""
|
||||
pass
|
||||
|
||||
def handles(self):
|
||||
"""Return an iterator for the handles owned by the item.
|
||||
"""
|
||||
return iter()
|
||||
|
||||
def point(self, context, x, y):
|
||||
"""Get the distance from a point (@x, @y) to the item.
|
||||
@x and @y are in item coordinates.
|
||||
Context contains the following attributes:
|
||||
- matrix_i2w: Item to World transformation matrix (no need to)
|
||||
- cairo: the Cairo Context use this one to draw.
|
||||
"""
|
||||
pass
|
||||
|
46
tool.py
46
tool.py
@ -1,23 +1,23 @@
|
||||
"""
|
||||
"""
|
||||
|
||||
class Tool(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def on_button_press(self, view, event):
|
||||
pass
|
||||
|
||||
def on_button_release(self, view, event):
|
||||
pass
|
||||
|
||||
def on_motion_notify(self, view, event):
|
||||
pass
|
||||
|
||||
def on_key_press(self, view, event):
|
||||
pass
|
||||
|
||||
def on_key_release(self, view, event):
|
||||
pass
|
||||
|
||||
"""
|
||||
"""
|
||||
|
||||
class Tool(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def on_button_press(self, view, event):
|
||||
pass
|
||||
|
||||
def on_button_release(self, view, event):
|
||||
pass
|
||||
|
||||
def on_motion_notify(self, view, event):
|
||||
pass
|
||||
|
||||
def on_key_press(self, view, event):
|
||||
pass
|
||||
|
||||
def on_key_release(self, view, event):
|
||||
pass
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user