本文記錄使用 Go 語言實現(xiàn) RESTful 的點坐標(biāo)的轉(zhuǎn)換。

十載的達(dá)拉特網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整達(dá)拉特建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“達(dá)拉特網(wǎng)站設(shè)計”,“達(dá)拉特網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
假設(shè)同一個點使用極坐標(biāo)表示為 (ρ, θ), 使用笛卡爾坐標(biāo)表示為(x,y),那么,這些數(shù)學(xué)符號之間,有如下關(guān)系
x = ρ* Cosθ
y = ρ* Sinθ
ρ= Sqrt(x*x+y*y)
θ = Arctan(x/y)
/*
* @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 main
import (
"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 []DotJ
type DotDs []DotD*/
/*
http://127.0.0.1:6688/d/12/5
{"r":13,"a":22.61986}
*/
func doD(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
errV := 0
x, errX := strconv.ParseFloat(strings.TrimSpace(vars["x"]), 64)
y, errY := strconv.ParseFloat(strings.TrimSpace(vars["y"]), 64)
if errX != nil {
fmt.Println("第1個值x輸入錯誤!")
errV = 1
} else {
if errY != nil {
fmt.Println("第2個值Y輸入錯誤!")
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")
}
}
//極坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo)
/*
http://127.0.0.1:6688/j/13/22.61986
{"x":12,"y":5}
*/
func doJ(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
errV := 0
rr, errR := strconv.ParseFloat(strings.TrimSpace(vars["r"]), 64)
aa, errA := strconv.ParseFloat(strings.TrimSpace(vars["a"]), 64)
if errR != nil {
fmt.Println("第1個值x輸入錯誤!")
errV = 1
} else {
if errA != nil {
fmt.Println("第2個值Y輸入錯誤!")
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)
// 笛卡爾坐標(biāo)轉(zhuǎn)換為極坐標(biāo)
myRouter.HandleFunc("/d/{x}/{y}", doD)
// 極坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo)
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使用*/
func fireNow() {
var (
ρ,
θ,
x,
y float64
)
methodType := 1
fmt.Print("請選擇轉(zhuǎn)換方式:\n輸入1,表示需要從極坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo);\n輸入2,表示需要從笛卡爾坐標(biāo)轉(zhuǎn)換為極坐標(biāo)\n?")
fmt.Scan(&methodType)
if methodType != 1 && methodType != 2 {
fmt.Println("貌似你輸入的不是1,也不是2啊,搞哪樣?")
fireNow()
} else {
switch methodType {
//輸入1,表示需要從極坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo);
case 1:
fmt.Println("請以極坐標(biāo)格式輸入點的坐標(biāo)(ρ和 θ之間用1個空格隔開,θ默認(rèn)為弧度單位)?")
fmt.Scan(&ρ, &θ)
θ = jiaodu2hudu(θ)
x = ρ * math.Cos(θ)
y = ρ * math.Sin(θ)
fmt.Printf("x = %f, y= %f\n", x, y)
//輸入2,表示需要從笛卡爾坐標(biāo)轉(zhuǎn)換為極坐標(biāo)
case 2:
fmt.Println("請以笛卡爾坐標(biāo)格式輸入點的坐標(biāo)(x和y之間用1個空格隔開, x不能為0)?")
fmt.Scan(&x, &y)
ρ = math.Sqrt(x*x + y*y)
θ = math.Atan(y / x)
θ = hudu2jiaodu(θ)
fmt.Printf("ρ= %f, θ= %f\n", ρ, θ)
}
}
}笛卡爾坐標(biāo)轉(zhuǎn)極坐標(biāo)示例 URL
http://127.0.0.1:6688/d/12/5
將會返回
{"r":13,"a":22.61986}
極坐標(biāo)轉(zhuǎn)笛卡爾坐標(biāo)示例URL
http://127.0.0.1:6688/j/13/22.61986
將會返回
{"x":12,"y":5}兩種轉(zhuǎn)換默認(rèn)精確到小數(shù)點后5位。
本文題目:Go語言:極坐標(biāo)與笛卡爾坐標(biāo)的互轉(zhuǎn)
網(wǎng)站鏈接:http://www.chinadenli.net/article46/gccshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、商城網(wǎng)站、面包屑導(dǎo)航、ChatGPT、微信小程序、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)