DistanceToPoint returns the distance between the rectangle and a point.
(p Point)
| 516 | |
| 517 | // DistanceToPoint returns the distance between the rectangle and a point. |
| 518 | func (r Rect) DistanceToPoint(p Point) float64 { |
| 519 | var q Point |
| 520 | if r.X0 <= p.X && p.X <= r.X1 { |
| 521 | if r.Y0 <= p.Y && p.Y <= r.Y1 { |
| 522 | // inside |
| 523 | return 0.0 |
| 524 | } else if r.Y1 < p.Y { |
| 525 | return p.Y - r.Y1 |
| 526 | } else { |
| 527 | return r.Y0 - p.Y |
| 528 | } |
| 529 | } else if r.X1 < p.X { |
| 530 | if r.Y0 <= p.Y && p.Y <= r.Y1 { |
| 531 | return p.X - r.X1 |
| 532 | } else if r.Y1 < p.Y { |
| 533 | q = Point{r.X1, r.Y1} |
| 534 | } else { |
| 535 | q = Point{r.X1, r.Y0} |
| 536 | } |
| 537 | } else { |
| 538 | if r.Y0 <= p.Y && p.Y <= r.Y1 { |
| 539 | return r.X0 - p.X |
| 540 | } else if r.Y1 < p.Y { |
| 541 | q = Point{r.X0, r.Y1} |
| 542 | } else { |
| 543 | q = Point{r.X0, r.Y0} |
| 544 | } |
| 545 | } |
| 546 | return p.Sub(q).Length() |
| 547 | } |
| 548 | |
| 549 | func (r Rect) ContainsLine(a, b Point) bool { |
| 550 | return r.ContainsPoint(a) && r.ContainsPoint(b) |