Example #1
0
 def draw_step(self, x0, y0, sx, sy, plot, goback):
     """ Make a DRAW step, drawing a line and reurning if requested. """
     scale = self.draw_scale
     rotate = self.draw_angle
     aspect = self.screen.mode.pixel_aspect
     yfac = aspect[1] / (1.*aspect[0])
     x1 = (scale*sx) / 4
     y1 = (scale*sy) / 4
     if rotate == 0 or rotate == 360:
         pass
     elif rotate == 90:
         x1, y1 = int(y1*yfac), -int(x1//yfac)
     elif rotate == 180:
         x1, y1 = -x1, -y1
     elif rotate == 270:
         x1, y1 = -int(y1*yfac), int(x1//yfac)
     else:
         fx, fy = fp.Single.from_int(x1), fp.Single.from_int(y1)
         phi = fp.mul(fp.Single.from_int(rotate), deg_to_rad)
         sinr, cosr = fp.sin(phi), fp.cos(phi)
         fxfac = fp.div(fp.Single.from_int(aspect[0]), fp.Single.from_int(aspect[1]))
         fx, fy = fp.add(fp.mul(cosr,fx), fp.div(fp.mul(sinr,fy), fxfac)), fp.mul(fp.sub(fp.mul(cosr,fy), fxfac), fp.mul(sinr,fx))
         x1, y1 = fx.round_to_int(), fy.round_to_int()
     y1 += y0
     x1 += x0
     if plot:
         self.draw_line(x0, y0, x1, y1, self.last_attr)
     self.last_point = x1, y1
     if goback:
         self.last_point = x0, y0
Example #2
0
 def get_window_scale(self, fx, fy):
     """ Get logical to physical scale factor. """
     if self.window:
         scalex, scaley, _, _ = self.window
         return (fp.mul(fx, scalex).round_to_int(), fp.mul(fy, scaley).round_to_int())
     else:
         return fx.round_to_int(), fy.round_to_int()
Example #3
0
 def get_window_scale(self, fx, fy):
     """ Get logical to physical scale factor. """
     if self.window:
         scalex, scaley, _, _ = self.window
         return (fp.mul(fx, scalex).round_to_int(),
                 fp.mul(fy, scaley).round_to_int())
     else:
         return fx.round_to_int(), fy.round_to_int()
Example #4
0
 def circle(self, lcoord, r, start, stop, c, aspect):
     """ Draw a circle, ellipse, arc or sector (CIRCLE). """
     x0, y0 = self.view_coords(*self.get_window_physical(*lcoord))
     c = self.get_attr_index(c)
     if aspect == None:
         aspect = fp.div(
             fp.Single.from_int(self.screen.mode.pixel_aspect[0]),
             fp.Single.from_int(self.screen.mode.pixel_aspect[1]),
         )
     if aspect.equals(aspect.one):
         rx, _ = self.get_window_scale(r, fp.Single.zero)
         ry = rx
     elif aspect.gt(aspect.one):
         _, ry = self.get_window_scale(fp.Single.zero, r)
         rx = fp.div(r, aspect).round_to_int()
     else:
         rx, _ = self.get_window_scale(r, fp.Single.zero)
         ry = fp.mul(r, aspect).round_to_int()
     start_octant, start_coord, start_line = -1, -1, False
     if start:
         start = fp.unpack(vartypes.pass_single_keep(start))
         start_octant, start_coord, start_line = get_octant(start, rx, ry)
     stop_octant, stop_coord, stop_line = -1, -1, False
     if stop:
         stop = fp.unpack(vartypes.pass_single_keep(stop))
         stop_octant, stop_coord, stop_line = get_octant(stop, rx, ry)
     if aspect.equals(aspect.one):
         self.draw_circle(x0, y0, rx, c, start_octant, start_coord, start_line, stop_octant, stop_coord, stop_line)
     else:
         startx, starty, stopx, stopy = -1, -1, -1, -1
         if start != None:
             startx = abs(fp.mul(fp.Single.from_int(rx), fp.cos(start)).round_to_int())
             starty = abs(fp.mul(fp.Single.from_int(ry), fp.sin(start)).round_to_int())
         if stop != None:
             stopx = abs(fp.mul(fp.Single.from_int(rx), fp.cos(stop)).round_to_int())
             stopy = abs(fp.mul(fp.Single.from_int(ry), fp.sin(stop)).round_to_int())
         self.draw_ellipse(
             x0,
             y0,
             rx,
             ry,
             c,
             start_octant / 2,
             startx,
             starty,
             start_line,
             stop_octant / 2,
             stopx,
             stopy,
             stop_line,
         )
     self.last_attr = c
     self.last_point = x0, y0
Example #5
0
 def circle(self, lcoord, r, start, stop, c, aspect):
     """ Draw a circle, ellipse, arc or sector (CIRCLE). """
     x0, y0 = self.view_coords(*self.get_window_physical(*lcoord))
     c = self.get_attr_index(c)
     if aspect is None:
         aspect = fp.div(
             fp.Single.from_int(self.screen.mode.pixel_aspect[0]),
             fp.Single.from_int(self.screen.mode.pixel_aspect[1]))
     if aspect.equals(aspect.one):
         rx, _ = self.get_window_scale(r, fp.Single.zero)
         ry = rx
     elif aspect.gt(aspect.one):
         _, ry = self.get_window_scale(fp.Single.zero, r)
         rx = fp.div(r, aspect).round_to_int()
     else:
         rx, _ = self.get_window_scale(r, fp.Single.zero)
         ry = fp.mul(r, aspect).round_to_int()
     start_octant, start_coord, start_line = -1, -1, False
     if start:
         start = fp.unpack(vartypes.pass_single_keep(start))
         start_octant, start_coord, start_line = get_octant(start, rx, ry)
     stop_octant, stop_coord, stop_line = -1, -1, False
     if stop:
         stop = fp.unpack(vartypes.pass_single_keep(stop))
         stop_octant, stop_coord, stop_line = get_octant(stop, rx, ry)
     if aspect.equals(aspect.one):
         self.draw_circle(x0, y0, rx, c, start_octant, start_coord,
                          start_line, stop_octant, stop_coord, stop_line)
     else:
         startx, starty, stopx, stopy = -1, -1, -1, -1
         if start is not None:
             startx = abs(
                 fp.mul(fp.Single.from_int(rx),
                        fp.cos(start)).round_to_int())
             starty = abs(
                 fp.mul(fp.Single.from_int(ry),
                        fp.sin(start)).round_to_int())
         if stop is not None:
             stopx = abs(
                 fp.mul(fp.Single.from_int(rx),
                        fp.cos(stop)).round_to_int())
             stopy = abs(
                 fp.mul(fp.Single.from_int(ry),
                        fp.sin(stop)).round_to_int())
         self.draw_ellipse(x0, y0, rx, ry, c, start_octant / 2, startx,
                           starty, start_line, stop_octant / 2, stopx,
                           stopy, stop_line)
     self.last_attr = c
     self.last_point = x0, y0
Example #6
0
 def set_window(self, fx0, fy0, fx1, fy1, cartesian=True):
     """ Set the logical coordinate window (WINDOW). """
     if fy0.gt(fy1):
         fy0, fy1 = fy1, fy0
     if fx0.gt(fx1):
         fx0, fx1 = fx1, fx0
     if cartesian:
         fy0, fy1 = fy1, fy0
     left, top, right, bottom = self.get_view()
     x0, y0 = fp.Single.zero, fp.Single.zero
     x1, y1 = fp.Single.from_int(right-left), fp.Single.from_int(bottom-top)
     scalex = fp.div(fp.sub(x1, x0), fp.sub(fx1,fx0))
     scaley = fp.div(fp.sub(y1, y0), fp.sub(fy1,fy0))
     offsetx = fp.sub(x0, fp.mul(fx0,scalex))
     offsety = fp.sub(y0, fp.mul(fy0,scaley))
     self.window = scalex, scaley, offsetx, offsety
     self.window_bounds = fx0, fy0, fx1, fy1, cartesian
Example #7
0
 def get_window_physical(self, fx, fy, step=False):
     """ Convert logical to physical coordinates. """
     if self.window:
         scalex, scaley, offsetx, offsety = self.window
         if step:
             fx0, fy0 = self.get_window_logical(*self.last_point)
         else:
             fx0, fy0 = fp.Single.zero.copy(), fp.Single.zero.copy()
         x = fp.add(offsetx, fp.mul(fx0.iadd(fx), scalex)).round_to_int()
         y = fp.add(offsety, fp.mul(fy0.iadd(fy), scaley)).round_to_int()
     else:
         x, y = self.last_point if step else (0, 0)
         x += fx.round_to_int()
         y += fy.round_to_int()
     # overflow check
     if x < -0x8000 or y < -0x8000 or x > 0x7fff or y > 0x7fff:
         raise error.RunError(error.OVERFLOW)
     return x, y
Example #8
0
 def get_window_physical(self, fx, fy, step=False):
     """ Convert logical to physical coordinates. """
     if self.window:
         scalex, scaley, offsetx, offsety = self.window
         if step:
             fx0, fy0 = self.get_window_logical(*self.last_point)
         else:
             fx0, fy0 = fp.Single.zero.copy(), fp.Single.zero.copy()
         x = fp.add(offsetx, fp.mul(fx0.iadd(fx), scalex)).round_to_int()
         y = fp.add(offsety, fp.mul(fy0.iadd(fy), scaley)).round_to_int()
     else:
         x, y = self.last_point if step else (0, 0)
         x += fx.round_to_int()
         y += fy.round_to_int()
     # overflow check
     if x < -0x8000 or y < -0x8000 or x > 0x7fff or y > 0x7fff:
         raise error.RunError(error.OVERFLOW)
     return x, y
Example #9
0
 def set_window(self, fx0, fy0, fx1, fy1, cartesian=True):
     """ Set the logical coordinate window (WINDOW). """
     if fy0.gt(fy1):
         fy0, fy1 = fy1, fy0
     if fx0.gt(fx1):
         fx0, fx1 = fx1, fx0
     if cartesian:
         fy0, fy1 = fy1, fy0
     left, top, right, bottom = self.get_view()
     x0, y0 = fp.Single.zero, fp.Single.zero
     x1, y1 = fp.Single.from_int(right - left), fp.Single.from_int(bottom -
                                                                   top)
     scalex = fp.div(fp.sub(x1, x0), fp.sub(fx1, fx0))
     scaley = fp.div(fp.sub(y1, y0), fp.sub(fy1, fy0))
     offsetx = fp.sub(x0, fp.mul(fx0, scalex))
     offsety = fp.sub(y0, fp.mul(fy0, scaley))
     self.window = scalex, scaley, offsetx, offsety
     self.window_bounds = fx0, fy0, fx1, fy1, cartesian
Example #10
0
def get_octant(mbf, rx, ry):
    """ Get the circle octant for a given coordinate. """
    neg = mbf.neg
    if neg:
        mbf.negate()
    octant = 0
    comp = fp.Single.pi4.copy()
    while mbf.gt(comp):
        comp.iadd(fp.Single.pi4)
        octant += 1
        if octant >= 8:
            raise error.RunError(error.IFC)
    if octant in (0, 3, 4, 7):
        # running var is y
        coord = abs(fp.mul(fp.Single.from_int(ry), fp.sin(mbf)).round_to_int())
    else:
        # running var is x
        coord = abs(fp.mul(fp.Single.from_int(rx), fp.cos(mbf)).round_to_int())
    return octant, coord, neg
Example #11
0
def get_octant(mbf, rx, ry):
    """ Get the circle octant for a given coordinate. """
    neg = mbf.neg
    if neg:
        mbf.negate()
    octant = 0
    comp = fp.Single.pi4.copy()
    while mbf.gt(comp):
        comp.iadd(fp.Single.pi4)
        octant += 1
        if octant >= 8:
            raise error.RunError(error.IFC)
    if octant in (0, 3, 4, 7):
        # running var is y
        coord = abs(fp.mul(fp.Single.from_int(ry), fp.sin(mbf)).round_to_int())
    else:
        # running var is x
        coord = abs(fp.mul(fp.Single.from_int(rx), fp.cos(mbf)).round_to_int())
    return octant, coord, neg
Example #12
0
 def draw_step(self, x0, y0, sx, sy, plot, goback):
     """ Make a DRAW step, drawing a line and reurning if requested. """
     scale = self.draw_scale
     rotate = self.draw_angle
     aspect = self.screen.mode.pixel_aspect
     yfac = aspect[1] / (1. * aspect[0])
     x1 = (scale * sx) / 4
     y1 = (scale * sy) / 4
     if rotate == 0 or rotate == 360:
         pass
     elif rotate == 90:
         x1, y1 = int(y1 * yfac), -int(x1 // yfac)
     elif rotate == 180:
         x1, y1 = -x1, -y1
     elif rotate == 270:
         x1, y1 = -int(y1 * yfac), int(x1 // yfac)
     else:
         fx, fy = fp.Single.from_int(x1), fp.Single.from_int(y1)
         phi = fp.mul(fp.Single.from_int(rotate), deg_to_rad)
         sinr, cosr = fp.sin(phi), fp.cos(phi)
         fxfac = fp.div(fp.Single.from_int(aspect[0]),
                        fp.Single.from_int(aspect[1]))
         fx, fy = fp.add(fp.mul(cosr, fx),
                         fp.div(fp.mul(sinr, fy), fxfac)), fp.mul(
                             fp.sub(fp.mul(cosr, fy), fxfac),
                             fp.mul(sinr, fx))
         x1, y1 = fx.round_to_int(), fy.round_to_int()
     y1 += y0
     x1 += x0
     if plot:
         self.draw_line(x0, y0, x1, y1, self.last_attr)
     self.last_point = x1, y1
     if goback:
         self.last_point = x0, y0
Example #13
0
def format_float_fixed(expr, decimals, force_dot):
    """ Put a float in fixed-point representation. """
    unrounded = mul(expr, pow_int(expr.ten, decimals)) # expr * 10**decimals
    num = unrounded.copy().iround()
    # find exponent
    exp10 = 1
    pow10 = pow_int(expr.ten, exp10) # pow10 = 10L**exp10
    while num.gt(pow10) or num.equals(pow10): # while pow10 <= num:
        pow10.imul10() # pow10 *= 10
        exp10 += 1
    work_digits = exp10 + 1
    diff = 0
    if exp10 > expr.digits:
        diff = exp10 - expr.digits
        num = div(unrounded, pow_int(expr.ten, diff)).iround()  # unrounded / 10**diff
        work_digits -= diff
    num = num.trunc_to_int()
    # argument work_digits-1 means we're getting work_digits==exp10+1-diff digits
    # fill up with zeros
    digitstr = get_digits(num, work_digits-1, remove_trailing=False) + ('0' * diff)
    return decimal_notation(digitstr, work_digits-1-1-decimals+diff, '', force_dot)
Example #14
0
def format_float_fixed(expr, decimals, force_dot):
    """ Put a float in fixed-point representation. """
    unrounded = mul(expr, pow_int(expr.ten, decimals))  # expr * 10**decimals
    num = unrounded.copy().iround()
    # find exponent
    exp10 = 1
    pow10 = pow_int(expr.ten, exp10)  # pow10 = 10L**exp10
    while num.gt(pow10) or num.equals(pow10):  # while pow10 <= num:
        pow10.imul10()  # pow10 *= 10
        exp10 += 1
    work_digits = exp10 + 1
    diff = 0
    if exp10 > expr.digits:
        diff = exp10 - expr.digits
        num = div(unrounded, pow_int(expr.ten,
                                     diff)).iround()  # unrounded / 10**diff
        work_digits -= diff
    num = num.trunc_to_int()
    # argument work_digits-1 means we're getting work_digits==exp10+1-diff digits
    # fill up with zeros
    digitstr = get_digits(num, work_digits - 1,
                          remove_trailing=False) + ('0' * diff)
    return decimal_notation(digitstr, work_digits - 1 - 1 - decimals + diff,
                            '', force_dot)