From ea894b526489e4c24a6bba1ea57773150b09cd06 Mon Sep 17 00:00:00 2001 From: pmoxhay Date: Fri, 08 Jan 2010 10:09:11 +0000 Subject: Merge branch 'master' of gitorious@git.sugarlabs.org:math/mainline --- diff --git a/volumeobject.py b/volumeobject.py index 1823050..16a74cf 100644 --- a/volumeobject.py +++ b/volumeobject.py @@ -72,10 +72,6 @@ class VolumeObject(MovableObject): 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) - - #def calculate_water_volume(self): - # return (math.pi * self.water_height / 3.0) * \ - # (self.lower_radius * self.lower_radius + self.lower_radius * self.water_upper_radius + self.water_upper_radius * self.water_upper_radius) def calculate_water_height(self, volume): a = (self.upper_radius - self.lower_radius)**2 @@ -93,8 +89,6 @@ class VolumeObject(MovableObject): def fill_to_given_volume(self, volume): self.water_height = self.calculate_water_height(volume) - - #self.water_volume = self.calculate_water_volume() self.water_volume = volume self.start_animating() @@ -156,21 +150,13 @@ class VolumeObject(MovableObject): return -pow(abs(x), 1.0/3.0) 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) cr.restore() - def fill_ellipse(self, cr, x, y, width, height): - cr.save() - cr.translate (x + width / 2., y) - cr.scale(width / 2., height / 2.) - cr.arc(0., 0., 1., 0., 2 * math.pi) - cr.set_source_rgb(0.37, 0.74, 1.0) - cr.fill() - cr.restore(); - def animate(self): if self.animated_water_volume < self.water_volume: self.animated_water_volume = min(self.animated_water_volume + VolumeObject.FILL_RATE, self.water_volume) @@ -191,151 +177,79 @@ class VolumeObject(MovableObject): self.filling_from_faucet = False def draw(self, cr): - cr.save() - - #cr.set_source_rgb(1, 0, 1) - #self_bounds_mn, self_bounds_mx = self.get_bounds() - #cr.rectangle(self_bounds_mn.x, self_bounds_mn.y, self_bounds_mx.x - self_bounds_mn.x, self_bounds_mx.y - self_bounds_mn.y) - #cr.fill() - + cr.translate(self.pos.x, self.pos.y) + cr.rotate(self.angle) cr.scale(self.scale, self.scale) - points = [ - self.pos + Vector(-self.upper_radius, -self.height/2), - self.pos + Vector(self.upper_radius, -self.height/2), - self.pos + Vector(self.lower_radius, self.height/2), - self.pos + Vector(-self.lower_radius, self.height/2) ] - - # Transform the points. - water_points = points[:] - water_points[0] = Vector(water_points[0].x + (self.upper_radius - self.lower_radius) * (self.height - self.animated_water_height)/float(self.height), water_points[0].y - self.height + self.animated_water_height) - water_points[1] = Vector(water_points[1].x - (self.upper_radius - self.lower_radius) * (self.height - self.animated_water_height)/float(self.height), water_points[1].y - self.height + self.animated_water_height) - - water_points[0] = Vector(water_points[0].x, water_points[3].y - self.animated_water_height) - water_points[1] = Vector(water_points[1].x, water_points[2].y - self.animated_water_height) - water_points[2] = Vector(water_points[2].x, water_points[2].y) - water_points[3] = Vector(water_points[3].x, water_points[3].y) + ul = Vector(-self.upper_radius, -self.height/2) + ur = Vector(self.upper_radius, -self.height/2) + lr = Vector(self.lower_radius, self.height/2) + ll = Vector(-self.lower_radius, self.height/2) - # Draw the water, if any. - cr.save() - # Generate the shape. - cr.move_to(water_points[0].x, water_points[0].y) - for p in water_points: - cr.line_to(p.x, p.y) - cr.line_to(water_points[0].x, water_points[0].y) - cr.close_path() - - #Draw the fill. - cr.set_source_rgb(0.37, 0.74, 1.0) - cr.fill() - cr.restore() + if self.animated_water_height > 1: + t = self.animated_water_height / float(self.height) + water_radius = self.upper_radius*t + self.lower_radius*(1-t) - # Fill in the lower ellipse. - cr.save() - if not self.animated_water_height == 0: - #self.fill_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) - self.fill_ellipse(cr, points[3].x, points[3].y, \ - 2.0 * self.lower_radius, self.lower_radius/2.0) - cr.restore() + wl = Vector(-water_radius, self.height/2 - self.animated_water_height) + wr = Vector(water_radius, self.height/2 - self.animated_water_height) - water_upper_radius = self.lower_radius + self.animated_water_height * (self.upper_radius - self.lower_radius)/self.height + cr.set_source_rgb(0.37, 0.74, 1.0) - # Fill in the upper ellipse. - cr.save() - if not self.animated_water_height == 0: - #self.fill_ellipse(cr, self.pos.x - water_upper_radius, self.pos.y + self.height/2.0 - self.animated_water_height - water_upper_radius/4.0, \ - #2.0 * water_upper_radius, water_upper_radius/2.0) - self.fill_ellipse(cr, water_points[0].x, water_points[0].y, \ - 2.0 * water_upper_radius, water_upper_radius/2.0) - cr.restore() - - # Draw the upper ellipse - if not self.animated_water_height == 0: + cr.line_to(wl.x, wl.y) + cr.line_to(wr.x, wr.y) + cr.line_to(lr.x, lr.y) + cr.line_to(ll.x, ll.y) + cr.close_path() - cr.save() + self.draw_ellipse(cr, wl.x, wl.y, 2.0 * water_radius, water_radius/2.0) + self.draw_ellipse(cr, ll.x, ll.y, 2.0 * self.lower_radius, self.lower_radius/2.0) + cr.fill() + cr.set_source_rgb(0.0, 0.0, 1.0) cr.set_line_width(4.0) - #self.draw_ellipse(cr, self.pos.x - water_upper_radius, \ - # self.pos.y + self.height/2.0 - self.animated_water_height - water_upper_radius/4.0, - # 2.0 * water_upper_radius, water_upper_radius/2.0) - self.draw_ellipse(cr, water_points[0].x, water_points[0].y, \ - 2.0 * water_upper_radius, water_upper_radius/2.0) + + self.draw_ellipse(cr, wl.x, wl.y, 2.0 * water_radius, water_radius/2.0) cr.stroke() - cr.restore() # Draw the faucet filling. if self.filling_from_faucet: - stream_y = self.container.problem.faucet_object.pos.y + FaucetObject.STREAM_Y - cr.rectangle(self.container.problem.faucet_object.pos.x + FaucetObject.STREAM_X, - stream_y, - FaucetObject.STREAM_WIDTH, self.pos.y + self.height/2 - self.animated_water_height - stream_y) - - #Draw the fill. + assert self.scale == 1.0 + stream_x = self.container.problem.faucet_object.pos.x + FaucetObject.STREAM_X - self.pos.x + stream_y = self.container.problem.faucet_object.pos.y + FaucetObject.STREAM_Y - self.pos.y + cr.rectangle(stream_x, stream_y, FaucetObject.STREAM_WIDTH, self.height/2 - self.animated_water_height - stream_y) + cr.set_source_rgb(0.37, 0.74, 1.0) cr.fill() - # Done drawing the shape of the water. # Now draw the shape of the container. - # 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, points[3].x, points[3].y, \ - 2.0 * self.lower_radius, self.lower_radius/2.0) - cr.stroke() - cr.restore() + cr.move_to(ul.x, ul.y) + cr.line_to(ll.x, ll.y) + cr.move_to(ur.x, ur.y) + cr.line_to(lr.x, lr.y) - # 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) - self.draw_ellipse(cr, points[0].x, points[0].y, \ - 2.0 * self.upper_radius, self.upper_radius/2.0) + 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() - cr.restore() # Draw the symbol (capital letter representing the shape'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.move_to(-x_bearing - width/2, -y_bearing - height/2) cr.show_text(self.symbol) - - cr.restore() def calculate_bounds(self): r = max(self.upper_radius, self.lower_radius) - halfsize = Vector(r + 2, self.height/2 + r/2 + 2) * self.scale - self.bounds_min = self.pos * self.scale - halfsize - self.bounds_max = self.pos * self.scale + halfsize + # 50 is a hack to account for a slight rotation + halfsize = Vector(r + 2 + 50, self.height/2 + r/2 + 2) * self.scale + self.bounds_min = self.pos - halfsize + self.bounds_max = self.pos + halfsize # Include the stream when animating. if self.filling_from_faucet: diff --git a/volumeproblem.py b/volumeproblem.py index b5a5318..6bec2c8 100644 --- a/volumeproblem.py +++ b/volumeproblem.py @@ -343,8 +343,10 @@ class VolumeProblem(Problem): 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) else: self.shape1.move(Vector(self.shape2.pos.x + 250, self.shape2.pos.y)) + self.shape1.rotate(15 * math.pi/180.0) #self.shape1.calculate_bounds() #self.shape2.calculate_bounds() @@ -376,8 +378,10 @@ class VolumeProblem(Problem): 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) else: self.shape2.move(Vector(self.shape1.pos.x + 250, self.shape1.pos.y)) + self.shape2.rotate(15 * math.pi/180.0) return True @@ -398,10 +402,11 @@ class VolumeProblem(Problem): def place_objects_in_final_positions(self): #print "VolumeProblem: place_objects_in_final_positions called" - + for o in self.container.objects: if isinstance(o, VolumeObject): o.scale = 0.9 + o.angle = 0 o.calculate_bounds() x_left_final = 300 -- cgit v0.9.1