欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Python實(shí)現(xiàn)繪制3D愛(ài)心的代碼分享

 更新時(shí)間:2022年11月11日 10:59:01   作者:神仙盼盼  
最近你是否也被李峋的愛(ài)心跳動(dòng)代碼所感動(dòng),心動(dòng)不如行動(dòng),相同的代碼很多,我們今天換一個(gè)玩法!構(gòu)建一個(gè)三維的跳動(dòng)愛(ài)心!嗯!這篇博客本著開(kāi)源的思想!不是說(shuō)誰(shuí)對(duì)浪漫過(guò)敏的

環(huán)境介紹

python3.8

numpy

matplotlib

第一步,繪制一個(gè)三維的愛(ài)心

關(guān)于這一步,我采用的是大佬博客中的最后一種繪制方法。當(dāng)然,根據(jù)我的代碼習(xí)慣,我還是稍做了一點(diǎn)點(diǎn)修改的。

class Guess:
    def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20) -> None:
        """
        bbox: 控制畫格的大小
        resolution: 控制愛(ài)心的分辨率
        lines: 控制等高線的數(shù)量
        """
        self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3
        A = np.linspace(self.xmin, self.xmax, resolution)
        self.B = np.linspace(self.xmin, self.xmax, lines)
        self.A1, self.A2 = np.meshgrid(A, A)
        

    def coordinate(self, x, y, z):
        """
        生成坐標(biāo)
        """
        return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3

    def draw(self, ax):
        """
        繪制坐標(biāo)
        """
        for z in self.B:
            X, Y = self.A1, self.A2
            Z = self.coordinate(X, Y, z)+z
            cset = ax.contour(X, Y, Z, [z], zdir='z', colors=('pink',))

        for y in self.B:
            X, Z = self.A1, self.A2
            Y = self.coordinate(X, y, Z)+y
            cset = ax.contour(X, Y, Z, [y], zdir='y', colors=('pink',))

        for x in self.B:
            Y, Z = self.A1, self.A2
            X = self.coordinate(x, Y, Z) + x
            cset = ax.contour(X, Y, Z, [x], zdir='x', colors=('pink',))

    def run(self):
        fig = plt.figure()
        ax = fig.add_subplot(projection='3d')
        ax.set_zlim3d(self.zmin, self.zmax)
        ax.set_xlim3d(self.xmin, self.xmax)
        ax.set_ylim3d(self.ymin, self.ymax)
        plt.show()

但是這可以達(dá)到我們想要的效果嗎?

顯然不能!于是我們開(kāi)始加入億點(diǎn)點(diǎn)細(xì)節(jié)!

億點(diǎn)點(diǎn)細(xì)節(jié)

加入時(shí)間序列

想要心臟跳起來(lái),我們就需要有時(shí)間維度的變化。那怎么做最合理呢?這里僅展示修改的代碼位置。

class Guess:
    def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20) -> None:
        plt.ion()                                         # 開(kāi)啟畫布的動(dòng)態(tài)圖模式
        self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3
        self.time = time.time()                           # 這里有一個(gè)衡量的時(shí)間坐標(biāo),很合理吧
        A = np.linspace(self.xmin, self.xmax, resolution)
        self.B = np.linspace(self.xmin, self.xmax, lines)
        self.A1, self.A2 = np.meshgrid(A, A)

    def run(self, count):
        """
        加入count是我們想循環(huán)的次數(shù)
        """
        fig = plt.figure()
        for i in range(count):
            plt.clf()                               # 每次清除畫布
            ax = fig.add_subplot(projection='3d')
            ax.set_zlim3d(self.zmin, self.zmax)
            ax.set_xlim3d(self.xmin, self.xmax)
            ax.set_ylim3d(self.ymin, self.ymax)
            times = time.time()-self.t/ime          # 計(jì)算畫布的當(dāng)前時(shí)間狀態(tài)
            self.draw(ax, coef)
            plt.show()

加入心臟的跳動(dòng)

心臟的跳動(dòng)當(dāng)然不會(huì)是線性的了,我們需要心臟的跳動(dòng)是有層次感的,并且還是可以做往返運(yùn)動(dòng)的。

emmmmm… 這么說(shuō)來(lái),cos是不是就是做這個(gè)用的?

于是…

    def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20, scale=1.2) -> None:
        """
        scale: 心臟縮放的系數(shù)
        """
        self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3
        plt.ion() 
        self.scale = scale   # scale: 心臟縮放的系數(shù) 設(shè)置為全局變量
        self.time = time.time()
        A = np.linspace(self.xmin, self.xmax, resolution)
        self.B = np.linspace(self.xmin, self.xmax, lines)
        self.A1, self.A2 = np.meshgrid(A, A)

    def draw(self, ax, coef):
        """
        coef: 使得心臟可以按照時(shí)間跳動(dòng)
        """
        for z in self.B:
            X, Y = self.A1, self.A2
            Z = self.coordinate(X, Y, z)+z
            cset = ax.contour(X * coef, Y * coef, Z * coef, [z * coef], zdir='z', colors=('pink',))

        for y in self.B:
            X, Z = self.A1, self.A2
            Y = self.coordinate(X, y, Z)+y
            cset = ax.contour(X * coef, Y * coef, Z * coef, [y * coef], zdir='y', colors=('pink',))

        for x in self.B:
            Y, Z = self.A1, self.A2
            X = self.coordinate(x, Y, Z) + x
            cset = ax.contour(X * coef, Y * coef, Z * coef, [x * coef], zdir='x', colors=('pink',))

    def run(self, count):
        """
        加入count是我們想循環(huán)的次數(shù)
        """
        fig = plt.figure()
        for i in range(count):
            plt.clf()                               # 每次清除畫布
            ax = fig.add_subplot(projection='3d')
            ax.set_zlim3d(self.zmin, self.zmax)
            ax.set_xlim3d(self.xmin, self.xmax)
            ax.set_ylim3d(self.ymin, self.ymax)
            times = time.time()-self.time
            coef = np.cos(times) * (self.scale-1) + 1
            # coef 是用來(lái)放縮心臟的大小的,加入cos來(lái)使它有節(jié)奏的跳動(dòng)
            self.draw(ax, coef)
            plt.pause(0.01)
            plt.show()

很好,這樣我們就有了一個(gè)可以跳動(dòng)的心臟,那么到這結(jié)束了嘛?

一個(gè)好的展示

當(dāng)然沒(méi)有!我們希望對(duì)象看到的時(shí)候他稍微有點(diǎn)東西,所以讓它跳動(dòng)卻不能改變方向,豈不是看的不夠全面?所以我們?cè)诩幼詈髢|點(diǎn)點(diǎn)細(xì)節(jié):

    def run(self, count):
        fig = plt.figure()
        for i in range(count):
            plt.clf()
            ax = fig.add_subplot(projection='3d')
            ax.set_title("你對(duì)象的名字?")              # 加上你對(duì)象的小name
            ax.set_zlim3d(self.zmin, self.zmax)
            ax.set_xlim3d(self.xmin, self.xmax)
            ax.set_ylim3d(self.ymin, self.ymax)
            times = time.time()-self.time
            ax.view_init(10, 100+np.cos(times) * 10)   # 讓三維坐標(biāo)圖可以變換坐標(biāo)展示
            coef = np.cos(times) * (self.scale-1) + 1
            self.draw(ax, coef)
            plt.pause(0.01)  # 讓繪制出來(lái)的心臟可以顯示
            plt.show()

完整代碼

代碼完整版及效果如下

import time
import numpy as np
import matplotlib.pyplot as plt


class Guess:
    def __init__(self, bbox=(-1.5, 1.5), resolution=50, lines=20, scale=1.2) -> None:
        self.xmin, self.xmax, self.ymin, self.ymax, self.zmin, self.zmax = bbox*3
        plt.ion() 
        self.scale = scale
        self.time = time.time()
        A = np.linspace(self.xmin, self.xmax, resolution)
        self.B = np.linspace(self.xmin, self.xmax, lines)
        self.A1, self.A2 = np.meshgrid(A, A)
        

    def coordinate(self, x, y, z):
        return (x**2+(9/4)*y**2+z**2-1)**3-x**2*z**3-(9/80)*y**2*z**3

    def draw(self, ax, coef):
        for z in self.B:
            X, Y = self.A1, self.A2
            Z = self.coordinate(X, Y, z)+z
            cset = ax.contour(X * coef, Y * coef, Z * coef, [z * coef], zdir='z', colors=('pink',))

        for y in self.B:
            X, Z = self.A1, self.A2
            Y = self.coordinate(X, y, Z)+y
            cset = ax.contour(X * coef, Y * coef, Z * coef, [y * coef], zdir='y', colors=('pink',))

        for x in self.B:
            Y, Z = self.A1, self.A2
            X = self.coordinate(x, Y, Z) + x
            cset = ax.contour(X * coef, Y * coef, Z * coef, [x * coef], zdir='x', colors=('pink',))

    def run(self, count):
        fig = plt.figure()
        for i in range(count):
            plt.clf()
            ax = fig.add_subplot(projection='3d')
            ax.set_title("2LiuYu")
            ax.set_zlim3d(self.zmin, self.zmax)
            ax.set_xlim3d(self.xmin, self.xmax)
            ax.set_ylim3d(self.ymin, self.ymax)
            times = time.time()-self.time
            ax.view_init(10, 100+np.cos(times) * 10)
            coef = np.cos(times) * (self.scale-1) + 1
            self.draw(ax, coef)
            plt.pause(0.01)
            plt.show()


if __name__ == '__main__':
    demo = Guess()
    demo.run(1000)

以上就是利用Python實(shí)現(xiàn)繪制3D愛(ài)心的代碼分享的詳細(xì)內(nèi)容,更多關(guān)于Python繪制3D愛(ài)心的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python多線程threading join和守護(hù)線程setDeamon原理詳解

    Python多線程threading join和守護(hù)線程setDeamon原理詳解

    這篇文章主要介紹了Python多線程threading join和守護(hù)線程setDeamon原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Python?Apschedule定時(shí)任務(wù)框架的用法詳解

    Python?Apschedule定時(shí)任務(wù)框架的用法詳解

    apschedule是一個(gè)用python寫的定時(shí)處理框架,這篇文章主要為大家詳細(xì)介紹了Apschedule定時(shí)任務(wù)框架的用法,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼

    python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼

    這篇文章主要介紹了python去除刪除數(shù)據(jù)中\(zhòng)u0000\u0001等unicode字符串的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python爬蟲之重放攻擊原理實(shí)例詳解

    Python爬蟲之重放攻擊原理實(shí)例詳解

    重放攻擊是一種網(wǎng)絡(luò)攻擊方式,攻擊者通過(guò)截獲合法用戶的請(qǐng)求,并將其重新發(fā)送,以模擬合法用戶的行為,在Python爬蟲領(lǐng)域,了解重放攻擊的原理和防范方法至關(guān)重要,本文將深入介紹重放攻擊的概念、示例代碼演示以及防范措施,幫助大家更好地理解和應(yīng)對(duì)這一威脅
    2023-12-12
  • 詳解Python中range()與xrange()的區(qū)別

    詳解Python中range()與xrange()的區(qū)別

    range()?和?xrange()?是兩個(gè)函數(shù),可用于在?Python的?for?循環(huán)中迭代一定次數(shù)。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)二者的區(qū)別與使用,需要的可以參考一下
    2022-09-09
  • python 中的[:-1]和[::-1]的具體使用

    python 中的[:-1]和[::-1]的具體使用

    這篇文章主要介紹了python 中的[:-1]和[::-1]的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • python計(jì)算無(wú)向圖節(jié)點(diǎn)度的實(shí)例代碼

    python計(jì)算無(wú)向圖節(jié)點(diǎn)度的實(shí)例代碼

    今天小編就為大家分享一篇python計(jì)算無(wú)向圖節(jié)點(diǎn)度的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python實(shí)現(xiàn)矩陣加法和乘法的方法分析

    Python實(shí)現(xiàn)矩陣加法和乘法的方法分析

    這篇文章主要介紹了Python實(shí)現(xiàn)矩陣加法和乘法的方法,結(jié)合實(shí)例形式對(duì)比分析了Python針對(duì)矩陣的加法與乘法運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • Django 模型類(models.py)的定義詳解

    Django 模型類(models.py)的定義詳解

    這篇文章主要介紹了Django 模型類(models.py)的定義詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python+pyqt5實(shí)現(xiàn)KFC點(diǎn)餐收銀系統(tǒng)

    python+pyqt5實(shí)現(xiàn)KFC點(diǎn)餐收銀系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python+pyqt5實(shí)現(xiàn)KFC點(diǎn)餐收銀系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01

最新評(píng)論