MCPcopy Create free account
hub / github.com/tdewolff/canvas / ClosestPoint

Method ClosestPoint

util.go:488–515  ·  view source on GitHub ↗

ClosestPoint returns a point in the rectangle closest to the given point.

(p Point)

Source from the content-addressed store, hash-verified

486
487// ClosestPoint returns a point in the rectangle closest to the given point.
488func (r Rect) ClosestPoint(p Point) Point {
489 if r.X0 <= p.X && p.X <= r.X1 {
490 if r.Y0 <= p.Y && p.Y <= r.Y1 {
491 // inside
492 return p
493 } else if r.Y1 < p.Y {
494 return Point{p.X, r.Y1}
495 } else {
496 return Point{p.X, r.Y0}
497 }
498 } else if r.X1 < p.X {
499 if r.Y0 <= p.Y && p.Y <= r.Y1 {
500 return Point{r.X1, p.Y}
501 } else if r.Y1 < p.Y {
502 return Point{r.X1, r.Y1}
503 } else {
504 return Point{r.X1, r.Y0}
505 }
506 } else {
507 if r.Y0 <= p.Y && p.Y <= r.Y1 {
508 return Point{r.X0, p.Y}
509 } else if r.Y1 < p.Y {
510 return Point{r.X0, r.Y1}
511 } else {
512 return Point{r.X0, r.Y0}
513 }
514 }
515}
516
517// DistanceToPoint returns the distance between the rectangle and a point.
518func (r Rect) DistanceToPoint(p Point) float64 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected