简体   繁体   中英

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

Looking to generate a set of whole integer co-ordinates for a circle using a user specified point, working with the formula for a circle: (xa)^2 + (yb)^2 = r^2

How can i do this in a 3d space, finding the co-ordinates of x,y and z.

Parametrics

Don't use the Cartesian format of the equations, use the parametric one

Instead of having (xa)^2 + (yb)^2 = r^2, you have

x = r * cos(t) + a

y = r * sin(t) + b

t, or more commonly for trigonometric functions, θ, is an angle between 0 and 2π

Example code

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)

Spheres

As this is a 2-dimensional surface, a second parameter will be required as one is insufficient

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM