point.go 901 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Point struct {
  7. X, Y float64
  8. }
  9. type Point3 struct {
  10. X, Y, Z float64
  11. }
  12. type Polar struct {
  13. R, T float64
  14. }
  15. func Abs(p *Point) float64 {
  16. return math.Sqrt(float64(p.X*p.X + p.Y*p.Y))
  17. }
  18. func Scale(p *Point, s float64) (q Point) {
  19. q.X = p.X * s
  20. q.Y = p.Y * s
  21. return
  22. }
  23. func main() {
  24. p1 := new(Point)
  25. p1.X = 3
  26. p1.Y = 4
  27. fmt.Printf("The length of the vector p1 is: %f\n", Abs(p1))
  28. p2 := &Point{4, 5}
  29. fmt.Printf("The length of the vector p2 is: %f\n", Abs(p2))
  30. q := Scale(p1, 5)
  31. fmt.Printf("The length of the vector q is: %f\n", Abs(&q))
  32. fmt.Printf("Point p1 scaled by 5 has the following coordinates: X %f - Y %f", q.X, q.Y)
  33. }
  34. /* Output:
  35. The length of the vector p1 is: 5.000000
  36. The length of the vector p2 is: 6.403124
  37. The length of the vector q is: 25.000000
  38. Point p1 scaled by 5 has the following coordinates: X 15.000000 - Y 20.000000
  39. */