python画蜗型线、心形线、三叶曲线、四叶曲线、螺旋线

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()

三叶曲线:
在这里插入图片描述
心形线:
在这里插入图片描述
螺旋线:
在这里插入图片描述
四叶曲线:
在这里插入图片描述
蜗型线:
在这里插入图片描述

参考:https://blog.csdn.net/Leonis_v/article/details/44902411

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值