Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize three-dimensional Snow effect by using Koch Curve in Python

Shulou Source: shulou.com Published: 2022-06-01 09:44:08 10月28日 Update

This article mainly introduces how Python uses Koch curve to achieve three-dimensional snow effect, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Random snowflake

If you randomly generate some points, and then draw some branches for each point, you can draw a structure similar to dandelion, but it doesn't look very good.

Import numpy as npimport matplotlib.pyplot as pltfrom numpy.random import rand,randintM,N = 10100x = rand (N) * 100y = rand (N) * 100plt.scatter (x [I], x [I] + r*np.cos (theta)] for i in range (N): M = randint (5, 15) r = rand () * 3 for j in range (M): theta = np.pi*2/M*j plt.plot ([x [I], x [I] + r*np.cos (theta)] [y [I], y [I] + r*np.sin (theta)]) plt.axis ('off') plt.show ()

Of course, it can also be drawn into a three-dimensional picture, and it is still very ugly.

Koch snowflakes

So, since you want to snow, you must first have snowflakes. Koch curve is also called snowflake curve because it is very similar to snowflake. The generation method is very simple, which is divided into two steps.

Draw a regular triangle to divide each side of the regular triangle equally, then take the middle part as the side, and then draw a triangle. Repeat step two.

So the difficulty is nothing more than how to draw a new triangle after the third division, and further, where is the new vertex of the new triangle?

In order to get

The code is as follows

Import numpy as npimport matplotlib.pyplot as pltfrom numpy.random import rand,randint# n > = 1, the method of generating Koch snowflakes def Koch (LMaginnum 1): if n

Tags: Snowflake Koch 3D curve article generation triangle triangle effect method regular triangle good-looking lowest highest further code to medium value interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Information MariaDB MySQL