2025-04-15 13:56:54 +02:00

150 lines
5.4 KiB
Python

import math
def generate_angle_coord_list(radius, start_angle, end_angle, step_size, clockwise=True):
# TODO: DOCS
"""Creates a list of discrete cartesian coordinates (x,y), given the radius, start- and end angles, the angle step size, and the direction of rotation.
Function then returns a list of two lists: list of angles and list of cartesian coordinates (x,y coordinates in a tuple).
Args:
radius (_type_): _description_
start_angle (_type_): _description_
end_angle (_type_): _description_
step_size (_type_): _description_
clockwise (bool, optional): _description_. Defaults to True.
Returns:
_type_: _description_
""" """"""
# Initialize lists to hold angles and (x, y) pairs
angles = []
coordinates = []
# Normalize angles to the range [0, 360)
start_angle = start_angle % 360
end_angle = end_angle % 360
if not clockwise:
# Clockwise rotation
current_angle = start_angle
while True:
# Append the current angle to the angles list
angles.append(current_angle % 360)
# Convert the current angle to radians
current_angle_rad = math.radians(current_angle % 360)
# Convert polar to Cartesian coordinates
x = radius * math.cos(current_angle_rad)
y = radius * math.sin(current_angle_rad)
# Append the (x, y) pair to the list
coordinates.append((x, y))
# Check if we've reached the end_angle (handling wrap-around) (current_angle - step_size) % 360 == end_angle or
if current_angle % 360 == end_angle:
break
# Decrement the current angle by the step size
current_angle -= step_size
if current_angle < 0:
current_angle += 360
else:
# Counterclockwise rotation
current_angle = start_angle
while True:
# Append the current angle to the angles list
angles.append(current_angle % 360)
# Convert the current angle to radians
current_angle_rad = math.radians(current_angle % 360)
# Convert polar to Cartesian coordinates
x = radius * math.cos(current_angle_rad)
y = radius * math.sin(current_angle_rad)
# Append the (x, y) pair to the list
coordinates.append((x, y))
# Check if we've reached the end_angle (handling wrap-around) (current_angle + step_size) % 360 == end_angle or
if current_angle % 360 == end_angle:
break
# Increment the current angle by the step size
current_angle += step_size
if current_angle >= 360:
current_angle -= 360
return [angles, coordinates]
def generate_coord_list_fixed_angle(angle, b_val, b_val_step_size, reverse=False):
"""
Generates a list of (x, y) Cartesian coordinates along a line defined by a fixed angle,
scanning from -b_val to b_val or from b_val to -b_val depending on the reverse flag.
Args:
angle (float): The fixed angle (in degrees) from the positive x-axis.
b_val (float): The maximum distance from the origin (both positive and negative).
b_val_step_size (float): The increment in distance for each point.
reverse (bool): If True, scan from b_val to -b_val. If False, scan from -b_val to b_val.
Returns:
list: A list of tuples representing Cartesian coordinates (x, y).
"""
coordinates = []
# Convert angle from degrees to radians
angle_rad = math.radians(angle)
# Determine the scan direction based on the reverse flag
if reverse:
# Scan from b_val to -b_val
current_b = b_val
while current_b >= -b_val:
x = current_b * math.cos(angle_rad)
y = current_b * math.sin(angle_rad)
coordinates.append((x, y))
current_b -= b_val_step_size
else:
# Scan from -b_val to b_val
current_b = -b_val
while current_b <= b_val:
x = current_b * math.cos(angle_rad)
y = current_b * math.sin(angle_rad)
coordinates.append((x, y))
current_b += b_val_step_size
return coordinates
if __name__=="__main__":
# Example usage
radius = 5
start_angle = 0
end_angle = 180
step_size = 10
angles, coordinates = generate_angle_coord_list(radius, start_angle, end_angle, step_size, clockwise=True)
print('\n', "Angles:", angles, '\n')
print("Coordinates:", coordinates, '\n',)
# device_target_values = [{'2301034': bval[0], '2101014': bval[1]} for bval in coordinates]
xcoord_tuple, ycoord_tuple = zip(*coordinates)
device_target_values = {'2301034': list(xcoord_tuple), '2101014': list(ycoord_tuple)}
print(f"{device_target_values['2301034']=}")
print(f"{device_target_values['2101014']=}")
for iteration, (device_id,bval_lst) in enumerate(device_target_values.items()):
print(iteration, device_id, bval_lst)
# print(generate_coord_list_fixed_angle(10, 5, 1, reverse=False))
testdict = [{'2301034': bval[0], '2101014': bval[1]} for bval in coordinates]
print(f"{testdict=}")
for i, target in enumerate(testdict):
print(i, target.keys())
# for key in target.keys():
# print(type(key))