簡體   English   中英

如何使用圓公式在python中為圓生成一組坐標?

[英]How to generate a set of co-ordinates for a circle in python using the circle formula?

希望使用用戶指定的點為圓生成一組整數坐標,使用圓的公式:(xa)^2 + (yb)^2 = r^2

我怎樣才能在 3d 空間中做到這一點,找到 x、y 和 z 的坐標。

參數

不要使用方程的笛卡爾格式,使用參數格式

而不是有 (xa)^2 + (yb)^2 = r^2,你有

x = r * cos(t) + a

y = r * sin(t) + b

t,或更常見的三角函數,θ,是 0 和 2π 之間的角度

示例代碼

import math

a = 2
b = 3
r = 3

#The lower this value the higher quality the circle is with more points generated
stepSize = 0.1

#Generated vertices
positions = []

t = 0
while t < 2 * math.pi:
    positions.append((r * math.cos(t) + a, r * math.sin(t) + b))
    t += stepSize

print(positions)

球體

由於這是一個二維表面,將需要第二個參數,因為一個不夠

u = [0, 2π] v = [-π/2, π/2]

x = r * sin(u) * cos(v) + a

y = r * cos(u) * cos(v) + b

z = r * sin(v) + c

import math

a = 2
b = 3
c = 7
r = 3

#The lower this value the higher quality the circle is with more points generated
stepSize = 0.1

#Generated vertices
positions = []

u = 0
v = -math.pi/2
while u < 2 * math.pi:
    while v < math.pi/2:
        positions.append((r * math.sin(u) * math.cos(v) + a, r * math.cos(u) * math.cos(v) + b, r * math.sin(v) +  c))
        v += stepSize
    u += stepSize

print(positions)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM