import numpy as np
import math
import matplotlib.pyplot as plt
import copy
class Curve:
def __init__(self,a,b,curve_type,plot_type):
self.a = a
self.b = b
self.curve_type = curve_type
self.plot_type = plot_type
self.points = []
def draw_curve(self):
points = np.array(self.points).T
if self.plot_type == "scatter":
plt.scatter(points[0], points[1])
elif self.plot_type == "line":
plt.plot(points[0], points[1])
plt.title(self.curve_type)
plt.show()
def get_points(self):
x0 = 200
y0 = 250
dtheta = 1.0/self.a
point1 = [x0,y0]
if self.curve_type == "limacon":
point1[0] += self.a + self.b
elif self.curve_type == "cardioid":
point1[0] += 2*self.a
elif self.curve_type == "threeLeaf":
point1[0] += a
elif self.curve_type == "fourLeaf":
point1[0] += a
self.points.append(point1)
theta = dtheta
point2 = [0,0]
while(theta<2*np.pi):
if self.curve_type == "limacon":
r = self.a * math.cos(theta) + self.b
elif self.curve_type == "cardioid":
r = self.a * math.cos(theta) + self.a
elif self.curve_type == "threeLeaf":
r = self.a * math.cos(3 * theta)
elif self.curve_type == "fourLeaf":
r = self.a * math.cos(2 * theta)
elif self.curve_type == "spiral":
r = (self.a / 4.0)*theta
point2[0] = x0 + r*math.cos(theta)
point2[1] = y0 + r*math.sin(theta)
point1 = copy.deepcopy(point2)
self.points.append(point1)
theta += dtheta
if __name__ == "__main__":
a = 175
b = 60
curve_type = "spiral"
plot_type = "scatter"
cur = Curve(175,60,curve_type,plot_type)
cur.get_points()
cur.draw_curve()
三叶曲线:
心形线:
螺旋线:
四叶曲线:
蜗型线: