Golang程序 来计算顶点的绝对值和比例
例子
Abs(x, y) => √(x)2 +(y)2
Scale(f) => (x*f, y*f)
处理方法
-
将顶点定义为一个结构。
-
用一些 x 和值初始化顶点。
-
定义一个方法来计算
absolute(x, y)
-
定义一个方法来计算
scale(x*f, y*f)
实例
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func Abs(v Vertex) float64{
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func Scale(v *Vertex, f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func main() {
v := Vertex{3, 4}
fmt.Println("Given vertex is: ", v)
fmt.Println("Absolute value of given vertex is: ", Abs(v))
f := 10.0
fmt.Printf("After scaling by %0.1f new vertex is: ", f)
Scale(&v, f)
fmt.Println(v)
fmt.Println("Absolute value of given vertex is: ", Abs(v))
}
输出
Given vertex is: {3 4}
Absolute value of given vertex is: 5
After scaling by 10.0 new vertex is: {30 40}
Absolute value of given vertex is: 50