-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
101 lines (73 loc) · 2.16 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import cairo, functions, time
# Set up surface
surface = cairo.ImageSurface (cairo.FORMAT_RGB24, 600, 400)
ctx = cairo.Context(surface)
ctx.set_source_rgb(0.8, 0.8, 0.8)
ctx.paint()
# Draw the rectangle in the center
rect_coordinates = {'x': 200, 'y': 200, 'width': 100, 'height': 100}
functions.draw_rectangle(ctx, rect_coordinates)
values = {'r': 0, 'g': 1, 'b': 0}
functions.set_shape_color(ctx, values)
ctx.fill()
surface.write_to_png('img/test_dict.png')
# bouncing sphere
# Create an image surface
width, height = 600, 400
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
# Initial sphere parameters
radius = 50
x, y = width // 2, height // 2
speed = 2
direction = [1, 1] # x and y direction
# Gradient colors
color1 = (1, 0, 0)
color2 = (0, 0, 1)
# Number of frames
num_frames = 10
for frame in range(num_frames):
# Clear the canvas
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
# Update sphere position
x += speed * direction[0]
y += speed * direction[1]
# Bounce off the walls
if x - radius <= 0 or x + radius >= width:
direction[0] *= -1
if y - radius <= 0 or y + radius >= height:
direction[1] *= -1
# Draw the sphere
functions.draw_sphere(ctx, x, y, radius, color1, color2)
# Save the frame
surface.write_to_png(f'{functions.IMAGE_PATH}/bounce_frame_{frame}.png')
# Pause for a short time to control the animation speed
time.sleep(0.05)
# ctx.move_to()
# ctx.restore()
# offset=150
# ctx.move_to(350, 200+offset)
# ctx.line_to(400, 150+offset)
# ctx.line_to(450, 200+offset)
# ctx.line_to(400, 250+offset)
# ctx.save()
#circumscribe triangle
# Draw the circle in the center using the arc method
# r=150
# # functions.draw_circle(ctx,250, 250, 150, 2,0,0,0)
# # ctx.fill()
# #draw line from center to top of circle
# x1,y1=50,50
# angle_in_radians=5/12
# x2 = x1 + r * math.cos(angle_in_radians)
# y2 = y1 + r * math.sin(angle_in_radians)
# ctx.move_to(x1,y1)
# ctx.line_to(x2,y2)
# ctx.set_source_rgb(.33,.67,0)
# ctx.stroke()
#draw an arrow
# functions.draw_arrowhead(ctx, 0,0,500,500,300,150,.9,.8,.7)
# ctx.stroke()