How to draw 3D Diagram by Python
This article mainly introduces how to draw a three-dimensional map of Python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Usually what we draw with Python is a two-dimensional plan, but sometimes we need to draw a three-dimensional scene, such as the following:
How do you make these pictures? Today, let's share how to draw a three-dimensional vector (SVG) diagram step by step.
Octahedron
Let's take the following octahedron as an example.
1 install related packages
First install two prerequisites:
3D function Library of import pyrr # NumPy
Import svgwrite # svg graphic processing Library
2 define 3D map generation environment
Next, define several classes to set up the basic environment of the 3D map:
Viewport: range of rectangular chart
Camera: including view matrix and projection matrix
Mesh surface matrices, shaders and style dictionaries required for mesh:svg vector graphics
3 generate octahedral data
Then generate data for each fixed point of the octahedron:
Def octahedron ():
"" Construct an eight-sided polyhedron "
F = sqrt (2.0) / 2.0
Verts = numpy.float32 ([(0,-1, 0), (- f, 0, f), (f, 0, f), (f, 0,-f), (- f, 0,-f), (0, 1, 0)])
Triangles = numpy.int32 ([(0,2,1), (0,3,2), (0,4,3), (0,1,4), (5,1,2), (5,2,3), (5,3,4), (5,4,1)]))
Return verts [triangles]
4 pyrr package rendering
Then use the pyrr 3D library to render the above raw data, and finally generate the svg vector graph.
5 generate svg
In the last step above, to generate the svg graph, you need to call the following Engine function, which is slightly more complicated:
The above code is all encapsulated into a class to generate octahedral graphics.
Many other graphics can be generated in addition to octahedrons.
Sphere and Klein bottle
The code is as follows:
Polyhedral sphere
The code is as follows:
A glowing sphere
Code implementation:
You can also draw this kind of surface.
The code is implemented as follows:
Thank you for reading this article carefully. I hope the article "how to draw a three-dimensional picture of Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!