-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_sdf.py
64 lines (44 loc) · 1.91 KB
/
circle_sdf.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
import py5
def setup():
# Set the window size to 400x400 pixels
py5.size(400, 400)
def draw():
# Clear the screen with white color
py5.background(255)
# Translate the drawing origin to the center of the window
py5.translate(py5.width / 2, py5.height / 2)
# Draw a circle with center at (0, 0) and radius 80
py5.stroke(0) # Set the stroke color to black
fill_circle(0, 0, 80) # Draw the circle using the `fill_circle` function
# Get the mouse coordinates relative to the center of the window
mx = py5.mouse_x - py5.width / 2
my = py5.mouse_y - py5.height / 2
# Compute the signed distance function value for the mouse position
sdf_value = signed_distance_function(mx, my, 0, 0, 80)
# Map the signed distance function value to a color
c = py5.remap(sdf_value, -1, 1, 0, 255)
# Display the signed distance function value
py5.fill(0, c, 255 - c)
py5.text_size(16)
py5.text(
"SDF Value: {:.2f}".format(sdf_value), -py5.width / 2 + 10, py5.height / 2 - 20
)
def fill_circle(x, y, r):
# Create a circle with center at (x, y) and radius r
num_points = 100 # Number of points used to draw the circle
py5.begin_shape()
# Generate a set of points evenly spaced around the circle
for i in range(num_points + 1):
# Calculate the angle for each point
theta = py5.remap(i, 0, num_points, 0, py5.TWO_PI)
# Calculate the corresponding x and y coordinates for the point
px = x + r * py5.cos(theta)
py = y + r * py5.sin(theta)
# Add the point to the shape
py5.vertex(px, py)
py5.end_shape(py5.CLOSE)
def signed_distance_function(x, y, center_x, center_y, radius):
"""Calculate the signed distance function for a circle centered at (center_x, center_y) with radius 'radius'."""
distance = py5.dist(x, y, center_x, center_y)
return distance - radius
py5.run_sketch()