Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw a beautiful Saturn ring with Python

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to draw a beautiful Saturn ring in Python. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.

Saturn's planetary rings are famous. Although Jupiter, Saturn, Uranus and Neptune also have rings, Saturn's rings are the largest, brightest and most widely known planetary rings in our solar system.

It consists of particles as small as dust and objects as large as boulders. These objects, mostly ice, are thought to have been created when comets or larger asteroids collided with one of Saturn's moons, both breaking into small pieces. Saturn has been known since ancient times, but it was not until 1610 that Galileo first observed it with a telescope.

The planet is named after Saturn, the Roman god of agriculture, which is Saturday, the sixth day of our week.

The images in Figures 1 to 5 were generated by the code at the end of this article. Each graph presents a different orientation angle, and the corresponding explanation is given in the graph title.

The unit vector components of incident light are also listed in the figure title, e.g. lx=+0.707, ly =+0.707, lz =0 for the light source in the upper left quadrant;lx=-1, ly =0, lz=0 for the light source from the right. Note in the image the shadows cast by the planets on the rings, especially the curvature of the planet's outline visible in Figure 5.

Saturn 1 with rings and shadows: Rx=-20°, Ry=0, Rz=-10°, lx=1, ly=0, lz=0

Saturn 2 with rings and shadows: Rx=-8°, Ry=0, Rz=-30°, lx=0.707, ly=.707, lz=0

Saturn 3 with rings and shadows: Rx=20°, Ry=0, Rz=25°, lx=0.707, ly=0.707, lz=0

Saturn 4 with rings and shadows: Rx=-10°, Ry=0, Rz=25°, lx=0.707, ly=-0.707, lz=0

Saturn 5 with rings and shadows: Rx=20°, Ry=0, Rz=30°, lx=-1, ly=0, lz=0

For comparison, you can find Saturn's photographs at:

https://www.jpl.nasa.gov/spaceimages/? search=saturn&category=#submit

Figure 6 shows the mathematical model used to construct Saturn's rings. Here we introduce an algorithm to realize sphere coloring. First create an upright sphere, that is, longitude is vertical and latitude is horizontal (i.e. parallel to the XZ plane), and then rotate the sphere around the x, y, and z axes from the initial orientation.

Figure 6 Saturn ring model: Planetary top view and looking down at rings from XZ plane Rx=0, Ry=0, Rz=0

We do the same thing with Saturn's rings. We can create a horizontal ring parallel to the XZ plane and rotate it the same angle as Saturn. Saturn's rings lie in planes that pass through Saturn's center, so Saturn and the rings have the same center of rotation.

Saturn's rings are drawn as a series of adjacent concentric circles, each consisting of short segments. Referring to Figure 6 and the end-of-text code, lines 42 and 43 set the inner and outer radii of Saturn's rings, and line 44 sets the spacing of concentric circles. Saturn's rings are divided into seven concentric rings (not shown in Figure 6) of different colors, and deltar in line 45 is their width.

Each of the segments that make up the concentric circles is drawn separately. Line 48 draws from r1 to r2, drawing arc segments through a radial loop. Line 49 is a circle drawn around the circumference. Performing the rotation operation on lines 50-61 produces the global plot coordinates xpg and ypg on lines 62 and 63, and the rotation function is the same as in the previous program.

Next, set the color of the segment in lines 66-75. Saturn's rings are composed of bands of different colors, consistent with the physical composition seen in NASA's observations. The first band from r = r1 to r1 + deltar has the color clr=(.63,.54,.18), as do the remaining bands.

The fifth bar is omitted because it is empty and the background color can be displayed. The sixth strip is twice as wide as the others and provides color for the seventh strip.

For a given light direction, the planetary body itself casts a shadow on the ring from most angles. Referring to Figure 7, our goal is to determine whether point p lies inside or outside the shadow region of the planet.

A spherical planet will produce a circular shadow with a diameter equal to the planet's size, or more accurately, the "great circle" of the sphere. It's the largest circle of a sphere cut by a plane passing through the center, like cutting an orange in half, and you see the largest circle of an orange.

In Figure 7, this shadow may be produced by a disk projection of the same size or by a spherical planet projection, and in both cases the shadow size is the same. In the profile view of Saturn, the great circle is shown as a bold line through the center of the plane.

As can be seen from the geometry of Figure 7, if p lies| B|> rs, then it lies outside the shadow zone, where rs is the radius of Saturn; if| B| < rs, then p lies in the shaded area. When drawing the stripe, if we determine that p is in the shaded area, we color it gray, and if it is outside the shaded area, we color it with the stripe color set in lines 66-75.

▲ Figure 7 Shadow model

So our goal is to find out what is going on at a given position p| B| As can be seen from Figure 7:

|B|=|V|sin(φ)

And we know:

V×û=|V||û|sin(φ)

where =-. Compare the above equation with| û| =1 is combined into:

B=V×û

|B|=|V×û|

In line 78 of the code, it is determined that the length of the incident light vector is 1, but if the input components in lines 23-25 are not calculated to be 1, that is,

the incident light vector may not equal 1. If necessary, refactor at lines 79-81. Lines 82-84 establish the components of vector V. Lines 85-87 calculate the component of B. Line 88 gives its amplitude magB =| B|。

Line 89 determines if p is in the shaded area, and if so, performs the dot product of V and Ny on line 90. This is used to determine whether p is on the side of the planet facing the light source, in which case it is opposite the dark side of the planet and not in the shadow zone. Because the shading algorithm in lines 78-89 does not distinguish the position of p, it must be explicit here. If p is indeed on the dark side of the shaded area, set p to medium gray in line 91.

You must have noticed that Saturn's rings in Figure 6 have a dark band because Saturn's rings are empty at that location: there are no solid particles there, and sunlight cannot be reflected. So we see the background color 'midnightblue' through the band. But this creates a problem where the shadow color is drawn over the background color in that white space, so reset it to 'midnightblue' on lines 93 and 94.

Now that the colors of the bands are established, we can draw Saturn's rings through short segments. Lines 97-100 calculate the starting position of the first segment. Referring to Figure 6, lines 100-101 determine the occlusion relationship of the segment to Saturn, which is drawn before.

103-108 The row determines if the segment is behind Saturn, and if it is behind, it will not be drawn. This occlusion relationship is accomplished by calculating the distance c between the global coordinates of the point and the center of Saturn.

Line 107 means draw the segment if c is greater than 1.075 times the radius of the sphere. The effect of the factor 1.075 is to prevent line segments from coinciding with the surface of the sphere, which is necessary, otherwise visible segments smaller than the radius of the sphere will not be drawn.

There are two things to note about the images generated by the code in this article: first, color. NASA's photographic images appear as a gray with little color, but many Saturn observers describe it as golden, so we chose gold.

All photographers know that it is difficult to represent the true color of an object in a photographic image, and that the color depends on the incident light and the color of the object itself, perhaps best by relying on the naked eye.

If you disagree with the colors in the images generated by this code, you can modify them by changing the definition of clr in the program. The second point to note is the curvature of Saturn's surface shadow in Figure 5, which indicates whether the shading algorithm works as expected.

You can change the direction of incident light in lines 24-26 and the angle of rotation in lines 32-34 at your discretion in the application.

It takes a while to run the code, so be patient.

1""" 2SATURN 3""" 4 5import numpy as np 6import matplotlib.pyplot as plt 7from math import sin, cos, radians, sqrt 8 9plt.axis([0,150,100,0]) 10plt.axis('off') 11plt.grid(False) 12 13print('running') 14#-----------------parameters 15g=[0]*3 16 17xc=80 #---sphere center 18yc=50 19zc=0 20 21rs=25 #---sphere radius 22 23lx=-1 #---light ray unit vector components 24ly=0 25lz=0 26 27IA=0 28IB=.8 29+n=2 30 31Rx=radians(-20) 32Ry=radians(0) 33Rz=radians(30) 34 35#--------same as SHADESPHERE-----– 36 37#-------------------rings 38alpha1=radians(-10) 39alpha2=radians(370) 40dalpha=radians(.5) 41 42r1=rs*1.5 43r2=rs*2.2 44dr=rs*.02 45deltar=(r2-r1)/7 #---ring band width 46 47#-------------rotate ring point p which is at r, alpha 48for r in np.arange(r1,r2,dr): 49 for alpha in np.arange(alpha1,alpha2,dalpha): 50 xp=r*cos(alpha) 51 yp=0 52 zp=-r*sin(alpha) 53 rotx(xc,yc,zc,xp,yp,zp,Rx) 54 xp=g[0]-xc 55 yp=g[1]-yc 56 zp=g[2]-zc 57 roty(xc,yc,zc,xp,yp,zp,Ry) 58 xp=g[0]-xc 59 yp=g[1]-yc 60 zp=g[2]-zc 61 rotz(xc,yc,zc,xp,yp,zp,Rz) 62 xpg=g[0] 63 ypg=g[1] 64 65#-----------------select ring band color 66 if r1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report