一道簡(jiǎn)單的算法題

已知A,B坐標(biāo),且A為圓心,求圓與AB連線的交點(diǎn)坐標(biāo)

import copy
class Point(object):
    def __init__(self,x=3,y=3):
        self.x = x
        self.y = y
A = Point(50,50) # 圓點(diǎn)
B = Point(3,3)

threshold = 0.01
target = copy.deepcopy(B)  # 初始點(diǎn)
dx = A.x - B.x
dy = A.y - B.y
dx_y = dx/dy
y_step = 0.01
x_step = y_step * dx_y
choice = None
import math

moves = {}
moves["up"] = [x_step,y_step]
moves["down"] = [-x_step,-y_step]

def cal_dist(p1,p2:Point)->float:
    return abs(math.sqrt((p1.x-p2.x) ** 2+(p1.y-p2.y)**2))

AB =  cal_dist(A,B)
# first_move = True

# choice = None
def move(target,choice):
    target.x +=moves[choice][0]
    target.y +=moves[choice][1]
    
import math
import copy

xs = []
ys = []

def find_point(r:int):
    """
    r:圓半徑
    target: 目標(biāo)點(diǎn)
    """
    start_loss = abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r) # 目標(biāo)點(diǎn)到圓心的距離
    target.x +=moves["up"][0]
    target.y +=moves["up"][1]
    loss =abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r)
    if loss < start_loss:
        choice = "up"
    else:
        choice = "down"
    while loss > 0.01:
        move(target,choice)
        xs.append(target.x)
        ys.append(target.y)
        loss = abs(math.sqrt((target.x-A.x) ** 2+(target.y-A.y)**2) - r)
    return target
res = find_point(1)
res.x
49.289999999998784
res.y
49.289999999998784
xs
[3.0199999999999996,
 3.0299999999999994,
 3.039999999999999,
 3.049999999999999,
  ...
 13.009999999999787,
 ...]
import matplotlib.pyplot as plt
plt.plot(xs, ys)
plt.show()
output_9_0.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基于學(xué)生學(xué)習(xí)共同體培育語(yǔ)文生態(tài)課堂文化的研究 近年來(lái),隨著現(xiàn)代教育理念的不斷深入與...
    火車頭123閱讀 2,272評(píng)論 0 8
  • 題目:統(tǒng)計(jì)給定數(shù)字中,值為1的二進(jìn)制位的數(shù)量。如果是數(shù)組呢? 解法1:遍歷算法 第一種想法比較簡(jiǎn)單,從最后一位開(kāi)始...
    Torival閱讀 1,537評(píng)論 0 9
  • 我們每周有技術(shù)分享會(huì),今天有道算法跟大家分享下。 我說(shuō)說(shuō)我知道的三種思路 先剔除N,再找出最大值 這種方式要for...
    慢慢爬的小蝸牛stone閱讀 350評(píng)論 0 0
  • 數(shù)學(xué)一直是部分同學(xué)的弱項(xiàng),小優(yōu)以前總以為數(shù)學(xué)是偏向計(jì)算的,整理下來(lái)才發(fā)現(xiàn)原來(lái)竟然有這么多的定理公式要記,特別分享給...
    錯(cuò)題plus閱讀 1,042評(píng)論 1 2
  • 專業(yè)考題類型管理運(yùn)行工作負(fù)責(zé)人一般作業(yè)考題內(nèi)容選項(xiàng)A選項(xiàng)B選項(xiàng)C選項(xiàng)D選項(xiàng)E選項(xiàng)F正確答案 變電單選GYSZ本規(guī)程...
    小白兔去釣魚閱讀 10,466評(píng)論 0 13

友情鏈接更多精彩內(nèi)容