def test_from_rotation(self): transform = affine.affine_from_rotation(pi / 4.0) pt1 = array([1.0, 0.0, 1.0]) actual = dot(pt1, transform) cos_pi_4 = cos(pi / 4.0) desired = array((cos_pi_4, cos_pi_4, 1.0)) assert alltrue((actual - desired) < 1e-6)
def test_from_rotation(self): transform = affine.affine_from_rotation(pi / 4.) pt1 = array([1., 0., 1.]) actual = dot(pt1, transform) #cos_pi_4 = 0.70710678118654757 cos_pi_4 = cos(pi / 4.0) desired = array((cos_pi_4, cos_pi_4, 1.0)) assert (alltrue((actual - desired) < 1e-6))
def test_concat_ctm(self): gc = basecore2d.GraphicsContextBase() ident = affine.affine_identity() trans = affine.affine_from_rotation(2.0) desired = affine.concat(ident, trans) gc.concat_ctm(trans) actual = gc.get_ctm() self.assertTrue(alltrue(ravel(actual == desired)))
def test_from_rotation(self): transform = affine.affine_from_rotation(pi/4.) pt1 = array([1.,0.,1.]) actual = dot(pt1,transform) #cos_pi_4 = 0.70710678118654757 cos_pi_4 = cos(pi/4.0) desired = array((cos_pi_4, cos_pi_4, 1.0)) assert(alltrue( (actual - desired) < 1e-6 ))
def test_concat_ctm(self): gc = basecore2d.GraphicsContextBase() ident = affine.affine_identity() trans = affine.affine_from_rotation(2.) x, y = 2., 3. desired = affine.concat(ident, trans) gc.concat_ctm(trans) actual = gc.get_ctm() self.assert_(alltrue(ravel(actual == desired)))
def _draw_rotated_label(self, gc, text, theta, radius): tx, ty, tw, th = gc.get_text_extent(text) rr = math.sqrt(radius ** 2 + (0.5 * tw) ** 2) dtheta = math.atan2(0.5 * tw, radius) text_theta = theta + dtheta x = rr * math.cos(text_theta) y = rr * math.sin(text_theta) rot_theta = theta - 0.5 * math.pi with gc: gc.set_text_matrix(affine.affine_from_rotation(rot_theta)) gc.set_text_position(x, y) gc.show_text(text)
def _draw_rotated_label(self, gc, text, theta, radius): tx, ty, tw, th = gc.get_text_extent(text) rr = math.sqrt(radius**2 + (0.5 * tw)**2) dtheta = math.atan2(0.5 * tw, radius) text_theta = theta + dtheta x = rr * math.cos(text_theta) y = rr * math.sin(text_theta) rot_theta = theta - 0.5 * math.pi with gc: gc.set_text_matrix(affine.affine_from_rotation(rot_theta)) gc.set_text_position(x, y) gc.show_text(text)
def test_handling_text(gc): font = Font(face_name="Arial", size=32) gc.set_font(font) gc.translate_ctm(100.0, 100.0) gc.move_to(-5, 0) gc.line_to(5, 0) gc.move_to(0, 5) gc.line_to(0, -5) gc.move_to(0, 0) gc.stroke_path() txtRot = affine.affine_from_rotation(PI / 6) gc.set_text_matrix(txtRot) gc.show_text("Hello") txtRot = affine.invert(txtRot) gc.set_text_matrix(txtRot) gc.show_text("inverted")
def test_handling_text(gc): font = Font(face_name="Arial", size = 32) gc.set_font(font) gc.translate_ctm(100.0, 100.0) gc.move_to(-5,0) gc.line_to(5,0) gc.move_to(0,5) gc.line_to(0,-5) gc.move_to(0,0) gc.stroke_path() txtRot = affine.affine_from_rotation(PI/6) gc.set_text_matrix(txtRot) gc.show_text("Hello") txtRot = affine.invert(txtRot) gc.set_text_matrix(txtRot) gc.show_text("inverted")
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): beta = self._beta phi = self._phi w = self.width M = self._outer_radial_margin R = self._axis_radius # (ox, oy) is the position of the "hinge point" of the needle # (i.e. the center of rotation). For beta > ~0, oy is negative, # so this point is below the visible region. ox = self.x + self.width // 2 oy = -beta * R * math.cos(phi / 2) + 1 left_theta = math.radians(180 - self.angle) right_theta = math.radians(self.angle) # The angle of the 100% position. nominal_theta = self._percent_to_theta(100.0) # The color of the axis for percent > 100. red = (0.8, 0, 0) with gc: gc.set_antialias(True) # Draw everything relative to the center of the circles. gc.translate_ctm(ox, oy) # Draw the primary ticks and tick labels on the curved axis. gc.set_fill_color((0, 0, 0)) gc.set_font(self.db_tick_font) for db in [-20, -10, -7, -5, -3, -2, -1, 0, 1, 2, 3]: db_percent = db_to_percent(db) theta = self._percent_to_theta(db_percent) x1 = R * math.cos(theta) y1 = R * math.sin(theta) x2 = (R + 0.3 * M) * math.cos(theta) y2 = (R + 0.3 * M) * math.sin(theta) gc.set_line_width(2.5) gc.move_to(x1, y1) gc.line_to(x2, y2) gc.stroke_path() text = str(db) if db > 0: text = '+' + text self._draw_rotated_label(gc, text, theta, R + 0.4 * M) # Draw the secondary ticks on the curve axis. for db in [-15, -9, -8, -6, -4, -0.5, 0.5]: ##db_percent = 100 * math.pow(10.0, db / 20.0) db_percent = db_to_percent(db) theta = self._percent_to_theta(db_percent) x1 = R * math.cos(theta) y1 = R * math.sin(theta) x2 = (R + 0.2 * M) * math.cos(theta) y2 = (R + 0.2 * M) * math.sin(theta) gc.set_line_width(1.0) gc.move_to(x1, y1) gc.line_to(x2, y2) gc.stroke_path() # Draw the percent ticks and label on the bottom of the # curved axis. gc.set_font(self.percent_tick_font) gc.set_fill_color((0.5, 0.5, 0.5)) gc.set_stroke_color((0.5, 0.5, 0.5)) percents = self.percent_ticks for tick_percent in percents: theta = self._percent_to_theta(tick_percent) x1 = (R - 0.15 * M) * math.cos(theta) y1 = (R - 0.15 * M) * math.sin(theta) x2 = R * math.cos(theta) y2 = R * math.sin(theta) gc.set_line_width(2.0) gc.move_to(x1, y1) gc.line_to(x2, y2) gc.stroke_path() text = str(tick_percent) if tick_percent == percents[-1]: text = text + "%" self._draw_rotated_label(gc, text, theta, R - 0.3 * M) if self.text: gc.set_font(self.text_font) tx, ty, tw, th = gc.get_text_extent(self.text) gc.set_fill_color((0, 0, 0, 0.25)) gc.set_text_matrix(affine.affine_from_rotation(0)) gc.set_text_position(-0.5 * tw, (0.75 * beta + 0.25) * R) gc.show_text(self.text) # Draw the red curved axis. gc.set_stroke_color(red) w = 10 gc.set_line_width(w) gc.arc(0, 0, R + 0.5 * w - 1, right_theta, nominal_theta) gc.stroke_path() # Draw the black curved axis. w = 4 gc.set_line_width(w) gc.set_stroke_color((0, 0, 0)) gc.arc(0, 0, R + 0.5 * w - 1, nominal_theta, left_theta) gc.stroke_path() # Draw the filled arc at the bottom. gc.set_line_width(2) gc.set_stroke_color((0, 0, 0)) gc.arc(0, 0, beta * R, math.radians(self.angle), math.radians(180 - self.angle)) gc.stroke_path() gc.set_fill_color((0, 0, 0, 0.25)) gc.arc(0, 0, beta * R, math.radians(self.angle), math.radians(180 - self.angle)) gc.fill_path() # Draw the needle. percent = self.percent # If percent exceeds max_percent, the needle is drawn at max_percent. if percent > self.max_percent: percent = self.max_percent needle_theta = self._percent_to_theta(percent) gc.rotate_ctm(needle_theta - 0.5 * math.pi) self._draw_vertical_needle(gc)