poisson-disk-sampling: Fix bug, add rect-radius file

This commit is contained in:
Joseph Ferano 2024-08-15 07:43:24 +07:00
parent c2309066e3
commit 80e4179448
2 changed files with 81 additions and 1 deletions

View File

@ -51,7 +51,7 @@ def player_input(w: World):
w.points.append(p) w.points.append(p)
idx = int(p.y // cell_size * cols + p.x // cell_size) idx = int(p.y // cell_size * cols + p.x // cell_size)
if idx < len(w.grid): if idx < len(w.grid):
w.grid[idx] = 1 w.grid[idx] = p
if RL.is_key_pressed(RL.KEY_SPACE): if RL.is_key_pressed(RL.KEY_SPACE):
w.debug_draw = not w.debug_draw w.debug_draw = not w.debug_draw
if RL.is_key_pressed(RL.KEY_ENTER): if RL.is_key_pressed(RL.KEY_ENTER):

80
rect-radius.py Normal file
View File

@ -0,0 +1,80 @@
import pyray as RL
from pyray import (Rectangle as Rect, Vector2 as Vec2, Vector3 as Vec3, Camera3D, BoundingBox, Color)
import math
import pdb
import random
from typing import Optional, Tuple, List
from dataclasses import dataclass, field
def dump(struct):
s = f"{RL.ffi.typeof(struct)}: (".replace('<ctype ', '').replace('>', '')
for field in dir(struct):
data = struct.__getattribute__(field)
if str(data).startswith("<cdata"):
data = dump(data)
s += f"{field}:{data} "
s += ")"
return s
screen_width = 1024
screen_height = 768
@dataclass
class World:
cam: Camera3D
frame_count: int = 0
def init() -> World:
cam = Camera3D(Vec3(0, 10, 10), Vec3(0, 0, 0), Vec3(0, 1, 0), 45, RL.CAMERA_PERSPECTIVE)
return World(cam)
def player_input(w: World):
pass
def update(w: World):
pass
def draw_2d(w: World):
radius = 100
grid_size = 13
rect_size = radius / math.sqrt(2)
half_size = rect_size // 2
mid_size = 5 * rect_size
mid_x = screen_width // 2 - mid_size // 2
mid_y = screen_height // 2 - mid_size // 2
mid_rect = Rect(mid_x, mid_y, mid_size, mid_size)
RL.draw_rectangle_rec(mid_rect, (Color)(45, 30, 45, 255))
center_rect = Rect(screen_width // 2 - half_size, screen_height // 2 - half_size, rect_size, rect_size)
RL.draw_rectangle_rec(center_rect, (Color)(100, 30, 45, 255))
for row in range(grid_size):
for col in range(grid_size):
x = screen_width // 2 - half_size + (row - grid_size // 2) * rect_size
y = screen_height // 2 - half_size + (col - grid_size // 2) * rect_size
rect = Rect(x, y, rect_size, rect_size)
RL.draw_rectangle_lines_ex(rect, 2, RL.GREEN)
mouse_pos = RL.get_mouse_position()
if mouse_pos.x < screen_width // 2 - half_size: mouse_pos.x = screen_width // 2 - half_size
if mouse_pos.x > screen_width // 2 + half_size: mouse_pos.x = screen_width // 2 + half_size
if mouse_pos.y < screen_height // 2 - half_size: mouse_pos.y = screen_height // 2 - half_size
if mouse_pos.y > screen_height // 2 + half_size: mouse_pos.y = screen_height // 2 + half_size
RL.draw_circle_v(mouse_pos, 5, RL.WHITE)
RL.draw_circle_lines_v(mouse_pos, radius, RL.RED)
RL.init_window(screen_width, screen_height, "Starter");
RL.set_target_fps(60)
w = init()
while not RL.window_should_close():
player_input(w)
update(w)
# Drawing
RL.begin_drawing()
RL.clear_background((Color)(30, 30, 45, 255))
draw_2d(w)
RL.end_drawing()
w.frame_count += 1