// ClampPoint returns the closest point in the rectangle to the given point.
// The rectangle must be non-empty.
551130
func (r Rect) ClampPoint(p Point) Point {
	return Point{r.X.ClampPoint(p.X), r.Y.ClampPoint(p.Y)}
}
187712
func clamp(x float64) uint8 {
	v := int64(x + 0.5)
	if v > 255 {
		return 255
	}
	if v > 0 {
		return uint8(v)
	}
	return 0
}
1104277
func (region *Region) ContainsPoint(x, y int) bool {
	ret := C.cairo_region_contains_point(region.Ptr, C.int(x), C.int(y)) != 0
	if err := region.status(); err != nil {
		panic(err)
	}
	return ret
}
1055582
func (r Rect) AddPoint(p Point) Rect {
	return Rect{r.X.AddPoint(p.X), r.Y.AddPoint(p.Y)}
}
297022
func ClipEdge(a, b r2.Point, clip r2.Rect) (aClip, bClip r2.Point, intersects bool) {
	// Compute the bounding rectangle of AB, clip it, and then extract the new
	// endpoints from the clipped bound.
	bound := r2.RectFromPoints(a, b)
	if bound, intersects = clipEdgeBound(a, b, clip, bound); !intersects {
		return aClip, bClip, false
	}
	ai := 0
	if a.X > b.X {
		ai = 1
	}
	aj := 0
	if a.Y > b.Y {
		aj = 1
	}

	return bound.VertexIJ(ai, aj), bound.VertexIJ(1-ai, 1-aj), true
}
352731
func (m map3857) point3857(p *Point) (err error) {
	// map crosses 180 and the point is to the right
	switch m.crossesCentral && p.Longitude > -180.0 && p.Longitude < 0.0 {
	case true:
		err = db.QueryRow(svgPointQuery, p.Longitude, p.Latitude, width3857-m.llx, m.yshift, m.dx, m.dx).Scan(&p.x, &p.y)
	case false:
		err = db.QueryRow(svgPointQuery, p.Longitude, p.Latitude, m.xshift, m.yshift, m.dx, m.dx).Scan(&p.x, &p.y)
	}

	return
}
38172
func (c Cap) ContainsPoint(p Point) bool {
	return c.center.Sub(p.Vector).Norm2() <= 2*c.height
}
895473
func (g *Geometry) Point(n int) (*Geometry, error) {
	return geomFromC("Point", cGEOSGeomGetPointN(g.g, C.int(n)))
}
928742
func RectFromPoints(pts ...Point) Rect {
	// Because the default value on interval is 0,0, we need to manually
	// define the interval from the first point passed in as our starting
	// interval, otherwise we end up with the case of passing in
	// Point{0.2, 0.3} and getting the starting Rect of {0, 0.2}, {0, 0.3}
	// instead of the Rect {0.2, 0.2}, {0.3, 0.3} which is not correct.
	if len(pts) == 0 {
		return Rect{}
	}

	r := Rect{
		X: r1.Interval{Lo: pts[0].X, Hi: pts[0].X},
		Y: r1.Interval{Lo: pts[0].Y, Hi: pts[0].Y},
	}

	for _, p := range pts[1:] {
		r = r.AddPoint(p)
	}
	return r
}
340757
func (dc *Context) GetCurrentPoint() (Point, bool) {
	if dc.hasCurrent {
		return dc.current, true
	}
	return Point{}, false
}
998192
func (p *Polygon) ContainsPoint(point Point) bool {
	// NOTE: A bounds check slows down this function by about 50%. It is
	// worthwhile only when it might allow us to delay building the index.
	if !p.index.IsFresh() && !p.bound.ContainsPoint(point) {
		return false
	}

	// For small polygons, and during initial construction, it is faster to just
	// check all the crossing.
	const maxBruteForceVertices = 32
	if p.numVertices < maxBruteForceVertices || p.index == nil {
		inside := false
		for _, l := range p.loops {
			// use loops bruteforce to avoid building the index on each loop.
			inside = inside != l.bruteForceContainsPoint(point)
		}
		return inside
	}

	// Otherwise, look up the ShapeIndex cell containing this point.
	it := p.index.Iterator()
	if !it.LocatePoint(point) {
		return false
	}

	return p.iteratorContainsPoint(it, point)
}
124085
func (matrix *Matrix) TransformPoint(x, y *float64) {
	C.cairo_matrix_transform_point((*C.cairo_matrix_t)(unsafe.Pointer(matrix)), (*C.double)(unsafe.Pointer(x)), (*C.double)(unsafe.Pointer(y)))
}
391226
func (lsf *LeastSquaresFunction) AppendPoint(x, y float64) {
	p := NewPoint(x, y)
	if lsf.count == 0 {
		lsf.xBar = p.X()
		lsf.yBar = p.Y()
	} else {
		dx := p.X() - lsf.xBar
		dy := p.Y() - lsf.yBar

		lsf.sumXX += dx * dx * float64(lsf.count) / float64(lsf.count+1.0)
		lsf.sumYY += dy * dy * float64(lsf.count) / float64(lsf.count+1.0)
		lsf.sumXY += dx * dy * float64(lsf.count) / float64(lsf.count+1.0)

		lsf.xBar += dx / float64(lsf.count+1.0)
		lsf.yBar += dy / float64(lsf.count+1.0)
	}
	lsf.sumX += p.X()
	lsf.sumY += p.Y()
	lsf.count++
}
832291
func NewBoundAroundPoint(center orb.Point, distance float64) orb.Bound {
	radDist := distance / orb.EarthRadius
	radLat := deg2rad(center[1])
	radLon := deg2rad(center[0])
	minLat := radLat - radDist
	maxLat := radLat + radDist

	var minLon, maxLon float64
	if minLat > minLatitude && maxLat < maxLatitude {
		deltaLon := math.Asin(math.Sin(radDist) / math.Cos(radLat))
		minLon = radLon - deltaLon
		if minLon < minLongitude {
			minLon += 2 * math.Pi
		}
		maxLon = radLon + deltaLon
		if maxLon > maxLongitude {
			maxLon -= 2 * math.Pi
		}
	} else {
		minLat = math.Max(minLat, minLatitude)
		maxLat = math.Min(maxLat, maxLatitude)
		minLon = minLongitude
		maxLon = maxLongitude
	}

	return orb.Bound{
		Min: orb.Point{rad2deg(minLon), rad2deg(minLat)},
		Max: orb.Point{rad2deg(maxLon), rad2deg(maxLat)},
	}
}
607364
func Midpoint(φ1, λ1, φ2, λ2 float64) Point {
	var point = Point{}

	φ1 = Deg2Rad(φ1)
	λ1 = Deg2Rad(λ1)
	φ2 = Deg2Rad(φ2)

	Δλ := Deg2Rad(λ2) - λ1

	Bx := math.Cos(φ2) * math.Cos(Δλ)
	By := math.Cos(φ2) * math.Sin(Δλ)

	point.lat = Rad2Deg(math.Atan2(math.Sin(φ1)+math.Sin(φ2), math.Sqrt(math.Pow(math.Cos(φ1)+Bx, 2)+math.Pow(By, 2))))
	point.lon = Rad2Deg(λ1 + math.Atan2(By, math.Cos(φ1)+Bx))

	return point
}
556723
func (l *Loop) ContainsPoint(p Point) bool {
	// Empty and full loops don't need a special case, but invalid loops with
	// zero vertices do, so we might as well handle them all at once.
	if len(l.vertices) < 3 {
		return l.originInside
	}

	// For small loops, and during initial construction, it is faster to just
	// check all the crossing.
	const maxBruteForceVertices = 32
	if len(l.vertices) < maxBruteForceVertices || l.index == nil {
		return l.bruteForceContainsPoint(p)
	}

	// Otherwise, look up the point in the index.
	it := l.index.Iterator()
	if !it.LocatePoint(p) {
		return false
	}
	return l.iteratorContainsPoint(it, p)
}
772891
func (cu *CellUnion) ContainsPoint(p Point) bool {
	return cu.ContainsCell(CellFromPoint(p))
}