############################## import rhinoscriptsyntax as rs import random, time, sys, math ### sys.path.append("P:\WWW\halil2003\DM2") ### add path where "DM_lib.py" can be found !!! sys.path.append("P:\WWW\halil2003\DM2") ### add path where "DM_lib.py" can be found !!! sys.path.append("P:\WWW\halil2003\DM2") ### add path where "DM_lib.py" can be found !!! import DM_lib as dm ### reload(dm) ############################## rs.UnitSystem(3) rs.ShowGrid(view=None, show=0) rs.ShowGridAxes(view=None, show=0) rs.ViewDisplayMode(view=None, mode="Wireframe") rs.EnableRedraw(0) dm.PointRadius(displayModeX=0, rad=3, styl=1) rs.DeleteObjects(rs.AllObjects()) strong_yellow_color = [255, 255, 0] strong_red_color = [255, 0, 0] strong_green_color = [0, 255, 0] strong_blue_color = [0, 0, 255] spehere_1_cen = [10, 10, 10] spehere_2_cen = [200, 10, 10] spehere_3_cen = [105, 200, 10] radius = 30 centroid_x = (spehere_1_cen[0] + spehere_2_cen[0] + spehere_3_cen[0]) / 3 centroid_y = (spehere_1_cen[1] + spehere_2_cen[1] + spehere_3_cen[1]) / 3 centroid_z = 100 centroid_pt = [centroid_x, centroid_y, centroid_z] coords_spehere_1 = [] for distance in range(radius): scalVec = [distance, 0, 0] for i in range(1001): scalVec = rs.VectorRotate( scalVec, random.uniform(-90,90), [random.uniform(-1,1) for i in range(3)]) coords_spehere_1.append( rs.VectorAdd( spehere_1_cen, scalVec ) ) rs.AddPoints( coords_spehere_1 ) coords_spehere_2 = [] for distance in range(radius): scalVec = [distance, 0, 0] for i in range(1001): scalVec = rs.VectorRotate( scalVec, random.uniform(-90,90), [random.uniform(-1,1) for i in range(3)]) coords_spehere_2.append( rs.VectorAdd( spehere_2_cen, scalVec ) ) rs.AddPoints( coords_spehere_2 ) coords_spehere_3 = [] for distance in range(radius): scalVec = [distance, 0, 0] for i in range(1001): scalVec = rs.VectorRotate( scalVec, random.uniform(-90,90), [random.uniform(-1,1) for i in range(3)]) coords_spehere_3.append( rs.VectorAdd( spehere_3_cen, scalVec ) ) rs.AddPoints( coords_spehere_3 ) top_left = [0, 15, 15] top_right = [0, -15, 15] bottom_left = [0, 15, -15] bottom_right = [0, -15, -15] offsets = [top_left, top_right, bottom_left, bottom_right] side_1_pts = [] for offset in offsets: side_1_pt = rs.VectorAdd( spehere_1_cen, offset) side_1_pts.append(side_1_pt) side_2_pts = [] for offset in offsets: side_2_pt = rs.VectorAdd( spehere_2_cen, offset) side_2_pts.append(side_2_pt) side_2_pts_id = rs.AddPoints(side_2_pts) rs.ObjectColor(side_2_pts_id, strong_yellow_color) cube_pts = [] left_x = int(side_1_pts[0][0]) right_x = int(side_2_pts[0][0]) closer_y = int(side_1_pts[1][1]) further_y = int(side_1_pts[0][1]) bottom_z = int(side_1_pts[2][2]) top_z = int(side_1_pts[0][2]) for x in range(left_x, right_x + 1): for y in range(closer_y, further_y + 1): for z in range(bottom_z, top_z + 1): pt = [x, y, z] diff_1 = rs.VectorSubtract(pt, spehere_1_cen) if rs.VectorLength(diff_1) < radius: # if it belongs to the first spehere # then skip it i.e. do not visualize it continue diff_2 = rs.VectorSubtract(pt, spehere_2_cen) if rs.VectorLength(diff_2) < radius: # if it belongs to the second spehere # then skip it i.e. do not visualize it continue cube_pts.append(pt) if z == top_z and y == further_y: line_id = rs.AddLine(centroid_pt, pt) rs.ObjectColor(line_id, strong_red_color) cube_pts_id = rs.AddPoints(cube_pts) rs.ObjectColor(cube_pts_id, strong_yellow_color) top_left = [-15, 0, 15] top_right = [15, 0, 15] bottom_left = [-15, 0, -15] bottom_right = [15, 0, -15] offsets = [top_left, top_right, bottom_left, bottom_right] side_3_pts = [] for offset in offsets: side_3_pt = rs.VectorAdd( spehere_1_cen, offset) side_3_pts.append(side_3_pt) diff_spehere_1_3 = rs.VectorSubtract( spehere_1_cen, spehere_3_cen) len_diff_spehere_1_3 = rs.VectorLength( diff_spehere_1_3 ) sphere_3_cen_temp = rs.VectorAdd( spehere_1_cen, [0, len_diff_spehere_1_3, 0] ) side_4_pts = [] for offset in offsets: side_4_pt = rs.VectorAdd( sphere_3_cen_temp, offset) side_4_pts.append(side_4_pt) left_x = int(side_3_pts[0][0]) right_x = int(side_3_pts[1][0]) print left_x, right_x closer_y = int(side_3_pts[1][1]) further_y = int(side_4_pts[0][1]) print closer_y, further_y bottom_z = int(side_3_pts[2][2]) top_z = int(side_3_pts[0][2]) print bottom_z, top_z k = ( spehere_1_cen[1] - spehere_3_cen[1] ) / ( spehere_1_cen[0] - spehere_3_cen[0] ) shift_angle = 90 - math.degrees(math.atan(k)) print "first shif_angle: " + str(shift_angle) cube_pts = [] for x in range(left_x, right_x + 1): for y in range(closer_y, further_y + 1): for z in range(bottom_z, top_z + 1): pt = [x, y, z] shifted_pt = rs.VectorRotate( pt, shift_angle, [0, 0, -1]) diff_1 = rs.VectorSubtract(shifted_pt, spehere_1_cen) if rs.VectorLength(diff_1) < radius: # if it belongs to the first spehere # then skip it i.e. do not visualize it continue diff_2 = rs.VectorSubtract(shifted_pt, spehere_3_cen) if rs.VectorLength(diff_2) < radius: # if it belongs to the third spehere # then skip it i.e. do not visualize it continue cube_pts.append(shifted_pt) if z == top_z and x == right_x: line_id = rs.AddLine(centroid_pt, shifted_pt) rs.ObjectColor(line_id, strong_blue_color) cube_pts_id = rs.AddPoints(cube_pts) rs.ObjectColor(cube_pts_id, strong_yellow_color) diff_spehere_2_3 = rs.VectorSubtract( spehere_2_cen, spehere_3_cen) len_diff_spehere_2_3 = rs.VectorLength( diff_spehere_2_3 ) sphere_3_cen_temp = rs.VectorAdd( spehere_2_cen, [0, len_diff_spehere_2_3, 0] ) side_5_pts = [] for offset in offsets: side_5_pt = rs.VectorAdd( spehere_2_cen, offset) side_5_pts.append(side_5_pt) side_6_pts = [] for offset in offsets: side_6_pt = rs.VectorAdd( sphere_3_cen_temp, offset) side_6_pts.append(side_6_pt) left_x = int(side_5_pts[0][0]) right_x = int(side_5_pts[1][0]) print left_x, right_x closer_y = int(side_5_pts[1][1]) further_y = int(side_6_pts[0][1]) print closer_y, further_y bottom_z = int(side_5_pts[2][2]) top_z = int(side_5_pts[0][2]) print bottom_z, top_z print spehere_2_cen[1], spehere_3_cen[1], spehere_2_cen[0], spehere_3_cen[0] k = ( spehere_2_cen[1] - spehere_3_cen[1] ) / ( spehere_2_cen[0] - spehere_3_cen[0] ) shift_angle = 180 - (90 - math.degrees(math.atan(k))) shift_angle = math.radians(shift_angle) print "second shift_angle: " + str(shift_angle) cube_pts = [] for x in range(left_x, right_x + 1): for y in range(closer_y, further_y + 1): for z in range(bottom_z, top_z + 1): diff = math.sqrt(math.pow(spehere_2_cen[1] - y, 2)) translated_x = x - spehere_2_cen[0] translated_y = y - spehere_2_cen[1] rotated_x = translated_x * math.cos(shift_angle) - translated_y * math.sin(shift_angle) rotated_y = translated_x * math.sin(shift_angle) + translated_y * math.cos(shift_angle) new_x = rotated_x + spehere_2_cen[0] new_y = rotated_y + spehere_2_cen[1] shifted_pt = [new_x, new_y, z] diff_1 = rs.VectorSubtract(shifted_pt, spehere_2_cen) if rs.VectorLength(diff_1) < radius: # if it belongs to the first spehere # then skip it i.e. do not visualize it continue diff_2 = rs.VectorSubtract(shifted_pt, spehere_3_cen) if rs.VectorLength(diff_2) < radius: # if it belongs to the second spehere # then skip it i.e. do not visualize it continue cube_pts.append(shifted_pt) if z == top_z and x == left_x: line_id = rs.AddLine(centroid_pt, shifted_pt) rs.ObjectColor(line_id, strong_green_color) cube_pts_id = rs.AddPoints(cube_pts) rs.ObjectColor(cube_pts_id, strong_yellow_color) rs.ZoomExtents()