science_action
w
import random import pprint import math import matplotlib.pyplot as plt def gen_random(magnify_=1000, round_=2): dd = round(random.random() * magnify_, round_) return dd def float_format(d, not_float=0.0): if d is float: print(232) else: try: d = float(d) except Exception: d = not_float return d def gen_atob_dis(a, b): a, b = float_format(a), float_format(b) return ((a ** 2 + b ** 2) * 0.5) ** 0.5 def chk_triangle(a, b, c): a, b, c = float_format(a), float_format(b), float_format(c) if a + b > c and a + c > b and b + c > a: return 1 else: return 0 def compute_triangle_angle(a, b, c): a, b, c = float_format(a), float_format(b), float_format(c) cos = (a ** 2 + b ** 2 - c ** 2) / (2 * a * b) return math.acos(cos) def radius_coordinates_to_perpendicular_coordinates(dic_): polar_radius, polar_angle = dic_['polar_radius'], dic_['polar_angle'] res_ = {} res_['y'], res_['x'] = polar_radius * math.sin(polar_angle), polar_radius * math.cos(polar_angle) return res_ PI = math.pi uuid_num = 5 uuid_num_plus = uuid_num + 1 str_ = '-86 -83 -71 -69 -73 -60 -72 -84 -82 -56 -82 -75 -71 -89 -83 -79 -67 -87 -77 -79' l_row = str_.split('\t\t\t') rssi_matrix = {} for i in range(0, uuid_num, 1): i_ = i + 1 rssi_matrix[i_] = {} l_one = l_row[i].split('\t') step_ = 0 for ii in range(0, uuid_num, 1): ii_ = ii + 1 if i == ii: rssi_matrix[i_][ii_] = 0 else: rssi_matrix[i_][ii_] = l_one[step_] step_ += 1 rssi_distance_matrix = {} for i in range(0, uuid_num, 1): i_ = i + 1 rssi_distance_matrix[i_] = {} for ii in range(0, uuid_num, 1): ii_ = ii + 1 a = rssi_matrix[i_][ii_] b = rssi_matrix[ii_][i_] rssi_distance_matrix[i_][ii_] = gen_atob_dis(a, b) pprint.pprint(str_) pprint.pprint(rssi_matrix) pprint.pprint(rssi_distance_matrix) triangle_dic = {} triangle_side_list = [] for i in range(1, uuid_num_plus, 1): for ii in range(1, uuid_num_plus, 1): for iii in range(1, uuid_num_plus, 1): if i != ii and i != iii and ii != iii: triangle_id = '%s%s%s' % (i, ii, iii) chk_unique_triangle_id_list = sorted(list(triangle_id)) if chk_unique_triangle_id_list in triangle_side_list: continue triangle_side_list.append(chk_unique_triangle_id_list) triangle_dic[triangle_id] = {} triangle_dic[triangle_id]['length_of_side_dic'] = [] triangle_dic[triangle_id]['is_triangle'] = 0 c = rssi_distance_matrix[i][ii] b = rssi_distance_matrix[i][iii] a = rssi_distance_matrix[ii][iii] is_triangle = chk_triangle(a, b, c) triangle_dic[triangle_id]['is_triangle'] = is_triangle if is_triangle == 1: triangle_dic[triangle_id]['length_of_side_dic'] = {} triangle_dic[triangle_id]['angle_of_side_dic'] = {} k12 = '%s%s' % (i, ii) k13 = '%s%s' % (i, iii) k23 = '%s%s' % (ii, iii) triangle_dic[triangle_id]['length_of_side_dic'][k12] = a triangle_dic[triangle_id]['length_of_side_dic'][k13] = b triangle_dic[triangle_id]['length_of_side_dic'][k23] = c # filter_ triangle rebuild radius_coordinates = {} radius_coordinates[1] = {} radius_coordinates[1]['polar_angle'] = 0 radius_coordinates[1]['polar_radius'] = 0 radius_coordinates[2] = {} radius_coordinates[2]['polar_radius'] = rssi_distance_matrix[1][2] radius_coordinates[2]['polar_angle'] = 0 radius_coordinates[3] = {} radius_coordinates[3]['polar_radius'] = rssi_distance_matrix[1][3] a = rssi_distance_matrix[1][2] b = rssi_distance_matrix[1][3] c = rssi_distance_matrix[2][3] radius_coordinates[3]['polar_angle'] = abs(compute_triangle_angle(a, b, c)) for i in range(4, uuid_num_plus, 1): radius_coordinates[i] = {} radius_coordinates[i]['polar_radius'] = 0 radius_coordinates[i]['polar_angle'] = 0 polar_radius_i = rssi_distance_matrix[1][i] a = rssi_distance_matrix[1][2] c = rssi_distance_matrix[2][i] angle_radius_from12 = compute_triangle_angle(a, polar_radius_i, c) a = rssi_distance_matrix[1][3] c = rssi_distance_matrix[3][i] angle_radius_from13 = compute_triangle_angle(a, polar_radius_i, c) polar_angle_i = angle_radius_from12 if abs(angle_radius_from12) + radius_coordinates[3]['polar_angle'] == angle_radius_from13: polar_angle_i = 2 * PI - angle_radius_from12 radius_coordinates[i]['polar_radius'] = polar_radius_i radius_coordinates[i]['polar_angle'] = polar_angle_i perpendicular_coordinates = {} for i in range(1, uuid_num_plus, 1): dic_ = radius_coordinates[i] res_ = radius_coordinates_to_perpendicular_coordinates(dic_) perpendicular_coordinates[i] = {} perpendicular_coordinates[i]['x'], perpendicular_coordinates[i]['y'] = res_['x'], res_['y'] x = [perpendicular_coordinates[i]['x'] for i in range(1, uuid_num_plus, 1)] y = [perpendicular_coordinates[i]['y'] for i in range(1, uuid_num_plus, 1)] for i in range(1, 4, 1): pprint.pprint(perpendicular_coordinates[i]) plt.plot(x, y) plt.show()