Go language: The mutual transfer of polar coordinates and Cartesian coordinates

Source: Internet
Author: User
Tags cos sin vars

This document records the transformation of point coordinates using the Go language for RESTful points.

The mathematical relationship between polar coordinates and Cartesian coordinates

Assuming that the same point is represented as (ρ,θ) using polar coordinates, and the Cartesian coordinates are represented as (x, y), then these mathematical symbols have the following relationship

X =ρ* Cosθ

Y =ρ* sinθθ

ρ= Sqrt (x*x+y*y)

θ= Arctan (x/y)


Go language implementation
/**  @Author: coolwp.com*  @Date: 2017-09-12 16:25:34*  @Last  modified by : suifengtec*  @Last  modified time: 2017-09-12 16:41:35**//*go build -o  a.exe  main.go*/package mainimport  ("Encoding/json" "FMT" "Github.com/gorilla/mux" "Log "Math" "net/http" "StrConv" "strings") type dotj struct {r float64  ' JSON: "R" ' a  float64  ' JSON: "A" '}type dotd struct {x float64  ' JSON: "X" ' y float64  ' JSON: "Y" '}/*type dotjs []dotjtype dotds []dotd*//*http://127.0.0.1:6688/d/12/5{"R": "A" : 22.61986}*/func dod (w http. Responsewriter, r *http. Request)  {vars := mux. Vars (R) errv := 0x, errx := strconv. Parsefloat (Strings. Trimspace (vars["x"]),  64) Y, erry := strconv. Parsefloat (Strings. Trimspace (vars["y"]),  64) if errx != nil {fmt. PrintlN ("1th value x input Error!") Errv = 1} else {if erry != nil {fmt. Println ("2nd value y input error!") Errv = 2}}if errv == 0 {w.header (). Set ("Content-type",  "Application/json") R := math. Sqrt (x*x + y*y) A := math. Atan (y / x) A = hudu2jiaodu (a) r = tofixed (r, 5) a = tofixed (A,  5) Dotj := dotj{r: r, a: a}json. Newencoder (W). Encode (DOTJ)} else {w.writeheader (404) fmt. Println ("error:404")}}//polar coordinates are converted to Cartesian coordinates/*http://127.0.0.1:6688/j/13/22.61986{"x": "Y": 5}*/func doj (w  http. Responsewriter, r *http. Request)  {vars := mux. Vars (R) errv := 0rr, errr := strconv. Parsefloat (Strings. Trimspace (vars["R"]),  64) Aa, erra := strconv. Parsefloat (Strings. Trimspace (vars["a"]),  64) if errr != nil {fmt. Println ("1th value x input Error!") Errv = 1} else {if&nbSp;erra != nil {fmt. Println ("2nd value y input error!") Errv = 2}}if errv == 0 {w.header (). Set ("Content-type",  "Application/json") Av := jiaodu2hudu (AA) X := rr * math. Cos (AV) Y := rr * math. Sin (AV) x = tofixed (x, 5) y = tofixed (y, 5) dotd := dotd{x: x,  Y: y}json. Newencoder (W). Encode (DOTD)} else {w.writeheader (404) fmt. Println ("error:404")}}func httphandler ()  {myrouter := mux. Newrouter (). Strictslash (true)//  Cartesian coordinates are converted to polar coordinates myrouter.handlefunc ("/d/{x}/{y}",  dod)//  Polar coordinates are converted to Cartesian coordinates myrouter.handlefunc ("/j/{r}/{a}",  doj) log. Fatal (http. Listenandserve (": 6688",  myrouter))}/*======================================================*/func  Jiaodu2hudu (Jiaodu float64)  float64 {return jiaodu * math. Pi / 180}func hudu2jiaodu (Hudu float64)  float64 {return hudU * 180 / math. Pi}func round (Num float64)  int {return int (Num + math. Copysign (0.5, num))}func tofixed (Num float64, precision int)  float64 { Output := math. Pow (10, float64 (precision)) Return float64 (Round (num*output))  / output}func main ()  {httphandler ()/*firenow () */}/*dev: cli use */func firenow ()  {var  (Ρ,θ,x,y float64) Methodtype := 1fmt. Print ("Choose convert: \ n Enter 1, which means you need to convert from polar coordinates to Cartesian coordinates; \ n Enter 2 to indicate that you need to convert from Cartesian coordinates to polar coordinates \ n?") Fmt. Scan (&methodtype) if methodtype != 1 && methodtype != 2 { Fmt. PRINTLN ("Looks like you're not typing 1, it's not 2. ") Firenow ()} else {switch methodtype {//input 1, which indicates the need to convert from polar coordinates to Cartesian coordinates; case 1:fmt. Println ("Please enter the coordinates of the point in polar format (between ρ and  θ with 1 spaces, θ is the default radian units)?" ") Fmt. Scan (&ρ, &θ) θ = jiaodu2hudu (θ) x = ρ * math. Cos (θ) y = ρ * math. Sin (θ) fmt. Printf ("x = %f, y= %f\n",  x, y)//Enter 2, which means that the conversion from Cartesian coordinates to polar case 2:fmt is required. Println ("Please enter the coordinates of the point in Cartesian coordinates (the x and y are separated by 1 spaces,  x cannot be 0)?" ") Fmt. Scan (&x, &y) Ρ = math. Sqrt (x*x + y*y) Θ = math. Atan (y / x) θ = hudu2jiaodu (θ) fmt. Printf ("ρ= %f, θ= %f\n",  ρ, θ)}}}


Cartesian coordinate Polar coordinates example URL

Http://127.0.0.1:6688/d/12/5

Will be returned

{"R": +, "a": 22.61986}

Polar Cartesian Example URL

http://127.0.0.1:6688/j/13/22.61986

will be returned

{"X": +, "Y": 5}

The two conversions are by default accurate to 5 digits after the decimal point.


Go language: The mutual transfer of polar coordinates and Cartesian coordinates

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.