### homework 4 ### Domen Jug ### 20241107 ############### import rhinoscriptsyntax as rs import math import random rs.UnitSystem(3) rs.ShowGrid(view=None, show=0) rs.ShowGridAxes(view=None, show=0) rs.ViewDisplayMode(view=None, mode="Wireframe") rs.EnableRedraw(0) rs.DeleteObjects(rs.AllObjects()) import rhinoscriptsyntax as rs import math import random def create_sphere_with_points_and_cubes(radius=10, num_points=50, cube_size=2, distance_from_sphere=2, num_curves=100): sphere = rs.AddSphere([0, 0, 0], radius) if not sphere: print("Failed to create the sphere.") return rs.ObjectColor(sphere, [255, 0, 0]) # red colour points_on_sphere = [] for i in range(num_points): theta = random.uniform(0, math.pi) phi = random.uniform(0, 2 * math.pi) x = radius * math.sin(theta) * math.cos(phi) y = radius * math.sin(theta) * math.sin(phi) z = radius * math.cos(theta) points_on_sphere.append([x, y, z]) cube_positions = [ [radius + distance_from_sphere, 0, 0], [-radius - distance_from_sphere, 0, 0], [0, radius + distance_from_sphere, 0], [0, -radius - distance_from_sphere, 0], [0, 0, radius + distance_from_sphere], [0, 0, -radius - distance_from_sphere], ] cubes = [] for idx, pos in enumerate(cube_positions): cube = rs.AddBox([ [pos[0] - cube_size/2, pos[1] - cube_size/2, pos[2] - cube_size/2], [pos[0] + cube_size/2, pos[1] - cube_size/2, pos[2] - cube_size/2], [pos[0] + cube_size/2, pos[1] + cube_size/2, pos[2] - cube_size/2], [pos[0] - cube_size/2, pos[1] + cube_size/2, pos[2] - cube_size/2], [pos[0] - cube_size/2, pos[1] - cube_size/2, pos[2] + cube_size/2], [pos[0] + cube_size/2, pos[1] - cube_size/2, pos[2] + cube_size/2], [pos[0] + cube_size/2, pos[1] + cube_size/2, pos[2] + cube_size/2], [pos[0] - cube_size/2, pos[1] + cube_size/2, pos[2] + cube_size/2] ]) cubes.append(cube) color = [random.randint(0, 255) for _ in range(3)] # random colour rs.ObjectColor(cube, color) for cube in cubes: bbox = rs.BoundingBox(cube) if not bbox: continue cube_center = [(bbox[0][0] + bbox[6][0]) / 2, (bbox[0][1] + bbox[6][1]) / 2, (bbox[0][2] + bbox[6][2]) / 2] vector_to_sphere = [0 - cube_center[0], 0 - cube_center[1], 0 - cube_center[2]] face_normals = { "positive_x": [1, 0, 0], "negative_x": [-1, 0, 0], "positive_y": [0, 1, 0], "negative_y": [0, -1, 0], "positive_z": [0, 0, 1], "negative_z": [0, 0, -1] } cube_faces = { "positive_x": [bbox[1], bbox[2], bbox[6], bbox[5]], "negative_x": [bbox[0], bbox[3], bbox[7], bbox[4]], "positive_y": [bbox[1], bbox[5], bbox[7], bbox[3]], "negative_y": [bbox[0], bbox[4], bbox[6], bbox[2]], "positive_z": [bbox[4], bbox[5], bbox[1], bbox[0]], "negative_z": [bbox[6], bbox[7], bbox[3], bbox[2]] } facing_faces = [] for face, normal in face_normals.items(): dot_product = sum([vector_to_sphere[i] * normal[i] for i in range(3)]) if dot_product > 0: facing_faces.append(cube_faces[face]) for i in range(num_curves): rand_point = random.choice(points_on_sphere) if facing_faces: selected_face = random.choice(facing_faces) p1 = selected_face[0] p2 = selected_face[1] p3 = selected_face[2] p4 = selected_face[3] u = random.uniform(0, 1) v = random.uniform(0, 1) interp_point = [ p1[0] * (1 - u) * (1 - v) + p2[0] * u * (1 - v) + p3[0] * u * v + p4[0] * (1 - u) * v, p1[1] * (1 - u) * (1 - v) + p2[1] * u * (1 - v) + p3[1] * u * v + p4[1] * (1 - u) * v, p1[2] * (1 - u) * (1 - v) + p2[2] * u * (1 - v) + p3[2] * u * v + p4[2] * (1 - u) * v ] point_to_sphere_dist = math.sqrt(interp_point[0]**2 + interp_point[1]**2 + interp_point[2]**2) if point_to_sphere_dist < radius: factor = radius / point_to_sphere_dist interp_point = [coord * factor for coord in interp_point] rs.AddCurve([interp_point, rand_point]) print("Created {num_curves} curves.") create_sphere_with_points_and_cubes(radius=1000, num_points=5000, cube_size=2000, distance_from_sphere=2000, num_curves=250)