How to find red points (in square borders)

Question:

How to find this points if we know only radius and a?

square with circle

i just know how to find the points in circle borders. but how to do this with square. I have attached code example. And get errors in square_borders. Dont know how to fix it

import numpy as np
import math
import cv2

map = np.zeros((500,500), dtype=np.int8)
cv2.circle(map,(250,250), 250, 50,2)
position = (250,250)
r = map.shape[0] / 2

ray_numbers = 120
field_of_view = math.radians(360)
step_angle = field_of_view / ray_numbers
start_angle = math.radians(0)

def circle_borders(start_angle,step_angle):
    for _ in range(ray_numbers):
        target_x = round(position[1]- r * math.sin(start_angle))
        target_y = round(position[0] - r * math.cos(start_angle))
        cv2.line(map, position, (target_x,target_y), 100, 2)
        start_angle += step_angle
        
def square_borders(start_angle,step_angle):
    for _ in range(ray_numbers):
        target_x = round(position[1] + r * math.tan(start_angle))   # here is the 
        target_y = round(position[0] + r)                           # trouble
        cv2.line(map, position, (target_x,target_y), 255, 2)
        start_angle += step_angle



circle_borders(start_angle,step_angle)
square_borders(start_angle,step_angle)

cv2.imshow('circle and square borders', map)
cv2.waitKey(0)
Asked By: Xover Chik

||

Answers:

Perhaps,

def square_borders(start_angle,step_angle):
    for _ in range(ray_numbers):
        b = max(abs(math.sin(start_angle)), abs(math.cos(start_angle)))
        target_x = round(position[1] - r * math.sin(start_angle) / b)
        target_y = round(position[0] - r * math.cos(start_angle) / b)
        cv2.line(map, position, (target_x,target_y), 255, 2)
        start_angle += step_angle
Answered By: ardget
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.