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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# Copyright 2008 by Peter Moxhay and Wade Brainerd.
# This file is part of Math.
#
# Math is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Math is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Math. If not, see <http://www.gnu.org/licenses/>.
from objectarea import Object
from vector import Vector
from shapeobject import ShapeObject
import gtk, math
# The global grid unit size, in pixels. Objects will snap to multiples of this value.
GRID_SIZE = 50
# Width and height of region within which we can drag objects.
DRAGGING_RECT_WIDTH = 24*GRID_SIZE
DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
class VolumeObject(ShapeObject):
"""Quasi three-dimensional container object."""
def __init__(self, color, symbol, pos, height = 400, lower_radius = 50, upper_radius = 100):
ShapeObject.__init__(self, color, symbol, [ Vector(0, 0), Vector(2.0 * upper_radius, 0), Vector(lower_radius + upper_radius, height), \
Vector(upper_radius - lower_radius , height) ], pos, 0.0, Vector(0, 0), False)
self.color = color
self.symbol = symbol
self.height = height
self.lower_radius = lower_radius
self.upper_radius = upper_radius
self.pos = pos
self.volume = self.calculate_volume()
self.selectable = True
self.symbol_visible = True
self.rotatable = False
self.calculate_bounds()
print "VolumeObject constructor: volume =", self.volume
def calculate_volume(self):
return (math.pi * self.height / 3.0) *(self.lower_radius * self.lower_radius + self.lower_radius * self.upper_radius + self.upper_radius * self.upper_radius)
# Modify to take into account trapezoids above and to the right.
def is_in_container(self):
for p in self.points:
p = self.transform_point(p)
if p.x < -2 or p.x > DRAGGING_RECT_WIDTH + 2 - 50 or \
p.y < -1 + 50 or p.y > DRAGGING_RECT_HEIGHT:
return False
return True
def draw_ellipse(self, cr, x, y, width, height):
cr.save();
cr.translate (x + width / 2., y + height / 2.);
cr.scale(width / 2., height / 2.);
cr.arc(0., 0., 1., 0., 2 * math.pi);
cr.restore();
def draw(self, cr):
cr.scale(self.scale, self.scale)
# Transform the points.
points = [self.transform_point(p) for p in self.points]
# Generate the shape.
cr.move_to(points[1].x, points[1].y)
cr.line_to(points[2].x, points[2].y)
# Draw the outline.
if self.selected:
cr.set_dash((10, 10), 0)
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.set_line_width(4.0)
cr.stroke()
# Generate the shape.
cr.move_to(points[0].x, points[0].y)
cr.line_to(points[3].x, points[3].y)
# Draw the outline.
if self.selected:
cr.set_dash((10, 10), 0)
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.set_line_width(4.0)
cr.stroke()
# Draw the lower ellipse
cr.save()
if self.selected:
cr.set_dash((10, 10), 0)
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.set_line_width(4.0)
self.draw_ellipse(cr, self.pos.x - self.lower_radius, self.pos.y + self.height/2.0 - self.lower_radius/4.0, 2.0 * self.lower_radius, self.lower_radius/2.0)
cr.stroke()
cr.restore()
# Draw the upper ellipse
cr.save()
if self.selected:
cr.set_dash((10, 10), 0)
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.set_line_width(4.0)
#cr.arc(self.pos.x, self.pos.y - self.size.y/2, self.size.x/2, 0.0, 2.0 * math.pi)
self.draw_ellipse(cr, self.pos.x - self.upper_radius, self.pos.y - self.height/2.0 - self.upper_radius/4.0, 2.0 * self.upper_radius, self.upper_radius/2.0)
cr.stroke()
cr.restore()
# Draw the symbol (capital letter representing the shapes's area).
if self.symbol_visible:
cr.set_source_rgb(0, 0, 0)
cr.set_font_size(50)
x_bearing, y_bearing, width, height = cr.text_extents(self.symbol)[:4]
cr.move_to(self.pos.x - x_bearing - width/2, self.pos.y - y_bearing - height/2)
cr.show_text(self.symbol)
## Algorithm to test whether point is inside the polygon
#def contains_point(self, pos):
# n = 0
# p = pos
#
# for i in range (0, len(self.points) ):
# p1 = self.points[i]
# p2 = self.points[(i+1) % len(self.points)]
#
# p1 = self.transform_point(p1)
# p2 = self.transform_point(p2)
#
# if p.y > min(p1.y, p2.y):
# if p.y <= max(p1.y, p2.y):
# if p.x <= max(p1.x, p2.x):
# if p1.y != p2.y:
# x = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x
# if p1.x == p2.x or p.x <= x:
# n = n + 1
#
# if n % 2 == 0:
# return(False)
# else:
# return(True)
#
#def is_in_container(self):
# for p in self.points:
# p = self.transform_point(p)
# if p.x < -2 or p.x > DRAGGING_RECT_WIDTH + 2 or \
# p.y < -1 or p.y > DRAGGING_RECT_HEIGHT:
# return False
# return True
#
#def move(self, pos):
# # Tentatively place in the object in the new position
# last_pos = self.pos
# self.pos = pos
#
# # If any point is out of bounds, move object back to last position.
# if not self.is_in_container():
# self.pos = last_pos
#
# self.calculate_bounds()
#
# if self.container:
# self.container.queue_draw()
#
#def rotate(self, angle):
# # Tentatively rotate the object to the new angle
# last_angle = self.angle
# self.angle = angle
#
# # If any point is out of bounds, move object back to last angle.
# if not self.is_in_container():
# self.angle = last_angle
#
# self.calculate_bounds()
#
# self.container.queue_draw()
#
#
|