Go 语言: 极坐标与笛卡尔坐标的互转

Go 语言: 极坐标与笛卡尔坐标的互转,第1张

概述本文记录使用 Go 语言实现 RESTful 的点坐标的转换。 极坐标与笛卡尔坐标的数学关系 假设同一个点使用极坐标表示为 (ρ, θ), 使用笛卡尔坐标表示为(x,y),那么,这些数学符号之间,有如下关系 x = ρ* Cosθ y = ρ* Sinθ ρ= Sqrt(x*x+y*y) θ = Arctan(x/y) Go语言实现 /** @Author: coolwp.com* @Date

本文记录使用 Go 语言实现 RESTful 的点坐标的转换。

极坐标与笛卡尔坐标的数学关系

假设同一个点使用极坐标表示为 (ρ,θ),使用笛卡尔坐标表示为(x,y),那么,这些数学符号之间,有如下关系

x = ρ* Cosθ

y = ρ* Sinθ

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

θ = Arctan(x/y)


Go语言实现
/**@Author:coolwp.com*@Date:2017-09-1216:25:34*@LastModifIEdby:suifengtec*@LastModifIEdtime:2017-09-1216:41:35**//*gobuild-oa.exemain.go*/packagemainimport(	"enCoding/Json"	"fmt"	"github.com/gorilla/mux"	"log"	"math"	"net/http"	"strconv"	"strings")typeDotJstruct{	Rfloat64`Json:"r"`	Afloat64`Json:"a"`}typeDotDstruct{	Xfloat64`Json:"x"`	Yfloat64`Json:"y"`}/*typeDotJs[]DotJtypeDotDs[]DotD*//*http://127.0.0.1:6688/d/12/5{"r":13,"a":22.61986}*/funcdoD(whttp.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)	iferrX!=nil{		fmt.Println("第1个值x输入错误!")		errV=1	}else{		iferrY!=nil{			fmt.Println("第2个值Y输入错误!")			errV=2		}	}	iferrV==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")	}}//极坐标转换为笛卡尔坐标/*http://127.0.0.1:6688/j/13/22.61986{"x":12,"y":5}*/funcdoJ(whttp.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)	iferrR!=nil{		fmt.Println("第1个值x输入错误!")		errV=1	}else{		iferrA!=nil{			fmt.Println("第2个值Y输入错误!")			errV=2		}	}	iferrV==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")	}}funchttpHandler(){	myRouter:=mux.NewRouter().StrictSlash(true)	//笛卡尔坐标转换为极坐标	myRouter.HandleFunc("/d/{x}/{y}",doD)	//极坐标转换为笛卡尔坐标	myRouter.HandleFunc("/j/{r}/{a}",doJ)	log.Fatal(http.@R_404_6818@enAndServe(":6688",myRouter))}/*======================================================*/funcjiaodu2hudu(jiaodufloat64)float64{	returnjiaodu*math.Pi/180}funchudu2jiaodu(hudufloat64)float64{	returnhudu*180/math.Pi}funcround(numfloat64)int{	returnint(num+math.copysign(0.5,num))}functoFixed(numfloat64,precisionint)float64{	output:=math.Pow(10,float64(precision))	returnfloat64(round(num*output))/output}funcmain(){	httpHandler()	/*fireNow()*/}/*DEV:Cli使用*/funcfireNow(){	var(		ρ,θ,x,yfloat64	)	methodType:=1	fmt.Print("请选择转换方式:\n输入1,表示需要从极坐标转换为笛卡尔坐标;\n输入2,表示需要从笛卡尔坐标转换为极坐标\n?")	fmt.Scan(&methodType)	ifmethodType!=1&&methodType!=2{		fmt.Println("貌似你输入的不是1,也不是2啊,搞哪样?")		fireNow()	}else{		switchmethodType{		//输入1,表示需要从极坐标转换为笛卡尔坐标;		case1:			fmt.Println("请以极坐标格式输入点的坐标(ρ和θ之间用1个空格隔开,θ默认为弧度单位)?")			fmt.Scan(&ρ,&θ)			θ=jiaodu2hudu(θ)			x=ρ*math.Cos(θ)			y=ρ*math.Sin(θ)			fmt.Printf("x=%f,y=%f\n",y)		//输入2,表示需要从笛卡尔坐标转换为极坐标		case2:			fmt.Println("请以笛卡尔坐标格式输入点的坐标(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",ρ,θ)		}	}}


笛卡尔坐标转极坐标示例 URL

http://127.0.0.1:6688/d/12/5

将会返回

{"r":13,"a":22.61986}

极坐标转笛卡尔坐标示例URL

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

将会返回

{"x":12,"y":5}

两种转换默认精确到小数点后5位。

总结

以上是内存溢出为你收集整理的Go 语言: 极坐标与笛卡尔坐标的互转全部内容,希望文章能够帮你解决Go 语言: 极坐标与笛卡尔坐标的互转所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1275112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存