diff options
author | pmoxhay <pmoxhay@earthlink.net> | 2010-01-11 22:30:12 (GMT) |
---|---|---|
committer | pmoxhay <pmoxhay@earthlink.net> | 2010-01-11 22:30:12 (GMT) |
commit | 7d1ec42eabef8f64caeb5592415a8c58185266d7 (patch) | |
tree | f3d91d405d57449ab6af939d99a0cd3cba394b09 | |
parent | ea894b526489e4c24a6bba1ea57773150b09cd06 (diff) |
Tips and pours the containers; problem 9 only.
-rw-r--r-- | volumeobject.py | 81 | ||||
-rw-r--r-- | volumeproblem.py | 88 |
2 files changed, 136 insertions, 33 deletions
diff --git a/volumeobject.py b/volumeobject.py index 16a74cf..a0df582 100644 --- a/volumeobject.py +++ b/volumeobject.py @@ -39,12 +39,32 @@ class VolumeObject(MovableObject): self.lower_radius = lower_radius
self.upper_radius = upper_radius
- #if upper_radius > lower_radius:
+ self.shifted_pos = Vector(0, 0)
+ self.x0 = 0
+ self.y0 = 0
+
+ self.a1 = 0
+ self.b1 = 0
+ self.h1 = 0
+ self.a2 = 0
+ self.b2 = 0
+ self.h2 = 0
+
+ # For debugging, set this equal to True to draw a simple
+ # trapezoid with a dot at the center.
+ self.ellipses_and_letter_visible = True
+
+ # Modify this so the centroid is really at (0, 0)? Maybe this isn't necessary.
self.points = [ Vector(-self.upper_radius, -self.height/2.), Vector(self.upper_radius-self.lower_radius, self.height/2.), \
Vector(self.lower_radius, self.height/2.), Vector(-self.lower_radius, self.height/2.) ]
- #else:
- # self.points = [ Vector(-self.upper_radius, -self.height/2.), Vector(self.upper_radius-self.lower_radius, self.height/2.), \
- # Vector(self.lower_radius, self.height/2.), Vector(self.upper_radius, self.height/2.) ]
+
+ #self.points = [ Vector(-self.upper_radius, -self.height * ( 4. * self.upper_radius + 2. * self.lower_radius)/(3. * (2. * self.upper_radius + 2. * self.lower_radius)) ), \
+ #
+ # Vector(self.upper_radius, -self.height * ( 4. * self.upper_radius + 2. * self.lower_radius)/(3. * (2. * self.upper_radius + 2. * self.lower_radius)) ), \
+ #
+ # Vector(self.lower_radius, self.height * ( 2. * self.upper_radius + 4. * self.lower_radius)/(3. * (2. * self.upper_radius + 2. * self.lower_radius)) ), \
+ #
+ # Vector(-self.lower_radius, self.height * ( 2. * self.upper_radius + 4. * self.lower_radius)/(3. * (2. * self.upper_radius + 2. * self.lower_radius)) ) ]
self.water_height = 0
self.water_lower_radius = lower_radius
@@ -55,6 +75,8 @@ class VolumeObject(MovableObject): self.animated_water_volume = 0
self.animated_water_height = 0
+
+ self.initial_volume_to_pour_out = 0
self.filling_from_faucet = False
@@ -152,9 +174,10 @@ class VolumeObject(MovableObject): def draw_ellipse(self, cr, x, y, width, height):
cr.new_sub_path()
cr.save()
- cr.translate (x + width / 2., y)
- cr.scale(width / 2., height / 2.)
- cr.arc(0., 0., 1., 0., 2 * math.pi)
+ if self.ellipses_and_letter_visible:
+ cr.translate (x + width / 2., y)
+ cr.scale(width / 2., height / 2.)
+ cr.arc(0., 0., 1., 0., 2 * math.pi)
cr.restore()
def animate(self):
@@ -165,6 +188,34 @@ class VolumeObject(MovableObject): self.queue_draw()
elif self.animated_water_volume > self.water_volume:
+ #print "animating: self.animated_water_volume = ", self.animated_water_volume
+ #print "animating: self.initial_volume_to_pour_out = ", self.initial_volume_to_pour_out
+ # Have the angle change from 0 deg to 90 deg.
+ angle = 90.0 * (1.0 - self.animated_water_volume/self.initial_volume_to_pour_out)
+ self.rotate(angle * math.pi/180.0)
+
+ theta = angle * math.pi/180.0
+
+ a1 = self.a1
+ b1 = self.b1
+ h1 = self.h1
+ a2 = self.a2
+ b2 = self.b2
+ h2 = self.h2
+
+ x_shift = - b2/2. - b1/2.
+ y_shift = - h2/2. + h1/2.
+
+ x_shift = - b2/2. - b1 * math.cos(theta) /2. - h1 * math.sin(theta) /2.
+ y_shift = - h2/2. + h1 * math.cos(theta) /2. - b1 * math.sin(theta) /2.
+
+ #print "x_shift =", x_shift
+ #print "y_shift =", y_shift
+
+ shifted_pos = Vector(self.x0 + x_shift, self.y0 + y_shift)
+
+ self.move(shifted_pos)
+
self.animated_water_volume = max(self.animated_water_volume - VolumeObject.FILL_RATE, self.water_volume)
self.animated_water_height = self.calculate_water_height(self.animated_water_volume)
self.calculate_bounds()
@@ -231,18 +282,32 @@ class VolumeObject(MovableObject): cr.line_to(ll.x, ll.y)
cr.move_to(ur.x, ur.y)
cr.line_to(lr.x, lr.y)
+
+ if not self.ellipses_and_letter_visible:
+ cr.move_to(ul.x, ur.y)
+ cr.line_to(ur.x, ur.y)
+ cr.move_to(ll.x, lr.y)
+ cr.line_to(lr.x, lr.y)
self.draw_ellipse(cr, ll.x, ll.y, 2.0 * self.lower_radius, self.lower_radius/2.0)
self.draw_ellipse(cr, ul.x, ul.y, 2.0 * self.upper_radius, self.upper_radius/2.0)
cr.stroke()
# Draw the symbol (capital letter representing the shape's area).
- if self.symbol_visible:
+ if self.symbol_visible and self.ellipses_and_letter_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(-x_bearing - width/2, -y_bearing - height/2)
cr.show_text(self.symbol)
+
+ # For debugging purposes, draw a dot to show the center of mass.
+ if not self.ellipses_and_letter_visible:
+ cr.save()
+ cr.arc(0, 0, 4, 0, 2.*math.pi)
+ cr.set_source_rgb(0., 0., 0.)
+ cr.fill()
+ cr.restore()
def calculate_bounds(self):
r = max(self.upper_radius, self.lower_radius)
diff --git a/volumeproblem.py b/volumeproblem.py index 6bec2c8..b408fa1 100644 --- a/volumeproblem.py +++ b/volumeproblem.py @@ -44,11 +44,6 @@ class VolumeProblem(Problem): self.answer = self.find_answer() self.under_faucet_position = Vector(200, 500) - - #self.container.moons_visible = False - - #self.pan1_position = Vector(200, 500) - #self.pan2_position = Vector(725, 500) def generate_problem(self): # The total number of problems. @@ -59,7 +54,7 @@ class VolumeProblem(Problem): self.problem_number = random.randrange(0, self.n_problems) # Uncomment to test a particular problem. - #self.problem_number = 0 + self.problem_number = 9 # Define the various problems. if self.problem_number == 0: @@ -317,54 +312,76 @@ class VolumeProblem(Problem): self.shape2.water_height = self.shape2.height self.shape2.filling_from_faucet = True self.shape2.fill_to_given_volume(self.shape2.volume) - + + # Shape 1 and shape 2 are moved close together. if self.shape1.pos.approx_equal(self.shape2.pos, tolerance=150): - #print " the two volumes are close to each other" + print " the two volumes are close to each other" if self.shape1.full and not self.shape2.full: #print "" - #print "pour from volume 1 into volume 2" + print "(first case) pour from volume 1 into volume 2" - #print " Before: water volume1 =", self.shape1.water_volume - #print " water volume2 =", self.shape2.water_volume - + print " Before: water volume1 =", self.shape1.water_volume + print " water volume2 =", self.shape2.water_volume + + # Pour from shape 1 to shape 2. if self.shape1.water_volume <= self.shape2.volume: - #print "(1): water volume 1 less than (or equal to) empty volume 2" + print "(1): water volume 1 less than (or equal to) empty volume 2" self.shape2.fill_to_given_volume(self.shape1.water_volume) self.shape1.fill_to_given_volume(0.0) + self.shape1.initial_volume_to_pour_out = self.shape2.water_volume else: - #print "(2): water volume 1 greater than empty volume 2" + print "(2): water volume 1 greater than empty volume 2" self.shape2.fill_to_given_volume(self.shape2.volume) self.shape2.full = True self.shape1.fill_to_given_volume(self.shape1.water_volume - self.shape2.water_volume) self.shape1.full = False + self.shape1.initial_volume_to_pour_out = self.shape2.water_volume #print " After: volume1 =", self.shape1.water_volume #print " volume2 =", self.shape2.water_volume if self.shape2.pos.x > 250: self.shape1.move(Vector(self.shape2.pos.x - 250, self.shape2.pos.y)) - self.shape1.rotate(15 * math.pi/180.0) + #self.shape1.rotate(15 * math.pi/180.0) else: self.shape1.move(Vector(self.shape2.pos.x + 250, self.shape2.pos.y)) - self.shape1.rotate(15 * math.pi/180.0) + #self.shape1.rotate(15 * math.pi/180.0) + + a1 = 2.0 * self.shape1.lower_radius + b1 = 2.0 * self.shape1.upper_radius + h1 = self.shape1.height + + a2 = 2.0 * self.shape2.lower_radius + b2 = 2.0 * self.shape2.upper_radius + h2 = self.shape2.height - #self.shape1.calculate_bounds() - #self.shape2.calculate_bounds() + self.shape1.move(Vector(self.shape2.pos.x, self.shape2.pos.y)) + self.shape1.a1 = a1 + self.shape1.b1 = b1 + self.shape1.h1 = h1 + self.shape1.a2 = a2 + self.shape1.b2 = b2 + self.shape1.h2 = h2 + + self.shape1.x0 = self.shape2.pos.x + self.shape1.y0 = self.shape2.pos.y + return True elif self.shape2.full and not self.shape1.full: - #print " pour from volume 2 into volume 1" + print "(second case) pour from volume 2 into volume 1" - #print " Before: water volume2 =", self.shape2.water_volume - #print " empty volume1 =", self.shape1.volume + print " Before: water volume2 =", self.shape2.water_volume + print " empty volume1 =", self.shape1.volume if self.shape2.water_volume <= self.shape1.volume: - #print "(3): water volume 2 less than (or equal to) empty volume 1" + print "(3): water volume 2 less than (or equal to) empty volume 1" self.shape1.fill_to_given_volume(self.shape2.water_volume) self.shape2.fill_to_given_volume(0.0) + self.shape2.initial_volume_to_pour_out = self.shape1.water_volume else: - #print "(4): water volume 2 greater than empty volume 1" + print "(4): water volume 2 greater than empty volume 1" self.shape1.fill_to_given_volume(self.shape1.volume) self.shape1.full = True self.shape2.fill_to_given_volume(self.shape2.water_volume - self.shape1.water_volume) @@ -372,16 +389,37 @@ class VolumeProblem(Problem): volume_difference = self.shape2.water_volume - self.shape1.water_volume #print "volume_difference =", volume_difference self.shape2.full = False + self.shape2.initial_volume_to_pour_out = self.shape1.water_volume #print " After: volume2 =", self.shape2.water_volume #print " volume1 =", self.shape1.water_volume if self.shape1.pos.x > 250: self.shape2.move(Vector(self.shape1.pos.x - 250, self.shape1.pos.y)) - self.shape2.rotate(15 * math.pi/180.0) + #self.shape2.rotate(15 * math.pi/180.0) else: self.shape2.move(Vector(self.shape1.pos.x + 250, self.shape1.pos.y)) - self.shape2.rotate(15 * math.pi/180.0) + #self.shape2.rotate(15 * math.pi/180.0) + + a1 = 2.0 * self.shape2.lower_radius + b1 = 2.0 * self.shape2.upper_radius + h1 = self.shape2.height + + a2 = 2.0 * self.shape1.lower_radius + b2 = 2.0 * self.shape1.upper_radius + h2 = self.shape1.height + + self.shape2.move(Vector(self.shape1.pos.x, self.shape1.pos.y)) + + self.shape2.a1 = a1 + self.shape2.b1 = b1 + self.shape2.h1 = h1 + self.shape2.a2 = a2 + self.shape2.b2 = b2 + self.shape2.h2 = h2 + + self.shape2.x0 = self.shape1.pos.x + self.shape2.y0 = self.shape1.pos.y return True |