1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#!/usr/bin/env python
# Copyright (C) 2011, One Laptop Per Child
# Author, Gonzalo Odiard <gonzalo@laptop.org>
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango, PangoCairo
import math
import logging
t1 = "[(130.0, 58.0), (111, 77), (84, 115), (73, 137), (64, 172), (55, 195), (51, 230), (53, 273), (61, 284), (107, 305), (130, 305), (171, 300), (189, 291), (214, 279), (241, 249), (284, 202), (308, 181), (333, 156), (355, 141), (391, 126), (409, 113), (447, 101), (468, 100), (499, 101), (508, 107), (519, 133), (522, 132)]"
class ShowPoints(Gtk.DrawingArea):
def __init__(self):
self.points = []
self._mark_next = False
self._last_crossproduct = 0
super(ShowPoints, self).__init__()
self.connect('draw', self.__draw_cb)
def __draw_cb(self, widget, ctx):
i = 0
EPS = 100
for point in self.points:
if self._mark_next:
ctx.set_source_rgba(1, 0, 0, 0.7)
else:
ctx.set_source_rgba(0.3, 0.3, 0.3, 0.7)
self._mark_next = False
crossproduct = 0
if i < len(self.points) - 3:
# X_ab, Y_ab - coordinates of vector B-A.
X_ab = float(self.points[i +1][0] - self.points[i][0])
Y_ab = float(self.points[i +1][1] - self.points[i][1])
# X_ac, Y_ac - coordinates of vector C-A.
X_ac = float(self.points[i +2][0] - self.points[i][0])
Y_ac = float(self.points[i +2][1] - self.points[i][1])
crossproduct = Y_ab * X_ac - X_ab * Y_ac
if abs(crossproduct) < EPS: # if crossprudct == 0
# on the same line.
innerproduct = X_ab * X_ac + Y_ab * Y_ac;
logging.error('innerproduct %f', innerproduct)
if innerproduct > 0:
self._mark_next = True
i = i + 1
ctx.save()
x, y = point[0], point[1]
logging.error('point %d %d', point[0], point[1])
ctx.arc(x, y, 5, 0., 2 * math.pi)
ctx.fill()
pango_layout = PangoCairo.create_layout(ctx)
fd = Pango.FontDescription('Sans 10')
pango_layout.set_font_description(fd)
label = str(self._last_crossproduct)
pango_layout.set_text(unicode(label), len(unicode(label)))
ctx.move_to(x + 20, y + 20)
PangoCairo.show_layout(ctx, pango_layout)
ctx.stroke()
ctx.restore()
self._last_crossproduct = crossproduct
class SPWin(Gtk.Window):
def __init__(self):
super(Gtk.Window, self).__init__( )
vbox = Gtk.VBox()
self.add(vbox)
self.show_points = ShowPoints()
vbox.add(self.show_points)
self.entry = Gtk.Entry()
self.entry.set_text(t1)
vbox.add(self.entry)
self.entry.connect('activate', self.show_points_from_entry)
self.connect("destroy", Gtk.main_quit)
self.show_all()
self.maximize()
def show_points_from_entry(self, entry):
self.show_points.points = self.parse_points(entry.get_text())
self.show_points.queue_draw()
def parse_points(self, text):
points_list = []
pairs = text.split('),')
for pair in pairs:
parts = pair.split(',')
xs = parts[0].replace('[','').replace('(','').replace(')','').replace(']','')
ys = parts[1].replace('[','').replace('(','').replace(')','').replace(']','')
x, y = int(float(xs)), int(float(ys))
points_list.append((x, y))
return points_list
#def update_points(entry):
def main():
window = SPWin()
Gtk.main()
if __name__ == "__main__":
main()
|