Go Language core beauty 2.3-plural

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go provides two complex types of sizes: Complex64 and complex128, composed of float32 and float64, respectively. Built-in function complex constructs complex numbers from specified real and imaginary parts, built-in functions real and imag to get the real and imaginary parts of complex numbers:

var x complex128 = Complex (1, 2)//1+2ivar y complex128 = Complex (3, 4)//3+4IFMT. Println (x*y)                 //"( -5+10i)" FMT. Println (Real (x*y))           //"-5" FMT. Println (Imag (x*y))           //"10"
If a floating-point literal is followed by an I, such as 3.141592i or 2i, it becomes the imaginary part of a complex number, and the real part of the complex number is 0:
Fmt. PRINTLN (1i * 1i)//"( -1+0i)", i²=-1

In a constant operation rule, complex constants can be added to ordinary numeric constants (integers or floats, real or imaginary), so we can write complex numbers like this: 1 + 2i or equivalent 2i + 1. The above declaration statements for X and Y can be simplified as follows:

x: = 1 + 2iy: = 3 + 4i
A complex number can be compared by = = or! =. Two complex numbers are equal if and only if their real and imaginary parts are equal (the bottom of the complex is a floating-point number, so be especially careful when doing equality comparisons)

The MATH/CMPLX package provides functions that manipulate complex numbers, such as the square root of a complex number or the power function of a complex number:

Fmt. Println (CMPLX. SQRT ( -1))//"(0+1i)"


The following program uses the COMLEX128 algorithm to generate the Mandelbrot image:

Package Mainimport ("image" "Image/color" "Image/png" "MATH/CMPLX" "OS") Func main () {const (XM In, Ymin, xmax, ymax =-2,-2, +2, +2 width, height = 1024x768, 1024x768) img: = image. Newrgba (image. Rect (0, 0, width, height)) for py: = 0; py < height; py++ {y: = float64 (py)/height* (ymax-ymin) + ymin for px: = 0; px < width; px++ {x: = Float64             (px)/width* (xmax-xmin) + xmin Z: = Complex (x, y)//Image point (px, py) represents complex value Z. Img. Set (px, py, Mandelbrot (z))}} png. Encode (OS. Stdout, IMG)//note:ignoring errors}func Mandelbrot (z complex128) color. Color {Const ITERATIONS = $ CONST CONTRAST = $ var v complex128 for N: = uint8 (0); n < iterations; n++ {v = v*v + z if CMPLX. Abs (v) > 2 {return color. Gray{255-contrast*n}}} return color. Black}

There are two loops in the program that read a 1024 * 1024 grayscale involve image at a point, which corresponds to a complex plane between 2 and +2. The program tests each point to see if the distance to the center of the circle is more than 2 (the points fall at the origin of the radius of 2), and if it is exceeded, the point is hidden by the number of cycles it uses to escape, and if not, the value belongs to the Mandelbrot collection and uses the black flag. The final program outputs the resulting PNG image to the discriminating output:



article ownership: Golang Contact: Sun Fei, cto@188.com!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.