131 lines
4.7 KiB
Python
131 lines
4.7 KiB
Python
import math
|
|
|
|
def polar_to_cartesian(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 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 = 0
|
|
step_size = 10
|
|
|
|
angles, coordinates = polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True)
|
|
|
|
print('\n', "Angles:", angles, '\n')
|
|
print("Coordinates:", coordinates, '\n',)
|
|
|
|
print(generate_coord_list_fixed_angle(10, 5, 1, reverse=False))
|
|
|