Python半圆边上任意点的坐标(郑)

import numpy as np

import os

import csv

 

base = os.path.abspath(os.path.dirname(__file__))  # 当前路径

file_name = "/ordinates_data.csv"

file_path = base + file_name

 

radius = 100  # 设置圆的半径

step = 0.05  # 步长为0.05

start = 0

end = 100 + step

X_LIST = np.arange(start,end, step)  # [0, 100] 每隔0.05取1个值

LENGTH = len(X_LIST)  # 序列x的长度

pow_value = np.ones(LENGTH) * 2    # 有LENGTH个元素值为2的array型的列表

 

Y_LIST = np.sqrt(radius**2 - np.power((X_LIST-radius),pow_value))  # 圆的方程求解

# 由于有些太小的值,计算机可能会将其赋值为Nan型,因此要将值为Nan的元素置为0

Y_LIST[np.isnan(Y_LIST)] = 0

 

X_LIST = np.round(X_LIST, 5) # np.round()函数第2个参数值为小数点后面保留的位数

Y_LIST = np.round(Y_LIST, 5)

 

 

# 将数据写入file_path文件中

with open(file_path,'w+') as csvfile:

    header = ['x', 'y']

    writer = csv.DictWriter(csvfile, fieldnames=header)

    writer.writeheader()

    for idx in range(LENGTH):

        x = X_LIST[idx]

        y = Y_LIST[idx]

        writer.writerow({'x':x, 'y':y})

 

# 因为输入的值不一定落在X_LIST序列里,因此需要将其转化为离其最近的x坐标点

def find_close_data(X_LIST, e):

    LENGTH = len(X_LIST)  # 序列x的长度

    e_list = np.ones(LENGTH) * e  # 有LENGTH个元素值为e的array型的列表

    distance = list(np.abs(e_list - X_LIST))

    index = distance.index(min(distance))

    y = Y_LIST[index]

    return  y

 

while True:

    input_x = input("Please enter the value of the x coordinate:")

    input_x = float(input_x)

 

    data_y = find_close_data(X_LIST, input_x)

 

    # 当输入大于100的值,程序退出

    if input_x > 100:

        break

    print(data_y)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值