Esempio n. 1
0
    def draw_frame(self, canvas, balls):
        #draws border, arrows and balls on to canvas
        cw = self.cw
        ch = self.ch
        xu = self.xu

        #reset canvas
        draw.reset2(canvas, xu)

        #clear canvas
        draw.clear_canvas(canvas, "#fff")

        #balls
        for i in range(0, 2):
            if i % 2 == 0:
                canvas.fill_style = "#4c7fbe"
            else:
                canvas.fill_style = "#bb2828"
            draw.circle(canvas, balls[i].radius, balls[i].pos.x,
                        balls[i].pos.y)
            canvas.fill()

        #borders
        canvas.stroke_style = "#000"
        canvas.line_width = self.border / xu
        canvas.stroke_rect(0, 0, cw / (1.0 * xu), ch / (1.0 * xu))

        #arrows
        if not self.running and self.check_vel.checked:
            for ball in balls:
                draw.vel_arrows(canvas, ball, self.linewidth, self.arrow_scale)

                draw.reset2(canvas, xu)
Esempio n. 2
0
 def draw_start(self):
     canvas = self.canvas
     draw.reset2(canvas, self.scale)
     canvas.translate(self.inpos[0], self.inpos[1])
     canvas.scale(self.hor_scale, self.ver_scale)
     canvas.rotate(self.rotate*math.pi/2)
     canvas.begin_path()
Esempio n. 3
0
  def draw_frame(self, canvas, balls):
    #draws border, arrows and balls on to canvas
    cw = self.cw
    ch = self.ch
    xu= self.xu

    #reset canvas
    draw.reset2(canvas,xu)

    #clear canvas
    draw.clear_canvas(canvas, "#fff")

    #balls
    for i in range(0,2):
        if i%2 == 0:
            canvas.fill_style = "#4c7fbe"
        else:
            canvas.fill_style ="#bb2828"
        draw.circle(canvas, balls[i].radius,balls[i].pos.x, balls[i].pos.y)
        canvas.fill()

    #borders
    canvas.stroke_style = "#000"
    canvas.line_width  = self.border/xu
    canvas.stroke_rect(0,0, cw/(1.0*xu),ch/(1.0*xu))


    #arrows
    if not self.running and self.check_vel.checked:
        for ball in balls:
            draw.vel_arrows(canvas, ball, self.linewidth, self.arrow_scale)

            draw.reset2(canvas, xu)
Esempio n. 4
0
 def btn_clear_click(self, **event_args):
     draw.reset2(self.canvas, 1)
     draw.clear_canvas(self.canvas, "#fff")
     self.graph.plot(xmarker=self.graph.xmarker / self.graph.xu,
                     ymarker=self.graph.ymarker / self.graph.yu,
                     colour="#fff")
     self.newvalues = []
    def draw_all(self):
        canvas = self.canvas
        self.cw = canvas.get_width()
        self.ch = canvas.get_height()
        cw = self.cw
        ch = self.ch
        xu = self.xu
        ball = self.ball

        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#fff")

        canvas.fill_style = "#000"
        canvas.fill_rect(0, 0, cw, self.floor_height)

        draw.circle(canvas, ball.radius, ball.pos.x, ball.pos.y)
        canvas.fill_style = "#3683f3"
        canvas.fill()

        #arrow
        if not self.running:
            ball = self.ball
            #velocity vector
            canvas.translate(ball.pos.x, ball.pos.y)
            canvas.rotate(ball.vel.phi())
            draw.arrow(canvas,
                       ball.vel.mag() * self.arrow_scale, 4 * self.linewidth)
            canvas.fill_style = "#49902a"
            canvas.fill()
            draw.reset2(canvas, xu)

        #dashes
        if not self.running:
            draw.paths(canvas, self.paths, self.linewidth, "#000")
    def timer_tick(self, **event_args):
        # This method is called Every [interval] seconds
        self.cw = self.canvas.get_width()
        self.ch = self.canvas.get_height()
        ch = self.ch

        canvas = self.canvas
        if self.first:
            self.slid_N = draw.slider(self.can_N, 3, 30, stepsize=1, start=4)
            self.slid_v = draw.slider(self.can_v,
                                      0,
                                      1,
                                      stepsize=0.01,
                                      start=0.01)
            self.slid_L = draw.slider(self.can_L,
                                      0.01,
                                      1,
                                      stepsize=0.01,
                                      start=0.1)
            self.L = self.slid_L.value
            self.v = self.slid_v.value
            self.N = self.slid_N.value
            self.param_boxes += [self.slid_N, self.slid_L, self.slid_v]
            self.a = 2 * math.pi / self.N
            self.first = False

        L = self.L
        v = self.v
        self.a = 2 * math.pi / self.N
        a = self.a
        #distance from centre to middle of one side
        self.d = float(L) * math.sqrt(
            (1 + math.cos(a)) / (1 - math.cos(a))) / 2
        d = self.d

        self.xu = self.give_xu(self.ch, self.d)
        xu = self.xu
        #if self.reset:
        self.initial(canvas)

        draw.reset2(canvas, self.xu)
        self.draw_path(canvas, self.path)

        if self.running:
            self.move_bug(self.bug)
            #self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
            draw.reset2(canvas, xu)

            if self.bug.pos.mag() <= self.bugradius:
                self.running = False
                self.btn_run.text = "Run"
                #self.initial(canvas)

        # if -0.1 <= self.xu - self.newxu <= 0.1:
        #     self.zoom = False
        # if self.zoom:
        #     self.xu += self.step

        self.counter += 1
Esempio n. 7
0
 def clear(self):
    '''Wipes the canvas and resets transformations.
       The wipe colour is a very light grey.
       The canvas system is set to bottom left origin (not to be confused with graph.origin)
       The canvas system is set to have horizontal left -> right, vertical bottom -> top.
       '''
    reset2(self.canvas, 1) # Vertical upward becomes +ve, origin of canvas set to bottom left
    self.canvas.fill_style = "#f5f5f5"
    self.canvas.fill_rect(0, 0, self.cw, self.ch) # Wipe with light grey
Esempio n. 8
0
    def btn_clear_click (self, **event_args):
        draw.reset2(self.canvas, 1)
        draw.clear_canvas(self.canvas, "#fff")
        self.newvalues = []
        self.all = []
        self.graph.func(self.startvals)
        self.lbl_mark.text = ""
        self.lbl_mark.background = "#fff"

        self.graph.plot(colour = "#fff", xmarker = self.xmarker, ymarker = self.ymarker)
    def timer_tick (self, **event_args):
        # This method is called Every [interval] seconds
        self.cw = self.canvas.get_width()
        self.ch  =self.canvas.get_height()
        ch = self.ch

        canvas = self.canvas
        if self.first:
            self.slid_N = draw.slider(self.can_N, 3, 30, stepsize = 1, start = 4)
            self.slid_v = draw.slider(self.can_v, 0, 1, stepsize = 0.01, start = 0.01)
            self.slid_L = draw.slider(self.can_L, 0.01, 1, stepsize = 0.01, start = 0.1)
            self.L = self.slid_L.value
            self.v =self.slid_v.value
            self.N =self.slid_N.value
            self.param_boxes += [self.slid_N, self.slid_L, self.slid_v]
            self.a = 2*math.pi/self.N
            self.first = False

        L = self.L
        v = self.v
        self.a = 2*math.pi/self.N
        a = self.a
        #distance from centre to middle of one side
        self.d = float(L)*math.sqrt((1+math.cos(a))/(1-math.cos(a)))/2
        d = self.d

        self.xu  =self.give_xu(self.ch, self.d)
        xu = self.xu
        #if self.reset:
        self.initial(canvas)

        draw.reset2(canvas, self.xu)
        self.draw_path(canvas, self.path)

        if self.running:
            self.move_bug(self.bug)
            #self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
            draw.reset2(canvas, xu)

            if self.bug.pos.mag() <= self.bugradius:
                self.running = False
                self.btn_run.text = "Run"
                #self.initial(canvas)

        # if -0.1 <= self.xu - self.newxu <= 0.1:
        #     self.zoom = False
        # if self.zoom:
        #     self.xu += self.step
        if self.counter %5 == 0:
            self.slid_N.draw()
            self.slid_v.draw()
            self.slid_L.draw()
        self.counter += 1
Esempio n. 10
0
 def btn_clear_click (self, **event_args):
     draw.reset2(self.canvas, 1)
     draw.clear_canvas(self.canvas, "#fff")
     self.graph2.plot(colour = "#fff", xmarker = self.xmarker, ymarker = self.ymarker)
     self.newvalues = []
     self.all = []
     self.grid_stat.clear()
     self.grid_x_int.clear()
     self.grid_y_int.clear()
     self.lbl_mark.text = ""
     self.lbl_mark.background = "#fff"
     self.started = False
Esempio n. 11
0
    def draw_all(self):
        canvas = self.canvas
        xu = self.xu
        canvas.xu  = xu
        ball = self.ball
        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#fff")

        #ball
        drawrad = ball.radius*(1/(1+ball.pos.z/3))
        if 0.01>drawrad or drawrad>10:
            self.running = False
            self.btn_run.text = "Run"
        else:
            draw.circle(canvas, drawrad, ball.pos.x, ball.pos.y)
        canvas.fill_style = "rgb(30, 96, 139)"
        canvas.fill()

        if self.running and self.check_trail.checked:
            b = len(self.trail)
            canvas.begin_path()
            canvas.move_to(self.trail[0].x, self.trail[0].y)
            canvas.quadratic_curve_to(self.trail[int(b/2)].x, self.trail[int(b/2)].y, self.trail[-1].x, self.trail[-1].y)
            canvas.line_width = 0.03
            canvas.stroke()

        #paths
        if not self.running and self.check_paths.checked:
            draw.paths(canvas,self.paths, self.line_width, "#000")

        #arrows
        if not self.running:
            draw.vel_arrows(canvas, self.ball, self.line_width, self.arrow_scale)
            draw.reset2(canvas, xu)

        #field arrows
        canvas.scale(1.0/self.xu, 1.0/self.xu)
        draw.cart_arrows(canvas, self.E, 3, 100/((self.E.mag()+1)), x = 30, y = 50)
        B2 = self.B*10e3
        draw.cart_arrows(canvas, B2, 3, 100/((B2.mag()+1)), x = (self.cw - 80), y = 50)
        canvas.scale(1,-1)
        canvas.font= "20px sans-serif"
        canvas.fill_text("E",50, -30 )
        canvas.fill_text("B",(self.cw - 60), -30 )
        canvas.scale(self.xu, -self.xu)


        #frame
        draw.border(canvas,5, "#000", xu)
    def draw_all(self):
        canvas = self.canvas
        draw.reset2(self.canvas, self.xu)
        draw.clear_canvas(self.canvas, "#fff")
        big = math.sqrt((self.cw/self.xu*1.0)**2 + (self.ch/self.xu*1.0)**2)


        for point in self.points:
            point.vel.x = self.spd_slider.value*0.1
            self.draw_source(point, self.canvas)
        for point in self.pulses:
            if point.wavefront >big:
                self.pulses.remove(point)
            else:
                self.draw_source(point, self.canvas, colour = "#fff")
    def draw_all(self):
        canvas = self.canvas
        draw.reset2(self.canvas, self.xu)
        draw.clear_canvas(self.canvas, "#fff")
        big = math.sqrt((self.cw / self.xu * 1.0)**2 +
                        (self.ch / self.xu * 1.0)**2)

        for point in self.points:
            point.vel.x = self.spd_slider.value * 0.1
            self.draw_source(point, self.canvas)
        for point in self.pulses:
            if point.wavefront > big:
                self.pulses.remove(point)
            else:
                self.draw_source(point, self.canvas, colour="#fff")
Esempio n. 14
0
 def draw_all(self):
     canvas = self.canvas
     draw.reset2(self.canvas, self.xu)
     draw.clear_canvas(self.canvas, "#fff")
     self.wav = self.slid_wav.value
     # div = self.cw/(self.xu*10.0)
     # canvas.begin_path()
     # for i in range(10):
     #     canvas.move_to(i*div, 0)
     #     canvas.line_to(i*div, self.ch/(self.xu*1.0))
     #
     # canvas.line_width = 0.003
     # canvas.stroke()
     for point in self.points:
         self.draw_source(point, self.canvas)
 def draw_all(self):
     canvas = self.canvas
     draw.reset2(self.canvas, self.xu)
     draw.clear_canvas(self.canvas, "#fff")
     self.wav = self.slid_wav.value
     # div = self.cw/(self.xu*10.0)
     # canvas.begin_path()
     # for i in range(10):
     #     canvas.move_to(i*div, 0)
     #     canvas.line_to(i*div, self.ch/(self.xu*1.0))
     #
     # canvas.line_width = 0.003
     # canvas.stroke()
     for point in self.points:
         self.draw_source(point, self.canvas)
    def draw(self, obj, colour, tides = False):
        cw= self.cw
        ch = self.ch
        xu = self.xu
        canvas = self.canvas

        canvas.translate(cw/(2*xu), ch/(2*xu))

        canvas.rotate(obj.angle)
        canvas.translate(obj.orbit, 0)

        if tides:
            canvas.scale(1.3, 1)
        draw.circle(canvas, obj.radius)

        canvas.fill_style = colour
        canvas.fill()

        draw.reset2(canvas, xu)
    def draw_slit_int(self, N, d, a, wav, choice = "slits"):
        canvas = self.canvas
        x_stp = self.x_stp
        x = []
        fx = []
        for i in range(int(float(self.W)/x_stp)):
            x.append(i*x_stp- float(self.W)/2)
            ang = math.asin((x[i] )/self.R)
            if choice =="slits":
                fx.append(self.slit_int(N,d,wav,ang))
            else:
                fx.append(self.single_int(a,wav,ang))

        graph = graphs.graph_plot(canvas,zip(x,fx))
        graph.yrange[0] = 0
        graph.xlabel = "x/m"
        graph.ylabel = "I"
        graph.axes_enabled = False
        graph.plot()
        draw.reset2(canvas, self.xu)
    def draw_slit_int(self, N, d, a, wav, choice="slits"):
        canvas = self.canvas
        x_stp = self.x_stp
        x = []
        fx = []
        for i in range(int(float(self.W) / x_stp)):
            x.append(i * x_stp - float(self.W) / 2)
            ang = math.asin((x[i]) / self.R)
            if choice == "slits":
                fx.append(self.slit_int(N, d, wav, ang))
            else:
                fx.append(self.single_int(a, wav, ang))

        graph = graphs.graph_plot(canvas, zip(x, fx))
        graph.yrange[0] = 0
        graph.xlabel = "x/m"
        graph.ylabel = "I"
        graph.axes_enabled = False
        graph.plot()
        draw.reset2(canvas, self.xu)
    def draw_all(self):
        draw.reset2(self.canvas, self.xu)
        draw.clear_canvas(self.canvas, "#fff")
        N = int(self.slits.N_slider.value)
        d = float(self.slits.d_slider.value)
        a = float(self.single.a_slider.value)
        n = float(self.grating.n_slider.value)

        if self.aperture == "slits":
            if self.rad_int.selected:
                self.draw_slit_int(N, d, a, self.wav)
            elif self.rad_pat.selected:
                self.draw_slit_pat(N, d, a, self.wav)
        elif self.aperture == "single":
            if self.rad_int.selected:
                self.draw_slit_int(N, d, a, self.wav, "single")
            elif self.rad_pat.selected:
                self.draw_slit_pat(N, d, a, self.wav, "single")

        elif self.aperture == "grating":
            self.draw_grating(n, self.wav)
    def draw_all(self):
        draw.reset2(self.canvas, self.xu)
        draw.clear_canvas(self.canvas, "#fff")
        N = int(self.slits.N_slider.value)
        d = float(self.slits.d_slider.value)
        a = float(self.single.a_slider.value)
        n = float(self.grating.n_slider.value)

        if self.aperture == "slits":
            if self.rad_int.selected:
                self.draw_slit_int(N,d,a, self.wav)
            elif self.rad_pat.selected:
                self.draw_slit_pat(N,d,a, self.wav)
        elif self.aperture == "single":
            if self.rad_int.selected:
                self.draw_slit_int(N,d,a, self.wav, "single")
            elif self.rad_pat.selected:
                self.draw_slit_pat(N,d,a, self.wav, "single")

        elif self.aperture == "grating":
            self.draw_grating(n, self.wav)
Esempio n. 21
0
    def draw_all(self):
        canvas = self.canvas
        cw = self.cw
        ch = self.ch
        xu = self.xu
        ball = self.ball

        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#7ec0ee")

        #floor
        canvas.fill_style = "#1f8107"
        canvas.fill_rect(0, 0, cw / self.xu, self.floor_height)

        #pole
        canvas.fill_style = "rgb(111, 62, 55)"
        canvas.fill_rect(self.L + self.ball.radius, self.floor_height, self.D,
                         3)
        canvas.translate(self.L + self.ball.radius - 0.05,
                         3 + self.floor_height)
        canvas.rotate(-math.pi / 2)
        draw.polygon(canvas, 3, self.D * 6)
        canvas.fill_style = "rgb(227, 81, 61)"
        canvas.fill()

        draw.reset2(canvas, xu)

        #ball
        draw.circle(canvas, ball.radius, ball.pos.x, ball.pos.y)
        canvas.fill_style = "#fff"
        canvas.fill()

        #arrow
        if not self.running:
            ball = self.ball
            #velocity vector
            canvas.translate(ball.pos.x, ball.pos.y)
            if ball.vel.y > 0:
                canvas.rotate(ball.vel.phi())
            else:
                canvas.rotate(-ball.vel.phi())
            draw.arrow(canvas,
                       ball.vel.mag() * self.arrow_scale, 4 * self.line_width)
            canvas.fill_style = "#49902a"
            canvas.fill()
            draw.reset2(canvas, xu)

        #dashes
        canvas.begin_path()
        if not self.running:
            for path in self.paths:
                if len(path) > 2:
                    for i in range(len(path) - 1):
                        canvas.move_to(path[i].x, path[i].y)
                        diff = path[i + 1] - path[i]
                        new = path[i] + diff * 0.8
                        canvas.line_to(new.x, new.y)

            canvas.line_width = self.line_width
            canvas.stroke()
    def btn_check_click(self, **event_args):
        tol = 100

        corr_x_ints = self.corr_x_ints
        corr_y_ints = self.corr_y_ints
        corr_x_stat = zip(*self.corrstats)[0]
        corr_y_stat = zip(*self.corrstats)[1]

        ent_x_ints = graphs.extract_vals(self.x_int_box)
        ent_y_ints = graphs.extract_vals(self.y_int_box)
        ent_x_stat = graphs.extract_vals(self.x_stat_box)
        ent_y_stat = graphs.extract_vals(self.y_stat_box)

        score = graphs.val_compare(ent_x_ints, corr_x_ints) + graphs.val_compare(ent_y_ints, corr_y_ints)
        score += graphs.val_compare(ent_x_stat, corr_x_stat) + graphs.val_compare(ent_y_stat, corr_y_stat)
        score *= 100/4


        self.lbl_mark.text = "{0}%".format(round(score))

        self.lbl_mark.background = "#fff"

        #TODO pass mark checking
        if score >self.pass_mark:
            self.lbl_mark.text += "\nWell done!"
        else:
            self.lbl_mark.text += "\nScore  over {0}% to pass".format(self.pass_mark)

        draw.reset2(self.canvas, 1)
        draw.clear_canvas(self.canvas)
        self.graph.plot(colour = "rgb(214, 106, 72)", xmarker = self.xmarker, ymarker = self.ymarker)

        xlabs = [x.text for x in self.x_int_box]
        ylabs = [x.text for x in self.y_int_box]
        statlabs = ["({0}, {1})".format(self.x_stat_box[i].text, self.y_stat_box[i].text) for i in range(len(self.x_stat_box))]


        self.graph.circle_points(zip(self.test_x_ints, [0]*len(self.test_x_ints)), self.x_circle, pointlabels = xlabs)
        self.graph.circle_points(zip([0]*len(self.test_y_ints), self.test_y_ints), self.y_circle, pointlabels = ylabs, pointoffset = len(xlabs))
        self.graph.circle_points(self.teststats, self.stat_circle, pointlabels = statlabs, pointoffset = len(xlabs) + len(ylabs))
Esempio n. 23
0
 def draw(self):
     self.draw_start()
     canvas = self.canvas
     canvas.move_to(0,0)
     canvas.line_to(10, 0)
     canvas.arc(20, 0, 10, 0, 2*math.pi)
     canvas.translate(20, 0)
     canvas.rotate(math.pi/4)
     canvas.move_to(-10,0)
     canvas.line_to(10, 0)
     canvas.move_to(0,-10)
     canvas.line_to(0, 10)
     canvas.rotate(-math.pi/4)
     canvas.translate(-20, 0)
     canvas.move_to(30, 0)
     canvas.line_to(40, 0)
     self.draw_end()
     canvas.translate(self.inpos[0] + 20, self.inpos[1])
     canvas.arc(0, 0, 10 - (self.outV-self.inV)*self.I, 0, 2*math.pi)
     canvas.fill_style = "rgb(195, 238, 48)"
     canvas.fill()
     draw.reset2(canvas, self.scale)
Esempio n. 24
0
 def initial(self, canvas):
     draw.reset2(canvas, self.xu)
     draw.clear_canvas(canvas, "#fff")
     self.draw_polygon(canvas, self.N, self.L)
     self.bug.pos = physics.vector3(-self.L / 2, self.d, 0)
     draw.reset2(canvas, self.xu)
     self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
     draw.reset2(canvas, self.xu)
Esempio n. 25
0
 def initial(self, canvas):
     draw.reset2(canvas, self.xu)
     draw.clear_canvas(canvas, "#fff")
     self.draw_polygon(canvas, self.N, self.L)
     self.bug.pos = physics.vector3(-self.L/2, self.d, 0)
     draw.reset2(canvas, self.xu)
     self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
     draw.reset2(canvas, self.xu)
Esempio n. 26
0
    def timer_tick(self, **event_args):
        # This method is called Every [interval] seconds
        self.cw = self.canvas.get_width()
        self.ch = self.canvas.get_height()
        ch = self.ch

        canvas = self.canvas

        L = self.L
        v = self.v
        a = self.a
        #distance from centre to middle of one side
        self.d = float(L) * math.sqrt(
            (1 + math.cos(a)) / (1 - math.cos(a))) / 2
        d = self.d
        if self.reset and not self.zoom:
            self.xu = self.give_xu(self.ch, self.d)
            self.newxu = self.give_xu(self.ch, self.d)
        xu = self.xu
        if self.reset:
            self.initial(canvas)

        draw.reset2(canvas, self.xu)

        if self.running:
            self.move_bug(self.bug)
            self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
            draw.reset2(canvas, xu)

            if self.bug.pos.mag() <= self.bugradius:
                self.running = False
                self.btn_run.text = "Run"
                #self.initial(canvas)

        if -0.1 <= self.xu - self.newxu <= 0.1:
            self.zoom = False
        if self.zoom:
            self.xu += self.step
    def draw_grating(self, n, wav):
        canvas = self.canvas

        draw.reset2(canvas, 1)
        canvas.translate(float(self.cw)/2, 0)
        col = draw.wavelength_to_rgb(self.wav*1e9)

        canvas.line_width = 5
        step = self.wav*n*self.R
        x = 0
        canvas.begin_path()
        while x*self.xu<float(self.cw)/2:
            xt = x*self.xu

            canvas.move_to(xt,0)
            canvas.line_to(xt, float(self.ch))
            canvas.move_to(-xt,0)
            canvas.line_to(-xt, float(self.ch))
            x+=step

        canvas.stroke_style = "rgb({0}, {1}, {2})".format(col[0],col[1],col[2])
        canvas.stroke()
        draw.reset2(canvas, self.xu)
Esempio n. 28
0
    def draw_all(self):
        canvas = self.canvas
        cw= self.cw
        ch = self.ch
        xu = self.xu
        ball= self.ball

        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#7ec0ee")

        #floor
        canvas.fill_style = "#1f8107"
        canvas.fill_rect(0, 0,cw/self.xu , self.floor_height)

        #pole
        canvas.fill_style =  "rgb(111, 62, 55)"
        canvas.fill_rect(self.L+self.ball.radius, self.floor_height, self.D, 3)
        canvas.translate(self.L+self.ball.radius-0.05, 3+self.floor_height)
        canvas.rotate(-math.pi/2)
        draw.polygon(canvas,3, self.D*6)
        canvas.fill_style =  "rgb(227, 81, 61)"
        canvas.fill()

        draw.reset2(canvas, xu)

        #ball
        draw.circle(canvas, ball.radius, ball.pos.x, ball.pos.y)
        canvas.fill_style= "#fff"
        canvas.fill()

        #arrow
        if not self.running:
            ball = self.ball
            #velocity vector
            canvas.translate(ball.pos.x, ball.pos.y)
            if ball.vel.y>0:
                canvas.rotate(ball.vel.phi())
            else:
                canvas.rotate(-ball.vel.phi())
            draw.arrow(canvas, ball.vel.mag()*self.arrow_scale, 4*self.line_width)
            canvas.fill_style = "#49902a"
            canvas.fill()
            draw.reset2(canvas, xu)

        #dashes
        canvas.begin_path()
        if not self.running:
            for path in self.paths:
                if len(path)>2:
                    for i in range(len(path)-1):
                        canvas.move_to(path[i].x, path[i].y)
                        diff = path[i+1] - path[i]
                        new  = path[i] + diff*0.8
                        canvas.line_to(new.x, new.y)

            canvas.line_width = self.line_width
            canvas.stroke()
Esempio n. 29
0
    def timer_tick (self, **event_args):
        # This method is called Every [interval] seconds
        self.cw = self.canvas.get_width()
        self.ch  =self.canvas.get_height()
        ch = self.ch

        canvas = self.canvas

        L = self.L
        v = self.v
        a = self.a
        #distance from centre to middle of one side
        self.d = float(L)*math.sqrt((1+math.cos(a))/(1-math.cos(a)))/2
        d = self.d
        if self.reset and not self.zoom:
            self.xu  =self.give_xu(self.ch, self.d)
            self.newxu = self.give_xu(self.ch, self.d)
        xu = self.xu
        if self.reset:
            self.initial(canvas)

        draw.reset2(canvas, self.xu)

        if self.running:
            self.move_bug(self.bug)
            self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
            draw.reset2(canvas, xu)

            if self.bug.pos.mag() <= self.bugradius:
                self.running = False
                self.btn_run.text = "Run"
                #self.initial(canvas)

        if -0.1 <= self.xu - self.newxu <= 0.1:
            self.zoom = False
        if self.zoom:
            self.xu += self.step
Esempio n. 30
0
    def draw_frame(self, canvas, balls):
        #draws border, arrows and balls on to canvas
        cw = self.cw
        ch = self.ch
        xu = self.xu

        #reset canvas
        draw.reset2(canvas, xu)

        #clear canvas
        draw.clear_canvas(canvas, "#fff")

        #balls
        for i in range(0, 2):
            if i % 2 == 0:
                canvas.fill_style = "#4c7fbe"
            else:
                canvas.fill_style = "#bb2828"
            draw.circle(canvas, balls[i].radius, balls[i].pos.x,
                        balls[i].pos.y)
            canvas.fill()

        #borders
        canvas.stroke_style = "#000"
        canvas.line_width = self.border / xu
        canvas.stroke_rect(0, 0, cw / (1.0 * xu), ch / (1.0 * xu))

        #arrows
        if not self.running and self.check_vel.checked:
            for ball in balls:
                #x component
                draw.arrow(canvas, ball.vel.x * self.arrow_scale,
                           2 * self.linewidth, ball.pos.x, ball.pos.y)
                canvas.fill_style = "#333333"
                canvas.fill()

                #y component
                canvas.translate(ball.pos.x, ball.pos.y)
                canvas.rotate(math.pi / 2)
                draw.arrow(canvas, ball.vel.y * self.arrow_scale,
                           2 * self.linewidth)
                canvas.fill()
                draw.reset2(canvas, xu)

                #velocity vector
                canvas.translate(ball.pos.x, ball.pos.y)
                if ball.vel.y > 0:
                    canvas.rotate(ball.vel.phi())
                else:
                    canvas.rotate(-ball.vel.phi())
                draw.arrow(canvas,
                           ball.vel.mag() * self.arrow_scale,
                           4 * self.linewidth)
                canvas.fill_style = "#49902a"
                canvas.fill()

                draw.reset2(canvas, xu)
    def draw_all(self):
        canvas = self.canvas
        self.cw  = canvas.get_width()
        self.ch = canvas.get_height()
        cw= self.cw
        ch = self.ch
        xu = self.xu
        ball= self.ball

        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#fff")

        canvas.fill_style = "#000"
        canvas.fill_rect(0, 0, cw, self.floor_height)

        draw.circle(canvas, ball.radius, ball.pos.x, ball.pos.y)
        canvas.fill_style= "#3683f3"
        canvas.fill()

        #arrow
        if not self.running:
            ball = self.ball
            #velocity vector
            canvas.translate(ball.pos.x, ball.pos.y)
            if ball.vel.y>0:
                canvas.rotate(ball.vel.phi())
            else:
                canvas.rotate(-ball.vel.phi())
            draw.arrow(canvas, ball.vel.mag()*self.arrow_scale, 4*self.linewidth)
            canvas.fill_style = "#49902a"
            canvas.fill()
            draw.reset2(canvas, xu)

        #dashes
        if not self.running:
            draw.paths(canvas,self.paths, self.linewidth, "#000")
    def draw_grating(self, n, wav):
        canvas = self.canvas

        draw.reset2(canvas, 1)
        canvas.translate(float(self.cw) / 2, 0)
        col = draw.wavelength_to_rgb(self.wav * 1e9)

        canvas.line_width = 5
        step = self.wav * n * self.R
        x = 0
        canvas.begin_path()
        while x * self.xu < float(self.cw) / 2:
            xt = x * self.xu

            canvas.move_to(xt, 0)
            canvas.line_to(xt, float(self.ch))
            canvas.move_to(-xt, 0)
            canvas.line_to(-xt, float(self.ch))
            x += step

        canvas.stroke_style = "rgb({0}, {1}, {2})".format(
            col[0], col[1], col[2])
        canvas.stroke()
        draw.reset2(canvas, self.xu)
    def draw_all(self):
        canvas = self.canvas
        self.cw  = canvas.get_width()
        self.ch = canvas.get_height()
        cw= self.cw
        ch = self.ch
        xu = self.xu

        draw.reset2(canvas, xu)
        self.draw(self.tides, "#ffd600", True)
        self.draw(self.earth, "#008cff")
        self.draw(self.moon, "#00ff94")
        canvas.begin_path()
        canvas.translate(cw/(2*xu), ch/(2*xu))
        canvas.move_to(0,0)
        canvas.rotate(self.earth.angle)
        canvas.line_to(self.earth.radius, 0)
        canvas.line_width =0.05
        canvas.stroke_style =  "#d6786b"
        canvas.stroke()
        draw.reset2(canvas, xu)

        if self.check_arrows.checked:
            #arrows
            canvas.translate(cw/(2*xu), ch/(2*xu))

            canvas.rotate(self.moon.angle)
            canvas.translate(self.moon.orbit, 0)
            canvas.rotate(math.pi/2 -(self.moon.angle-self.tides.angle))
            draw.arrow(canvas,-0.01*(self.earth.ang_vel - self.moon.ang_vel), 0.2)
            canvas.fill_style = "#d14f42"
            canvas.fill()


            draw.reset2(canvas, xu)
            canvas.translate(cw/(2*xu), ch/(2*xu))
            canvas.rotate(self.tides.angle)
            canvas.translate(1.3*self.earth.radius, 0)
            canvas.rotate(math.pi/2)
            draw.arrow(canvas,0.01*(self.earth.ang_vel - self.moon.ang_vel), 0.2)
            canvas.fill_style = "#d14f42"
            canvas.fill()
            draw.reset2(canvas, xu)
Esempio n. 34
0
    def draw_frame(self, canvas, balls):
        # draws border, arrows and balls on to canvas
        cw = self.cw
        ch = self.ch
        xu = self.xu

        # reset canvas
        draw.reset2(canvas, xu)

        # clear canvas
        draw.clear_canvas(canvas, "#fff")

        # balls
        for i in range(0, 2):
            if i % 2 == 0:
                canvas.fill_style = "#4c7fbe"
            else:
                canvas.fill_style = "#bb2828"
            draw.circle(canvas, balls[i].radius, balls[i].pos.x, balls[i].pos.y)
            canvas.fill()

        # borders
        canvas.stroke_style = "#000"
        canvas.line_width = self.border / xu
        canvas.stroke_rect(0, 0, cw / (1.0 * xu), ch / (1.0 * xu))

        # arrows
        if not self.running and self.check_vel.checked:
            for ball in balls:
                # x component
                draw.arrow(canvas, ball.vel.x * self.arrow_scale, 2 * self.linewidth, ball.pos.x, ball.pos.y)
                canvas.fill_style = "#333333"
                canvas.fill()

                # y component
                canvas.translate(ball.pos.x, ball.pos.y)
                canvas.rotate(math.pi / 2)
                draw.arrow(canvas, ball.vel.y * self.arrow_scale, 2 * self.linewidth)
                canvas.fill()
                draw.reset2(canvas, xu)

                # velocity vector
                canvas.translate(ball.pos.x, ball.pos.y)
                if ball.vel.y > 0:
                    canvas.rotate(ball.vel.phi())
                else:
                    canvas.rotate(-ball.vel.phi())
                draw.arrow(canvas, ball.vel.mag() * self.arrow_scale, 4 * self.linewidth)
                canvas.fill_style = "#49902a"
                canvas.fill()

                draw.reset2(canvas, xu)
Esempio n. 35
0
    def initial(self, canvas):
        draw.reset2(canvas, self.xu)
        draw.clear_canvas(canvas, "#fff")

        self.N  =  self.slid_N.value
        self.L = self.slid_L.value
        self.v = self.slid_v.value

        self.draw_polygon(canvas, self.N, self.L)
        if self.reset:
            self.bug.pos = physics.vector3(-self.L/2, self.d, 0)
            self.path  = [self.bug.pos, self.bug.pos]
        draw.reset2(canvas, self.xu)
        self.draw_bugs(canvas, self.bug.pos, "#2a2ac7")
        # for i in range(len(self.path)-1):
        #     canvas.begin_path()
        #     canvas.move_to(self.path[i].x, self.path[i].y)
        #     canvas.line_to(self.path[i+1].x, self.path[i+1].y)
        #     canvas.stroke()

        draw.reset2(canvas, self.xu)
Esempio n. 36
0
    def draw_all(self):
        canvas = self.canvas
        xu = self.xu
        canvas.xu = xu
        ball = self.ball
        draw.reset2(canvas, xu)
        draw.clear_canvas(canvas, "#fff")

        #ball
        drawrad = ball.radius * (1 / (1 + ball.pos.z / 3))
        if 0.01 > drawrad or drawrad > 10:
            self.running = False
            self.btn_run.text = "Run"
        else:
            draw.circle(canvas, drawrad, ball.pos.x, ball.pos.y)
        canvas.fill_style = "rgb(30, 96, 139)"
        canvas.fill()

        if self.running and self.check_trail.checked:
            b = len(self.trail)
            canvas.begin_path()
            canvas.move_to(self.trail[0].x, self.trail[0].y)
            canvas.quadratic_curve_to(self.trail[int(b / 2)].x,
                                      self.trail[int(b / 2)].y,
                                      self.trail[-1].x, self.trail[-1].y)
            canvas.line_width = 0.03
            canvas.stroke()

        #paths
        if not self.running and self.check_paths.checked:
            draw.paths(canvas, self.paths, self.line_width, "#000")

        #arrows
        if not self.running:
            draw.vel_arrows(canvas, self.ball, self.line_width,
                            self.arrow_scale)
            draw.reset2(canvas, xu)

        #field arrows
        canvas.scale(1.0 / self.xu, 1.0 / self.xu)
        draw.cart_arrows(canvas,
                         self.E,
                         3,
                         100 / ((self.E.mag() + 1)),
                         x=30,
                         y=50)
        B2 = self.B * 10e3
        draw.cart_arrows(canvas,
                         B2,
                         3,
                         100 / ((B2.mag() + 1)),
                         x=(self.cw - 80),
                         y=50)
        canvas.scale(1, -1)
        canvas.font = "20px sans-serif"
        canvas.fill_text("E", 50, -30)
        canvas.fill_text("B", (self.cw - 60), -30)
        canvas.scale(self.xu, -self.xu)

        #frame
        draw.border(canvas, 5, "#000", xu)
Esempio n. 37
0
   def draw(self):
      '''Calling this method draws the graph, overwriting anything currently on the graph's canvas.
         '''
      # Clear the canvas and set vertical to upward
      self.clear()
      
      self.set_origin() # Make sure the origin position and scaling factors are up to date
      
      # Draw the axes - the self.gaps and (2 * self.gap)s just leave a gap between axis ends and the canvas edge
      # This is simpler without translation to the origin, so that isn't done yet
      # The axis objects are just vectors of the correct length and direction
      xaxis = vector3(self.cw - (2 * self.gap), 0)
      yaxis = vector3(0, self.ch - (2 * self.gap))
      draw_line(self.canvas, vector3(self.gap, self.origin.y), xaxis, self.axis_wt, self.axis_colour)
      draw_line(self.canvas, vector3(self.origin.x, self.gap), yaxis, self.axis_wt, self.axis_colour)
      # Now add arrows at the ends of the axis
      draw_arrow(self.canvas, vector3(self.gap, self.origin.y) + xaxis, vector3(10, 0), self.axis_wt, self.axis_colour)
      draw_arrow(self.canvas, vector3(self.origin.x, self.gap) + yaxis, vector3(0, 10), self.axis_wt, self.axis_colour)
      # If wanted, draw the gridlines
      if self.x_gridlines is True:
         for x in self.x_divis:
            draw_line(self.canvas, vector3(self.origin.x + self.scale_x * x, self.gap), yaxis, 0.5 * self.axis_wt, self.axis_colour)
      if self.y_gridlines is True:
         for y in self.y_divis:
            draw_line(self.canvas, vector3(self.gap, self.origin.y + self.scale_y * y), xaxis, 0.5 * self.axis_wt, self.axis_colour)

      # Add the text to the graph
      # For the text sections all plotting y values are multiplied by -1 to compensate for the coord flip
      # Text is in general drawn half the gap distance from the relevant axis point
      self.canvas.fill_style = "#000"
      # Start with the labels, these are the last objects not to use the new origin to draw from
      self.canvas.font = "12px sans-serif"
      self.canvas.scale(1, -1) # Need to temporarily flip the coordinates or the text ends up upside down
      self.canvas.text_align = "left"
      self.canvas.text_baseline = "bottom"
      self.canvas.fill_text(self.x_label, 0.5 * self.cw - 0.5 * self.canvas.measure_text(self.x_label), -1 * self.axis_wt) # Baseline just above the edge of the canvas
      self.canvas.text_align = "left"
      self.canvas.text_baseline = "top"
      self.canvas.reset_transform() # For ease of use
      self.canvas.translate(self.axis_wt, 0.5 * self.ch + 0.5 * self.canvas.measure_text(self.y_label)) # "Top" of the rotated text just offset from the edge of the canvas
      self.canvas.rotate(math.pi / -2) # Rotate y-axis label
      self.canvas.fill_text(self.y_label, 0, 0)
      reset2(self.canvas, 1) # Reset to vertical positive, bottom left origin
      
      # Set the origin for all future drawing operations (until clear() is called)
      self.canvas.translate(self.origin.x, self.origin.y)
      
      # If gridlines are not on, draw tick marks for positions along the axis
      # Tick marks are 3 pixels long
      if self.x_gridlines is False:
         for x in self.x_divis:
            draw_line(self.canvas, vector3(x * self.scale_x, 0), vector3(0, -3))
      if self.y_gridlines is False:
         for y in self.y_divis:
            draw_line(self.canvas, vector3(0, y * self.scale_y), vector3(-3, 0))

      # Continue adding text with the new origin
      self.canvas.font = "10px sans-serif"
      self.canvas.scale(1, -1) # Need to temporarily flip the coordinates or the text ends up upside down
      if self.x_marks is True:
         self.canvas.text_align = "right"
         self.canvas.text_baseline = "top"
         for x in self.x_divis:
            self.canvas.fill_text("{0}".format(sig_round(x, self.x_sig_figs)), x * self.scale_x - self.axis_wt, self.axis_wt)
      if self.y_marks is True:
         self.canvas.text_align = "right"
         self.canvas.text_baseline = "top"
         # This now removes the closest element in y to 0, to avoid labelling 0 on both axes
         # and to account for floats possibly not being exactly 0
         for y in [a for a in self.y_divis if abs(a) != min([abs(b) for b in self.y_divis])]:
            self.canvas.fill_text("{0}".format(sig_round(y, self.y_sig_figs)), -1, -1 * y * self.scale_y)
      self.canvas.scale(1, -1) # Need to temporarily flip the coordinates or the text ends up upside down

      # Now plot the dataset
      for name in self.data:
         if self.plot_type[name] == "line" and len(self.data[name]) != 0:
            self.canvas.stroke_style = self.line_colour[name]
            self.canvas.line_width = self.line_wt[name]
            prev = self.data[name][0] # Keep note of the previous data point
            self.canvas.begin_path()
            for point in self.data[name]: # Draw a line between the previous data point and current for each point
               self.canvas.move_to(prev.x * self.scale_x, prev.y * self.scale_y)
               self.canvas.line_to(point.x * self.scale_x, point.y * self.scale_y)
               prev = vector3(point.x, point.y) # Note that the current point is now the previous point
            self.canvas.stroke() # Draw the whole line
         elif self.plot_type[name] == "points" and len(self.data[name]) != 0:
            self.canvas.fill_style = self.line_colour[name]
            r = 1.5 * self.line_wt[name]
            for point in self.data[name]: # Draw a circle of diameter 3x the line width at each data point (for visibility)
               self.canvas.begin_path()
               self.canvas.arc(point.x * self.scale_x, point.y * self.scale_y, r)
               self.canvas.fill()
 def btn_clear_click (self, **event_args):
     draw.reset2(self.canvas, 1)
     draw.clear_canvas(self.canvas, "#fff")
     self.graph.plot(xmarker = self.graph.xmarker/self.graph.xu, ymarker = self.graph.ymarker/self.graph.yu, colour = "#fff")
     self.newvalues = []
Esempio n. 39
0
 def draw_end(self):
     canvas = self.canvas
     canvas.stroke_style = "#000"
     canvas.line_width = self.line_width
     canvas.stroke()
     draw.reset2(canvas, self.scale)