// Distance reports the distance from the cell to the given point. Returns zero if
// the point is inside the cell.
500785
func (c Cell) Distance(target Point) s1.ChordAngle {
	return c.distanceInternal(target, true)
}
521033
func (address *Address) Distance(address2 *Address) AddressDistance {
	return (address.value).Distance(address2.value)
}
530888
func sin2Distance(x, y Point) (sin2, err float64) {
	// The (x-y).Cross(x+y) trick eliminates almost all of error due to x
	// and y being not quite unit length. This method is extremely accurate
	// for small distances; the *relative* error in the result is O(dblError) for
	// distances as small as dblError.
	n := x.Sub(y.Vector).Cross(x.Add(y.Vector))
	sin2 = 0.25 * n.Norm2()
	err = ((21+4*math.Sqrt(3))*dblError*sin2 +
		32*math.Sqrt(3)*dblError*dblError*math.Sqrt(sin2) +
		768*dblError*dblError*dblError*dblError)
	return sin2, err
}
1081856
func (a *Location) Distance(b *Location) float64 {
	if a.DataCenter != b.DataCenter {
		return 1000
	}
	if a.Rack != b.Rack {
		return 100
	}
	if a.Server != b.Server {
		return 10
	}
	return 1
}
541260
func (c Cell) distanceInternal(targetXYZ Point, toInterior bool) s1.ChordAngle {
	// All calculations are done in the (u,v,w) coordinates of this cell's face.
	target := faceXYZtoUVW(int(c.face), targetXYZ)

	// Compute dot products with all four upward or rightward-facing edge
	// normals. dirIJ is the dot product for the edge corresponding to axis
	// I, endpoint J. For example, dir01 is the right edge of the Cell
	// (corresponding to the upper endpoint of the u-axis).
	dir00 := target.X - target.Z*c.uv.X.Lo
	dir01 := target.X - target.Z*c.uv.X.Hi
	dir10 := target.Y - target.Z*c.uv.Y.Lo
	dir11 := target.Y - target.Z*c.uv.Y.Hi
	inside := true
	if dir00 < 0 {
		inside = false // Target is to the left of the cell
		if c.vEdgeIsClosest(target, false) {
			return edgeDistance(-dir00, c.uv.X.Lo)
		}
	}
	if dir01 > 0 {
		inside = false // Target is to the right of the cell
		if c.vEdgeIsClosest(target, true) {
			return edgeDistance(dir01, c.uv.X.Hi)
		}
	}
	if dir10 < 0 {
		inside = false // Target is below the cell
		if c.uEdgeIsClosest(target, false) {
			return edgeDistance(-dir10, c.uv.Y.Lo)
		}
	}
	if dir11 > 0 {
		inside = false // Target is above the cell
		if c.uEdgeIsClosest(target, true) {
			return edgeDistance(dir11, c.uv.Y.Hi)
		}
	}
	if inside {
		if toInterior {
			return s1.ChordAngle(0)
		}
		// Although you might think of Cells as rectangles, they are actually
		// arbitrary quadrilaterals after they are projected onto the sphere.
		// Therefore the simplest approach is just to find the minimum distance to
		// any of the four edges.
		return minChordAngle(edgeDistance(-dir00, c.uv.X.Lo),
			edgeDistance(dir01, c.uv.X.Hi),
			edgeDistance(-dir10, c.uv.Y.Lo),
			edgeDistance(dir11, c.uv.Y.Hi))
	}

	// Otherwise, the closest point is one of the four cell vertices. Note that
	// it is *not* trivial to narrow down the candidates based on the edge sign
	// tests above, because (1) the edges don't meet at right angles and (2)
	// there are points on the far side of the sphere that are both above *and*
	// below the cell, etc.
	return minChordAngle(c.vertexChordDist2(target, false, false),
		c.vertexChordDist2(target, true, false),
		c.vertexChordDist2(target, false, true),
		c.vertexChordDist2(target, true, true))
}
108328
func Distance2D(c1, c2 geom.Coord) float64 {
	dx := c1[0] - c2[0]
	dy := c1[1] - c2[1]

	return math.Sqrt(dx*dx + dy*dy)
}
39242
func DistanceFraction(x, a, b Point) float64 {
	d0 := x.Angle(a.Vector)
	d1 := x.Angle(b.Vector)
	return float64(d0 / (d0 + d1))
}
88895
func DistanceFromSegmentSquared(a, b, point orb.Point) float64 {
	x := a[0]
	y := a[1]
	dx := b[0] - x
	dy := b[1] - y

	if dx != 0 || dy != 0 {
		t := ((point[0]-x)*dx + (point[1]-y)*dy) / (dx*dx + dy*dy)

		if t > 1 {
			x = b[0]
			y = b[1]
		} else if t > 0 {
			x += dx * t
			y += dy * t
		}
	}

	dx = point[0] - x
	dy = point[1] - y

	return dx*dx + dy*dy
}
519843
func (p *Point) GeoDistanceFrom(point *Point, haversine ...bool) float64 {
	dLat := deg2rad(point.Lat() - p.Lat())
	dLng := math.Abs(deg2rad(point.Lng() - p.Lng()))

	if yesHaversine(haversine) {
		// yes trig functions
		dLat2Sin := math.Sin(dLat / 2)
		dLng2Sin := math.Sin(dLng / 2)
		a := dLat2Sin*dLat2Sin + math.Cos(deg2rad(p.Lat()))*math.Cos(deg2rad(point.Lat()))*dLng2Sin*dLng2Sin

		return 2.0 * EarthRadius * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
	}

	if dLng > math.Pi {
		dLng = 2*math.Pi - dLng
	}

	// fast way using pythagorean theorem on an equirectangular projection
	x := dLng * math.Cos(deg2rad((p.Lat()+point.Lat())/2.0))
	return math.Sqrt(dLat*dLat+x*x) * EarthRadius
}
418585
func (p *Path) GeoDistance(haversine ...bool) float64 {
	yesgeo := yesHaversine(haversine)
	sum := 0.0

	loopTo := len(p.PointSet) - 1
	for i := 0; i < loopTo; i++ {
		sum += p.PointSet[i].GeoDistanceFrom(&p.PointSet[i+1], yesgeo)
	}

	return sum
}
77862
func (p Point) String() string {
	return fmt.Sprintf("Point(%f, %f)", p.Latitude, p.Longitude)
}
341313
func DistanceFromPointToLine(p, lineStart, lineEnd geom.Coord) float64 {
	// if start = end, then just compute distance to one of the endpoints
	if lineStart[0] == lineEnd[0] && lineStart[1] == lineEnd[1] {
		return internal.Distance2D(p, lineStart)
	}

	// otherwise use comp.graphics.algorithms Frequently Asked Questions method

	// (1) r = AC dot AB
	//         ---------
	//         ||AB||^2
	//
	// r has the following meaning:
	//   r=0 P = A
	//   r=1 P = B
	//   r<0 P is on the backward extension of AB
	//   r>1 P is on the forward extension of AB
	//   0<r<1 P is interior to AB

	len2 := (lineEnd[0]-lineStart[0])*(lineEnd[0]-lineStart[0]) + (lineEnd[1]-lineStart[1])*(lineEnd[1]-lineStart[1])
	r := ((p[0]-lineStart[0])*(lineEnd[0]-lineStart[0]) + (p[1]-lineStart[1])*(lineEnd[1]-lineStart[1])) / len2

	if r <= 0.0 {
		return internal.Distance2D(p, lineStart)
	}
	if r >= 1.0 {
		return internal.Distance2D(p, lineEnd)
	}

	// (2) s = (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
	//         -----------------------------
	//                    L^2
	//
	// Then the distance from C to P = |s|*L.
	//
	// This is the same calculation as {@link #distancePointLinePerpendicular}.
	// Unrolled here for performance.
	s := ((lineStart[1]-p[1])*(lineEnd[0]-lineStart[0]) - (lineStart[0]-p[0])*(lineEnd[1]-lineStart[1])) / len2
	return math.Abs(s) * math.Sqrt(len2)
}
715303
func DistanceLineToLine(line1Start, line1End, line2Start, line2End geom.Coord) float64 {
	/**
	 * This calculation is susceptible to roundoff errors when
	 * passed large ordinate values.
	 * It may be possible to improve this by using {@link DD} arithmetic.
	 */
	if Equals(line1Start, line1End) {
		return DistancePointToLine(line1Start, line2Start, line2End)
	}
	if Equals(line2Start, line1End) {
		return DistancePointToLine(line2Start, line1Start, line1End)
	}

	/**
	 * Algorithm derived from http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
	 */
	a := VectorDot(line1Start, line1End, line1Start, line1End)
	b := VectorDot(line1Start, line1End, line2Start, line2End)
	c := VectorDot(line2Start, line2End, line2Start, line2End)
	d := VectorDot(line1Start, line1End, line2Start, line1Start)
	e := VectorDot(line2Start, line2End, line2Start, line1Start)

	denom := a*c - b*b
	if math.IsNaN(denom) {
		panic("Ordinates must not be NaN")
	}

	var s, t float64
	if denom <= 0.0 {
		/**
		 * The lines are parallel.
		 * In this case solve for the parameters s and t by assuming s is 0.
		 */
		s = 0
		// choose largest denominator for optimal numeric conditioning
		if b > c {
			t = d / b
		} else {
			t = e / c
		}
	} else {
		s = (b*e - c*d) / denom
		t = (a*e - b*d) / denom
	}
	switch {
	case s < 0:
		return DistancePointToLine(line1Start, line2Start, line2End)
	case s > 1:
		return DistancePointToLine(line1End, line2Start, line2End)
	case t < 0:
		return DistancePointToLine(line2Start, line1Start, line1End)
	case t > 1:
		return DistancePointToLine(line2End, line1Start, line1End)
	}
	/**
	 * The closest points are in interiors of segments,
	 * so compute them directly
	 */
	x1 := line1Start[0] + s*(line1End[0]-line1Start[0])
	y1 := line1Start[1] + s*(line1End[1]-line1Start[1])
	z1 := line1Start[2] + s*(line1End[2]-line1Start[2])

	x2 := line2Start[0] + t*(line2End[0]-line2Start[0])
	y2 := line2Start[1] + t*(line2End[1]-line2Start[1])
	z2 := line2Start[2] + t*(line2End[2]-line2Start[2])

	// length (p1-p2)
	return Distance(geom.Coord{x1, y1, z1}, geom.Coord{x2, y2, z2})
}
22015
func (p *Point) PointDistanceSquared(p2 Point) float32 {
	return (p.X-p2.X)*(p.X-p2.X) + (p.Y-p2.Y)*(p.Y-p2.Y)
}
1079924
func Distance(point1, point2 geom.Coord) float64 {
	// default to 2D distance if either Z is not set
	if math.IsNaN(point1[2]) || math.IsNaN(point2[2]) {
		return xy.Distance(point1, point2)
	}

	dx := point1[0] - point2[0]
	dy := point1[1] - point2[1]
	dz := point1[2] - point2[2]
	return math.Sqrt(dx*dx + dy*dy + dz*dz)
}
895473
func (g *Geometry) Point(n int) (*Geometry, error) {
	return geomFromC("Point", cGEOSGeomGetPointN(g.g, C.int(n)))
}
488043
func (l *Line) DistanceFrom(point *Point) float64 {
	// yes duplicate code, but saw a 15% performance increase by removing the function call
	// return math.Sqrt(l.SquaredDistanceFrom(point))
	x := l.a[0]
	y := l.a[1]
	dx := l.b[0] - x
	dy := l.b[1] - y

	if dx != 0 || dy != 0 {
		t := ((point[0]-x)*dx + (point[1]-y)*dy) / (dx*dx + dy*dy)

		if t > 1 {
			x = l.b[0]
			y = l.b[1]
		} else if t > 0 {
			x += dx * t
			y += dy * t
		}
	}

	dx = point[0] - x
	dy = point[1] - y

	return math.Sqrt(dx*dx + dy*dy)
}