def drawAbstractLine(svg): W, H = svg.get_size() # cx, cy = W // 2, H // 2 N = 10 # svg.draw(draw_rect(0,0,W,H,color='#808B96')) # background slope = 1.7 # ptYaxis = getLinePointFromSlope(slope,(20,0)) # print('ptYaxis=',ptYaxis) pts1 = random_points((N, ), min=0, max=W).reshape((N, 1)) pts1 = np.append(pts1, np.zeros_like(pts1), axis=1) pts2 = None for i in pts1: pt = getLinePointFromSlope(slope, (i[0], i[1])) pt = np.array(pt).reshape(1, 2) # print('pt=',pt) # pts2 = np.append(pts2, pt, axis=1) pts2 = np.concatenate((pts2, pt), axis=0) if pts2 is not None else pt # print('pts1=',pts1) # print('pts2=',pts2) linePoints = [] widths = [] for pt1, pt2 in zip(pts1, pts2): linePoints.append((pt1[0], pt1[1], pt2[0], pt2[1])) widths.append(random.choice([2, 2, 4, 6, 8, 10])) # print(widths) drawlinePoints(svg, linePoints, color=None, stroke_widths=widths)
def drawLineGrapic2(svg): W, H = svg.get_size() len = 0 offsetX = W / 4 offsetY = 0 yInter = 0.5 wStep = 0.5 pts = [] while (offsetY < H / 2): offsetY = offsetY + yInter if offsetY < H / 4: len = len + wStep else: len = len - wStep if (len < 0): break pts.append((offsetX - len, offsetY, offsetX + len, offsetY)) offsetX = W * 3 / 4 offsetY = 0 len = 0 while (offsetY < H / 2): offsetY = offsetY + yInter if offsetY < H / 4: len = len + wStep else: len = len - wStep if (len < 0): break pts.append((offsetX - len, offsetY, offsetX + len, offsetY)) offsetX = W / 4 offsetY = H / 2 len = 0 while (offsetY < H): offsetY = offsetY + yInter if offsetY < H * 3 / 4: len = len + wStep else: len = len - wStep if (len < 0): break pts.append((offsetX - len, offsetY, offsetX + len, offsetY)) offsetX = W * 3 / 4 offsetY = H / 2 len = 0 while (offsetY < H): offsetY = offsetY + yInter if offsetY < H * 3 / 4: len = len + wStep else: len = len - wStep if (len < 0): break pts.append((offsetX - len, offsetY, offsetX + len, offsetY)) drawlinePoints(svg, pts)
def draw_line_param(svg, node, a, b, min_x=0, max_x=100): """draw line y= a*x + b""" def fun(x): return a * x + b # x = np.linspace(min_x, max_x, N) # y = a * x + b # start_pt = [min_x, fun(min_x)] # stop_pt = [max_x, fun(max_x)] pts = [[min_x, fun(min_x), max_x, fun(max_x)]] drawlinePoints(svg, pts, node)
def anim10(svg): """moving ball bounce with a line""" H, W = svg.get_size() cx, cy = W // 2, H // 2 g = svg.draw(draw_tag('g')) svg.set_node(g, 'opacity', '1.0') wall_line = [2, -1.8, 1] # line parameters pts = draw_line_param_abc(wall_line, 0, W, 0, H) drawlinePoints(svg, pts, g, color='black') for i in range(50): pt = random_point(4, W - 5) # print(pt, type(pt)) vx = random.randint(1, 5) * [-1, 1][random.randrange(2)] vy = random.randint(1, 5) * [-1, 1][random.randrange(2)] res = get_math_bounce_parameter(wall_line, pt, vx, vy) perpend_pt, reflect_pt, inter_pt, path_line, reflect_line = res coords = [pt, inter_pt] if reflect_pt is not None: min_x, max_x = 0, W # min_y, max_y = 0, H far_pt = [0, 0] if reflect_pt[0] < inter_pt[0]: min_x = inter_pt[0] far_pt[0] = max_x far_pt[1] = get_line_ABC_y(reflect_line, far_pt[0]) else: max_x = inter_pt[0] far_pt[0] = min_x far_pt[1] = get_line_ABC_y(reflect_line, far_pt[0]) coords.append(far_pt) coords = np.asarray(coords) path = get_points_path(coords, False) draw_circle_path_anim(svg, g, path, radius=3, duration=len(coords) * 1.0, color='red')
def drawSoundGrapic(svg, data): W, H = svg.get_size() cx, cy = W // 2, H // 2 data = H // 2 - data // 400 # shrink sound value to a range that svg can show print('data=', data, np.min(data), np.max(data)) pts = [] # print('pts=', pts) xScale = 55 if 0: # style 1 for i in range(len(data) - 1): pts.append((i / xScale, data[i], (i + 1) / xScale, data[i + 1])) drawlinePoints(svg, pts) elif 1: # style 2, rainbow color totlal = len(data) if 1: for i in range(totlal): pts.append((i / xScale, data[i])) else: R = 120 data = data - H // 2 maxV = np.max(data) for i in range(totlal): angle = i * 2 * np.pi / totlal r = R * math.fabs(data[i]) / maxV x, y = cx + r * math.cos(angle), cy + r * math.sin(angle) # print("x, y=", x, y) # x, y = translation_pts_xy(x, y, (cx,cy)) pts.append((x, y)) # print('pts=', pts) if 0: # rainbowcolor and draw line colors = [] c = rainbow_colors(N=W) for pt in pts: x, y = pt colors.append(c[int(x) % W]) drawlinePointsContinusRainbow(svg, pts, colors=colors) else: # draw path drawPathContinuPoints(svg, pts, strokeWidth=0.2)
def drawLineGrapic6(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 N = 18 xs, ys = [], [] r1 = 50 r2 = 80 for i in range(N + 1): theta = i * 2 * np.pi / N x = r1 * np.cos(theta) y = r1 * np.sin(theta) xs.append(x) ys.append(y) xs, ys = translation_pts_xy(xs, ys, (cx, cy)) pts = np.vstack((xs, ys)).T linePts = [] for i in pts: linePts.append((cx, cy, i[0], i[1])) drawlinePoints(svg, linePts, color='black') drawPointsCircle(svg, pts, r=5, color='#000000') xs, ys = [], [] for i in range(N + 1): theta = i * 2 * np.pi / N + np.pi / N x = r2 * np.cos(theta) y = r2 * np.sin(theta) xs.append(x) ys.append(y) xs, ys = translation_pts_xy(xs, ys, (cx, cy)) pts = np.vstack((xs, ys)).T linePts = [] for i in pts: linePts.append((cx, cy, i[0], i[1])) drawlinePoints(svg, linePts, color='black') # drawPointsCircle(svg, pts, r=5, color='#808B96') drawPointsCircleFadeColor(svg, pts, r=5)
def drawLineGrapic3(svg): W, H = svg.get_size() cx, cy = W // 2, H // 2 length = 160 pt1 = (-1 * length / 2, -1 * length / 2 * np.tan(np.pi / 6)) pt2 = (length / 2, -1 * length / 2 * np.tan(np.pi / 6)) pt3 = (0, (length / 2) / np.cos(np.pi / 6)) x = [pt1[0], pt2[0], pt3[0]] y = [pt1[1], pt2[1], pt3[1]] x, y = translation_pts_xy(x, y, (cx, cy)) pt1 = (x[0], y[0]) pt2 = (x[1], y[1]) pt3 = (x[2], y[2]) N = 20 pts = [] s, b = get_line(pt2, pt3) for i in range(N + 1): xn = pt3[0] + np.abs(pt2[0] - pt3[0]) * i / N yn = s * xn + b pts.append((pt1[0], pt1[1], xn, yn)) s, b = get_line(pt1, pt3) for i in range(N + 1): xn = pt1[0] + np.abs(pt1[0] - pt3[0]) * i / N yn = s * xn + b pts.append((pt2[0], pt2[1], xn, yn)) s, b = get_line(pt1, pt2) for i in range(N + 1): xn = pt1[0] + np.abs(pt1[0] - pt2[0]) * i / N yn = s * xn + b pts.append((pt3[0], pt3[1], xn, yn)) drawlinePoints(svg, pts, color='black')
def drawLineGrapic(svg): W, H = svg.get_size() times = 100 len = 0 offsetX = W // 2 offsetY = 0 pts = [] for _ in range(times): len = len + 0.5 offsetY = offsetY + 0.5 x1 = offsetX - len y1 = offsetY x2 = offsetX + len y2 = y1 pts.append((x1, y1, x2, y2)) for _ in range(times): len = len - 0.5 offsetY = offsetY + 0.5 pts.append((offsetX - len, offsetY, offsetX + len, offsetY)) drawlinePoints(svg, pts)
def anim9(svg): """moving ball bounce with a line""" H, W = svg.get_size() cx, cy = W // 2, H // 2 g = svg.draw(draw_tag('g')) svg.set_node(g, 'opacity', '1.0') wall_line = [2, -1.8, 1] # line parameters pt = random_point(4, W - 5) print(pt, type(pt)) vx = -2 vy = 1 res = get_math_bounce_parameter(wall_line, pt, vx, vy) perpend_pt, reflect_pt, inter_pt, path_line, reflect_line = res # draw_line_param(svg, g, a, b, 0, W) pts = draw_line_param_abc(wall_line, 0, W, 0, H) drawlinePoints(svg, pts, g, color='black') svg.draw_node(g, draw_circle(pt[0], pt[1], 1, color='black')) svg.draw_node(g, draw_circle(perpend_pt[0], perpend_pt[1], 1, color='red')) svg.draw_node(g, draw_circle(reflect_pt[0], reflect_pt[1], 1, color='red')) svg.draw_node(g, draw_circle(inter_pt[0], inter_pt[1], 1, color='black')) svg.draw_node( g, draw_line(pt[0], pt[1], reflect_pt[0], reflect_pt[1], stroke_dasharray="2")) svg.draw_node( g, draw_line(inter_pt[0], inter_pt[1], reflect_pt[0], reflect_pt[1], stroke_dasharray="2")) if inter_pt is not None: min_x = min(pt[0], inter_pt[0]) max_x = max(pt[0], inter_pt[0]) min_y = min(pt[1], inter_pt[1]) max_y = min(pt[1], inter_pt[1]) pts = draw_line_param_abc(path_line, min_x, max_x, min_y, max_y) drawlinePoints(svg, pts, g, color='black') coords = [pt, inter_pt] if reflect_pt is not None: min_x, max_x = 0, W min_y, max_y = 0, H far_pt = [0, 0] if reflect_pt[0] < inter_pt[0]: min_x = inter_pt[0] far_pt[0] = max_x far_pt[1] = get_line_ABC_y(reflect_line, far_pt[0]) else: max_x = inter_pt[0] far_pt[0] = min_x far_pt[1] = get_line_ABC_y(reflect_line, far_pt[0]) pts = draw_line_param_abc(reflect_line, min_x, max_x, min_y, max_y) drawlinePoints(svg, pts, g, color='black') coords.append(far_pt) coords = np.asarray(coords) print('coords=', coords, coords.shape) path = get_points_path(coords, False) draw_circle_path_anim(svg, g, path, radius=6, duration=len(coords) * 1.0, color='red')