Ejemplo n.º 1
0
    def line(self):
        stddraw.setYscale(-1.5, 1.5)
        stddraw.setXscale(-1.5, 1.5)
        # try:
        done_point = []
        for (x1, y1) in zip(self.x, self.y):
            if (x1, y1) not in done_point:

                done_point.append((x1, y1))

                for (x2, y2) in zip(self.x, self.y):
                    if (x1, y1) != (x2, y2) and self.possibility():
                        color_list = [
                            stddraw.LIGHT_GRAY, stddraw.RED, stddraw.BLUE
                        ]
                        color_choice = random.sample(color_list, 1)
                        stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE)
                        stddraw.setPenRadius(r=0.01)
                        stddraw.line(x1, y1, x2, y2)
                        stddraw.show(20)
                    else:
                        pass
            else:
                pass
        for (x, y) in zip(self.x, self.y):
            stddraw.setPenRadius(r=0.02)
            stddraw.setPenColor(stddraw.BLACK)
            stddraw.point(x, y)
Ejemplo n.º 2
0
def draw_line(dragonline, turn):
    stddraw.setXscale(-5, 5)
    stddraw.setYscale(-5, 5)
    stddraw.setFontSize(20)

    theta = 0
    x_list = [0, 1]
    y_list = [0, 0]
    stddraw.line(x_list[0], y_list[0], x_list[1], y_list[1])
    x = 1
    y = 0
    List = dragonline.turn_flow(turn)
    Turn = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    for index in range(2 * (turn - 1) + 1):
        det_theta = 1 if List[index] == 'L' else -1
        theta = (theta + det_theta) % 4

        det_x = Turn[theta][0]
        det_y = Turn[theta][1]
        x = x + det_x
        y = y + det_y

        x_list.append(round(x))
        y_list.append(round(y))

        stddraw.setPenColor(stddraw.BLACK)
        stddraw.line(x_list[index], y_list[index], x_list[index + 1],
                     y_list[index + 1])
        stddraw.setPenColor(stddraw.RED)
        # stddraw.text(x_list[index],y_list[index],'({0},{1}) to ({2},{3})'.format(x_list[index],y_list[index],x_list[index+1],y_list[index+1]))
        # stddraw.text(x_list[index],y_list[index],str(index))
        stddraw.show(50)
Ejemplo n.º 3
0
def draw_spirograph(R,r,a):
    stddraw.setXscale(-40,40)
    stddraw.setYscale(-40,40)
    l = 1500
    x = stdarray.create1D(l,0)
    y = stdarray.create1D(l,0)
    for t in range(l):
        x[t] = (R+r)*math.cos(t) - (r+a) * math.cos((R+r)*t/r)
        y[t] = (R+r)*math.sin(t) - (r+a) * math.sin((R+r)*t/r)

    for t in range(l+1):
        if t >= l-1:
            pass
        else:
            if t ==0:
                stddraw.setPenColor(stddraw.RED)
            if t==l/3:
                stddraw.setPenColor(stddraw.DARK_BLUE)
            if t == 2*l/3:
                stddraw.setPenColor(stddraw.BOOK_BLUE)


            stddraw.line(x[t],y[t],x[t+1],y[t+1])
            stddraw.show(5)
    stddraw.show()
Ejemplo n.º 4
0
def draw_possibility():
    print('enter draw_possibility')
    stddraw.setYscale(-0.5,100)
    stddraw.setXscale(-50,400)
    for i in range(len(possibility)-1):
        stddraw.line(i,possibility[i],i+1,possibility[i+1])

    stddraw.show()
Ejemplo n.º 5
0
def main(x_0, x_1, y_0, y_1, n):
    stddraw.setXscale(0, 6)
    stddraw.setYscale(0, 10)
    global B_list
    B_list = [[x_0, y_0], [x_1, y_1]]
    print(B_list)

    brownian(x_0, y_0, x_1, y_1, n)
    print(B_list)
    draw(B_list)
Ejemplo n.º 6
0
def draw_count(countlist):
    # stddraw.setCanvasSize(100,100)
    stddraw.setXscale(-1,10)
    stddraw.setYscale(-0.1,0.5)
    stddraw.setPenColor(stddraw.GRAY)
    stddraw.line(0,0,100,0)
    stddraw.line(0,0,0,5000)
    for List in countlist:
        for i in range(1,len(List)):
            stddraw.line(i-1,List[i-1],i,List[i])
Ejemplo n.º 7
0
def main():
    duration = int(input('what is the duration?(min):'))
    status = input('start to work?(Y/N)')

    while status == 'Y':
        stddraw.clear()
        stddraw.setXscale(0, 10)
        stddraw.setYscale(0, 10)
        time.sleep(duration * 60)
        stddraw.text(5, 5, 'go to do sport')
        stddraw.show()
        status = input('continue to work?(Y/N')
        if status == 'N':
            break
Ejemplo n.º 8
0
    def __init__(self, n):

        self._balls = stdarray.create1D(n)
        self._min = -50
        self._max = 50
        stddraw.setYscale(self._min, self._max)
        stddraw.setXscale(self._min, self._max)
        for i in range(n):
            mass = random.randint(1, 10)
            radius = random.uniform(1, 2)
            velocity = Vector([random.randint(0, 3), random.randint(0, 3)])
            location = Vector([random.randint(0, 10), random.randint(5, 10)])
            print('generate location:', location)
            ball = Ball(mass, radius, velocity, location)
            self._balls[i] = ball
Ejemplo n.º 9
0
def run(n):

    dice_1 = [1, 3, 4, 5, 6, 8]
    dice_2 = [1, 2, 2, 3, 3, 4]
    dice_s = [1, 2, 3, 4, 5, 6]

    result1 = dice(dice_1, dice_2, n)
    result2 = dice(dice_s, dice_s, n)

    print(result1, result2)

    stddraw.setYscale(0, 1.1 * max(max(result1), max(result2)))
    stdstats.plotLines(result1)
    stdstats.plotBars(result2)
    stddraw.show(50)
Ejemplo n.º 10
0
    def __init__(self, filename):
        instream = InStream(filename)
        n = instream.readInt()
        radius = instream.readFloat()
        stddraw.setXscale(-radius, +radius)
        stddraw.setYscale(-radius, +radius)

        self._bodies = stdarray.create1D(n)
        for i in range(n):
            rx = instream.readFloat()
            ry = instream.readFloat()
            vx = instream.readFloat()
            vy = instream.readFloat()
            mass = instream.readFloat()
            r = Vector([rx, ry])
            v = Vector([vx, vy])
            self._bodies[i] = Body(r, v, mass)
Ejemplo n.º 11
0
def draw_setup(n):
    stddraw.setYscale(-1, 11)
    stddraw.setXscale(-1, 11)
    stddraw.line(0, -1, 10, -1)
    location = {'a': (3, 0, 3, 8), 'b': (6, 0, 6, 8), 'c': (9, 0, 9, 8)}

    stddraw.line(3, 0, 3, 8)
    stddraw.line(6, 0, 6, 8)
    stddraw.line(9, 0, 9, 8)
    for i in range(1, n + 2):
        print(i)

        stddraw.line(3 - i / n, 8 * (n + 1 - i) / (n + 1), 3 + i / n,
                     8 * (n - i + 1) / (n + 1))
        stddraw.text(3 - i / n - 0.5, 8 * (n - i) / n, str(i))

    return location
Ejemplo n.º 12
0
    def audio_note(self):
        stddraw.setXscale(0, 50)
        stddraw.setYscale(-10, 10)

        print('enter audio_note')
        duration = self.f
        hz = notes[self.n - 1]
        n = int(SPS * duration)
        samples = stdarray.create1D(n + 1, 0.0)
        for i in range(n + 1):
            samples[i] = math.sin(2.0 * math.pi * i * hz / SPS)
        print(samples)
        for i in range(0, n - 100, 100):
            stddraw.line(i / 500, samples[i], (i + 100) / 500,
                         samples[i + 100])
        stddraw.show()
        stdaudio.playSample(samples)
        print('play')
        stdaudio.wait()
Ejemplo n.º 13
0
    def blome(self):
        stddraw.setXscale(-1.2, 1.2)
        stddraw.setYscale(-1.2, 1.2)
        x_list = []
        y_list = []
        for i in range(1, self.n + 1):
            theta = self.theta
            for n in range(100):

                x = math.sin(theta) * math.cos(theta)
                y = math.sin(theta) * math.sin(theta)
                x_list.append(x)
                y_list.append(y)
                if n > 0:
                    stddraw.line(x_list[n - 1], y_list[n - 1], x_list[n],
                                 y_list[n])
                    stddraw.show(50)

                theta += n * self.theta / 100
Ejemplo n.º 14
0
def main():


#     parser = optparse.OptionParser("""\
# usage: %prog [options] infile outfile
#
# draw circles in random location ,with random radius and color.""")
#
#     parser.add_option("-n", "--number", dest="n",
#             help=("input the number"))
#
#     parser.add_option("-p", "--possibility", dest="p",
#             help=("input the possibility in black"))
#
#
#     opts, args = parser.parse_args()
#
#     n = int(opts.n)
#     p = float(opts.p)


    stddraw.setXscale(0,2)
    stddraw.setYscale(0,2)


    i = 0
    while i < n:
        x = 2*random.random()
        y = 2*random.random()
        r = 0.01*random.randrange(1,30)
        color = stddraw.BLUE if random.random()<p else stddraw.BOOK_RED
        stddraw.setPenColor(color)
        stddraw.filledCircle(x,y,r)

        i +=1
        stddraw.show(10)
Ejemplo n.º 15
0
def draw(a, which):

    n = len(a)
    stddraw.setXscale(-1, n)
    stddraw.setYscale(-1, n)
    if which == False:
        for i in range(n):
            for j in range(n):
                if a[i][j] == which:
                    stddraw.filledSquare(j, n - i - 1, 0.5)

    elif which == True:
        Li = []

        for i in range(n):
            for j in range(n):
                print(i, j)
                if a[i][j] == False:
                    Li.append(j)
                    print('{0} is false'.format(j))
                if a[i][j] == which and (j not in Li):
                    print('draw at({0},{1})'.format(i, j))

                    stddraw.filledSquare(j, n - i - 1, 0.5)
Ejemplo n.º 16
0
def draw_clock(H=0, M=0, S=0):
    stddraw.setXscale(-1.5, 1.5)
    stddraw.setYscale(-1.5, 1.5)

    R = 1.1
    Hour_L = 0.4
    Min_L = 0.55
    Sec_L = 0.7
    Hour_count = 0

    Hour_X = stdarray.create1D(12, 0)
    Hour_Y = stdarray.create1D(12, 0)

    for i in range(1, 13):
        Hour_X[i - 1] = 1 * math.cos(-(i / 12 * 2 * math.pi - 1 / 2 * math.pi))
        Hour_Y[i - 1] = 1 * math.sin(-(i / 12 * 2 * math.pi - 1 / 2 * math.pi))

    stddraw.setFontSize(25)

    if H > 11:
        print('H is {0}'.format(H))
        H = H % 12
        Hour_count = 1

    while True:
        while H < 13:

            angle_H = H * 2 * math.pi / 12 - 1 / 2 * math.pi
            print('H is {0}'.format(H))
            if H == 12:
                H = 0
                Hour_count += 1

            H += 1

            if Hour_count % 2 == 0:
                text = 'morining'
            else:
                text = 'afternoon'

            while M < 61:

                angle_M = M * 2 * math.pi / 60 - 1 / 2 * math.pi
                print('m is {0}'.format(M))

                if M == 60:
                    print('m is 60')
                    M = 0
                    break
                else:
                    M += 1
                while S < 61:
                    angle_s = S * 2 * math.pi / 60 - 1 / 2 * math.pi
                    print('s is {0}'.format(S))

                    if S == 60:
                        S = 0
                        print('s is 60')
                        break
                    else:
                        S += 1

                    stddraw.clear()
                    stddraw.circle(0, 0, R)
                    for i in range(1, 13):
                        stddraw.text(Hour_X[i - 1], Hour_Y[i - 1], str(i))
                    stddraw.text(0, 0.85, text)

                    stddraw.setPenRadius(0.02)
                    stddraw.line(0, 0, Hour_L * math.cos(-angle_H),
                                 Hour_L * math.sin(-angle_H))
                    stddraw.setPenRadius(0.01)
                    stddraw.line(0, 0, Min_L * math.cos(-angle_M),
                                 Min_L * math.sin(-angle_M))
                    stddraw.setPenRadius(0.005)

                    stddraw.line(0, 0, Sec_L * math.cos(-angle_s),
                                 Sec_L * math.sin(-angle_s))
                    stddraw.show(1000)
Ejemplo n.º 17
0
import math
from stdpackage import stdarray
from stdpackage import stdaudio
from stdpackage import stdio
from stdpackage import stddraw

SPS = 44100
CONCERT_A = 440.0

stddraw.setXscale(0,50)
stddraw.setYscale(-20,20)
height = 1
while not stdio.isEmpty():

    pitch = stdio.readInt()
    duration = stdio.readFloat()
    hz = CONCERT_A*(2**(pitch/12.0))
    n = int(SPS*duration)
    samples = stdarray.create1D(n+1,0.0)
    for i in range(n+1):
        samples[i] = math.sin(2.0*math.pi*i*hz/SPS)
    stdaudio.playSamples(samples)


    for i in range(0,n-100,100):
        stddraw.line(i/500,samples[i]+20-height,(i+100)/500,samples[i+100]+20-height)
    stddraw.show(duration*1000)
    height +=2

stdaudio.wait()
Ejemplo n.º 18
0
# stddraw.show()

# stddraw.square(.2,.8,.1)
# stddraw.filledSquare(.8,.8,.2)
# stddraw.circle(.8,.2,.2)
# xd=[.1,.2,.3,.2]
# yd=[.2,.3,.2,.1]
# stddraw.filledPolygon(xd,yd)
# stddraw.text(.2,.5,'black text')
# stddraw.setPenColor(stddraw.WHITE)
# stddraw.text(.8,.8,'white text')
# stddraw.show()

from miaozaiye.stdpackage1 import stdarray

n = 2000

x = stdarray.create1D(n + 1, 0.0)
y = stdarray.create1D(n + 1, 0.0)

for i in range(n + 1):
    x[i] = math.pi * i / n
    y[i] = math.sin(4.0 * x[i]) + math.sin(20.0 * x[i])

stddraw.setXscale(0, math.pi)
stddraw.setYscale(-2.0, +2.0)
for i in range(n):
    stddraw.line(x[i], y[i], x[i + 1], y[i + 1])

stddraw.show()
Ejemplo n.º 19
0
import math
from unittest.mock import _ANY

from stdpackage import stddraw

n = 7
a = 0.7
b = 0.5
c = 0.65
A = 30
B = 5
C = -25
r = 5

stddraw.setXscale(-10, 20)
stddraw.setYscale(0, 30)


def recursive_tree(n, A0, r, x0, y0):
    if n == 0:

        return
    xa = x0 + r * math.cos((A0 + A) / 360 * 2 * math.pi)
    ya = y0 + r * math.sin((A0 + A) / 360 * 2 * math.pi)
    stddraw.line(x0, y0, xa, ya)
    stddraw.show(50)
    recursive_tree(n - 1, A0 + A, r * a, xa, ya)

    xb = x0 + r * math.cos((A0 + B) / 360 * 2 * math.pi)
    yb = y0 + r * math.sin((A0 + B) / 360 * 2 * math.pi)
    stddraw.line(x0, y0, xb, yb)
Ejemplo n.º 20
0
from stdpackage import stddraw

stddraw.setXscale(0,10)
stddraw.setYscale(0,10)

stddraw.line(0,0,10,10)
stddraw.show()
Ejemplo n.º 21
0
servicegroup = RandomQueue()

for i in range(M):
    servicegroup.enqueue(list())

Length = stdarray.create1D(M,0)
for i in range(N):
    best = servicegroup.sample()
    print("object {0}".format(i))
    for randomtry in range(T-1):
        queue = servicegroup.sample()
        print('find random queue:',queue)
        if len(best) > len(queue):
            best = queue
    best.append(i)

    a = 0
    for list in servicegroup.queue():
        Length[a]=len(list)
        a+=1
    stddraw.clear()
    stddraw.setYscale(-1,70)
    stdstats.plotBars(Length)
    stddraw.show(100)

print('end')
for list in servicegroup.queue():
    print(list)


Ejemplo n.º 22
0
def main(n):
    life = stdarray.create2D(10, 10, 0)
    stddraw.setXscale(-1, 10)
    stddraw.setYscale(-1, 10)
    life = initiate(life, n)
    lifegame(life)
Ejemplo n.º 23
0
 def draw(self):
     stddraw.setYscale(0, max(self._freq))
     stdstats.plotBars(self._freq)
Ejemplo n.º 24
0
#两个刚性小球在指定范围内碰撞,1个有轨迹,1个没有

from stdpackage import stddraw

stddraw.setXscale(-1.0, 1.0)
stddraw.setYscale(-1.0, 1.0)

DT = 20
RADIUS = 0.1
r1x = 0.480
r1y = 0.860
v1x = 0.015
v1y = 0.023
position1 = [(r1x, r1y)]
r2x = 0.080
r2y = -0.360
v2x = 0.03
v2y = 0.02
position2 = [(r2x, r2y)]
changepoint = []
while True:
    if (abs(r1x + v1x - r2x - v2x) < 2 * RADIUS
            and abs(r1y + v1y - r2y - v2y) < 2 * RADIUS):

        print(r1x, v1x, r2x, v2x, 'change')
        a = v1x
        v1x = v2x
        v2x = a
        changepoint.append((r1x, r1y, r2x, r2y))
        # stddraw.line(r1x,r1y,r2x,r2y)
        # stddraw.show()
Ejemplo n.º 25
0
# n = int(sys.argv[1])
# trials = int(sys.argv[2])

n = 20
trials = 10000
p = 0.1
stddraw.setCanvasSize(1000, 400)

for n in range(20, 1000):

    stddraw.clear()
    freq = stdarray.create1D(n + 1, 0)

    for t in range(trials):
        heads = stdrandom.binomial(n, 0.7)
        freq[heads] += 1

    norm = stdarray.create1D(n + 1, 0)
    for i in range(n + 1):
        norm[i] = 1.0 * freq[i] / trials

    phi = stdarray.create1D(n + 1, 0.0)
    stddev = math.sqrt(n) / 2.0

    for i in range(n + 1):
        phi[i] = gaussian.pdf(i, n / 2.0, stddev)

    stddraw.setYscale(0, 1.1 * max(max(norm), max(phi)))
    stdstats.plotBars(norm)
    stdstats.plotLines(phi)
    stddraw.show(20)
Ejemplo n.º 26
0
#从命令行获取整数m, 从标准输入获取最近m个浮点数,并用动画展示出来

from stdpackage import stdarray, stddraw, stdio, stdstats
import sys

m = int(sys.argv[1])
list = []

stddraw.setCanvasSize(500, 500)
stddraw.setYscale(-1, 1)
for i in range(m):

    stddraw.clear()
    list.append([stdio.readFloat(), str(i)])
    print(list)
    stdstats.plotBars(list, text=True)
    stddraw.show(200)

stddraw.show()