Normalize returns the unit vector from p, and its magnitude. if you try to normalize the null vector, the return value will be null values
()
| 151 | // Normalize returns the unit vector from p, and its magnitude. |
| 152 | // if you try to normalize the null vector, the return value will be null values |
| 153 | func (p *Point) Normalize() (Point, float32) { |
| 154 | if p.X == 0 && p.Y == 0 { |
| 155 | return *p, 0 |
| 156 | } |
| 157 | |
| 158 | mag := math.Sqrt(p.X*p.X + p.Y*p.Y) |
| 159 | unit := Point{p.X / mag, p.Y / mag} |
| 160 | |
| 161 | return unit, mag |
| 162 | } |
| 163 | |
| 164 | // Within reports whether the point is contained within the given container. |
| 165 | func (p Point) Within(c Container) bool { |