码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-12 23:17:56      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:go   golang   polar   

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

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

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

x = ρ* Cosθ

y = ρ* Sinθ

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

θ = Arctan(x/y)


Go语言实现

/*
* @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")
	}
}

//极坐标转换为笛卡尔坐标
/*
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)
	// 笛卡尔坐标转换为极坐标
	myRouter.HandleFunc("/d/{x}/{y}", doD)
	// 极坐标转换为笛卡尔坐标
	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("请选择转换方式:\n输入1,表示需要从极坐标转换为笛卡尔坐标;\n输入2,表示需要从笛卡尔坐标转换为极坐标\n?")
	fmt.Scan(&methodType)

	if methodType != 1 && methodType != 2 {
		fmt.Println("貌似你输入的不是1,也不是2啊,搞哪样?")
		fireNow()
	} else {
		switch methodType {

		//输入1,表示需要从极坐标转换为笛卡尔坐标;
		case 1:
			fmt.Println("请以极坐标格式输入点的坐标(ρ和 θ之间用1个空格隔开,θ默认为弧度单位)?")
			fmt.Scan(&ρ, &θ)
			θ = jiaodu2hudu(θ)
			x = ρ * math.Cos(θ)
			y = ρ * math.Sin(θ)
			fmt.Printf("x = %f, y= %f\n", x, y)
		//输入2,表示需要从笛卡尔坐标转换为极坐标
		case 2:

			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   golang   polar   

原文地址:http://13239289.blog.51cto.com/13229289/1964669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!