我创建了一个名为“坐标”的python列表变量。坐标列表由图中随机点的X轴和Y轴值对组成。一对中的第一个值表示X轴值,第二个值表示Y轴值。坐标列表变量如下所示。coordinates = [(95, 171), (54, 131), (125, 131), (213, 78)]
我实现了一个名为'direction_finder'的函数,通过使用坐标列表变量中的X轴值和Y轴值来查找基本方向。
函数的实现步骤如下
1.访问坐标列表变量中每对中的X轴值。x1 = coordinates[0][0]
x2 = coordinates[1][0]
x3 = coordinates[2][0]
x4 = coordinates[3][0]
2.访问坐标列表变量中每对Y轴值。y1 = coordinates[0][1]
y2 = coordinates[1][1]
y3 = coordinates[2][1]
y4 = coordinates[3][1]
3.取X轴和Y轴值的度数的值。degrees_x1y1 = math.atan2(x1,y1)/math.pi* 180
degrees_x2y2 = math.atan2(x2,y2)/math.pi* 180
degrees_x3y3 = math.atan2(x3,y3)/math.pi* 180
degrees_x4y4 = math.atan2(x4,y4)/math.pi* 180
4,加一个if条件,取最终度值。if degrees_x1y1 < 0:
degrees_final_x1y1 = 360 + degrees_x1y1
else:
degrees_final_x1y1 = degrees_x1y1
if degrees_x2y2 < 0:
degrees_final_x2y2 = 360 + degrees_x2y2
else:
degrees_final_x2y2 = degrees_x2y2
if degrees_x3y3 < 0:
degrees_final_x3y3 = 360 + degrees_x3y3
else:
degrees_final_x3y3 = degrees_x3y3
if degrees_x4y4 < 0:
degrees_final_x4y4 = 360 + degrees_x4y4
else:
degrees_final_x4y4 = degrees_x4y4
5.为基数方向创建数组。direction_brackets = ["NORTH", "NORTH-EAST","EAST","SOUTH-EAST","SOUTH","SOUTH-WEST","WEST","NORTH-WEST"]
6.对最终度值进行四舍五入round_x1y1 = round(degrees_final_x1y1/45)
round_x2y2 = round(degrees_final_x2y2/45)
round_x3y3 = round(degrees_final_x3y3/45)
round_x4y4 = round(degrees_final_x4y4/45)
7.获取最终度值的最终基本方向final_direction_x1y1 = direction_brackets[round_x1y1]
final_direction_x2y2 = direction_brackets[round_x2y2]
final_direction_x3y3 = direction_brackets[round_x3y3]
final_direction_x4y4 = direction_brackets[round_x4y4]
8.返回最终的基数方向值return final_direction_x1y1,final_direction_x2y2, final_direction_x3y3, final_direction_x4y4
9.创建一个名为directions_list的列表变量,以收集direction_finder函数返回的最终基本方向值。direction_list = []
direction_list= direction_lookup(coordinates)
10.从“direction_list”变量中获取最终的基本方向。direction_x1y1 = direction_list[0]
direction_x2y2 = direction_list[1]
direction_x3y3 = direction_list[2]
direction_x4y4 = direction_list[3]
在实现代码之后,我必须创建数据库连接来在PHPMyAdmin数据库中存储最终的基本方向,我创建了一个数据库和一个单独的表来存储最终的基本方向。这些STPE的代码如下所示。connection = pymysql.connect(host="localhost", user="root", passwd="", database="directions")
cursor = connection.cursor()
directionsCollect= """Create Table directions_store(
id Int(11) Primary Key Auto_Increment,
cardinal_directions Varchar(255),
)"""
cursor.execute(directionsCollect)
之后,我实现了insert命令,向创建的表插入一个方向。record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
'cardinal_directions': direction_x1y1
}
cursor.execute(record_x1y1)
#commit the connection
connection.commit()
#close the connection
connection.close()
但问题是插入没有发生。错误如下所示。Traceback (most recent call last):
File "floor_plan_detector.py", line 320, in
cursor.execute(record_x1y1)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 762, in _execute_command
packet = prelude + sql[:packet_size-1]
TypeError: can't concat tuple to bytes
错误表示类型错误。有人能解释一下这里出了什么问题吗?。