package common

import (
	"image/color"
	"math"
)

type Point struct {
	X, Y float64
}

// Distance traditional function
func Distance(p, q Point) float64 {
	return math.Hypot(q.X-p.X, q.Y-p.Y)
}

// Path 不同的类型可以有同名的方法, 不同类型的调用，将会指向不同的方法, 实现不同的功能
// A Path is a journey connecting the points with straight lines.
type Path []Point

// Distance returns the distance traveled along the path.
func (path Path) Distance() float64 {
	sum := 0.0
	for i := range path {
		if i > 0 {
			sum += path[i-1].Distance(path[i])
		}
	}
	return sum
}

// Distance same thing, but as a method of the Point type
func (p Point) Distance(q Point) float64 {
	return math.Hypot(q.X-p.X, q.Y-p.Y)
}

func (p *Point) ScaleBy(factor float64) {
	p.X *= factor
	p.Y *= factor
}

// ColoredPoint 这个结构体包含了Point这个结构体
// 同时也获得了Point的方法
type ColoredPoint struct {
	Point
	Color color.RGBA
}
