Given a point and a line segment, return the vector from the point to
    the closest point on the segment.
340909
def point_to_line(point, segment_start, segment_end):
    
    # TODO: Needs unittests.

    segment_vec = segment_end - segment_start
    # t is distance along line
    t = -(segment_start - point).dot(segment_vec) / (
          segment_vec.length_squared())

    closest_point = segment_start + scale_v3(segment_vec, t)
    return point - closest_point
1185720
function(startPoint, endPoint) {
                startPoint = vector.create(startPoint).to3D();
                endPoint = vector.create(endPoint).to3D();
                if (startPoint === null || endPoint === null) { return null; }
                this.line = line.create(startPoint, endPoint.subtract(startPoint));
                this.start = startPoint;
                this.end = endPoint;
                return this;
              }
909706
def get_point(cls, idx, size):
        
        x, y = cls.POSITION[idx % 4]
        idx //= 4
        block_size = 2
        while block_size < size:
            block_idx = idx % 4
            x, y = cls.get_point_in_block(x, y, block_idx, block_size)
            idx //= 4
            block_size *= 2
        return x, y
728242
def moveTo(self, vector):
        
        self._position = vector
        if self.isPenDown(): 
            self._pointsOfPolyline.append(self._position)
944943
public void getClosestPoint(Vector2f point, Vector2f result) {
		loc.set(point);
		loc.sub(start);

		float projDistance = vec.dot(loc);

		projDistance /= vec.lengthSquared();

		if (projDistance < 0) {
			result.set(start);
			return;
		}
		if (projDistance > 1) {
			result.set(end);
			return;
		}

		result.x = start.getX() + projDistance * vec.getX();
		result.y = start.getY() + projDistance * vec.getY();
	}
618009
public Vector getMidpoint(Vector other) {
        double x = (this.x + other.x) / 2;
        double y = (this.y + other.y) / 2;
        double z = (this.z + other.z) / 2;
        return new Vector(x, y, z);
    }
442479
def nearestPoint(self, pos):
        
        # lookup subpaths
        for x, y, path in self._subpaths:
            if path.contains(pos):
                return (x, y)
        
        return (None, None)
1169254
def intersect (self, line):
     
     eps   = 1e-8
     d     = (line.q - line.p)
     dn    = d.dot(self.n)
     point = None

     if abs(dn) >= eps:
       mu = self.n.dot(self.p - line.p) / dn
       if mu >= 0 and mu <= 1:
         point = line.p + mu * d

     return point
754448
def tValueForPoint(self, point):
        
        if self.segmentType == "curve":
            on1 = self.previousOnCurve
            off1 = self.points[0].coordinates
            off2 = self.points[1].coordinates
            on2 = self.points[2].coordinates
            return _tValueForPointOnCubicCurve(point, (on1, off1, off2, on2))
        elif self.segmentType == "line":
            return _tValueForPointOnLine(point, (self.previousOnCurve, self.points[0].coordinates))
        elif self.segmentType == "qcurve":
            raise NotImplementedError
        else:
            raise NotImplementedError
736207
private static Point getEllipseIntersection(Shape shape,
                                                Line2D.Double line) {
        double angle = Math.atan2(line.y2 - line.y1,
                                  line.x2 - line.x1);
        double x = shape.getBounds2D().getWidth() / 2 * Math.cos(angle) + shape.getBounds2D().getCenterX();
        double y = shape.getBounds2D().getHeight() / 2 * Math.sin(angle) + shape.getBounds2D().getCenterY();
        Point p = new Point();
        p.setLocation(x,
                      y);
        return p;
    }
1024660
function findSquaredDistanceFromSegmentToPoint(segment, point) {
  findClosestPointOnSegmentToPoint(_segmentDistance_tmpVecA, segment, point);
  return vec3.squaredDistance(_segmentDistance_tmpVecA, point);
}
591020
int closestPoint( Point2D_F64 target ) {
		double bestDistance = Double.MAX_VALUE;
		int bestIndex = -1;
		for (int i = 0; i < contour.size(); i++) {
			Point2D_I32 c = contour.get(i);

			double d = UtilPoint2D_F64.distanceSq(target.x,target.y,c.x,c.y);
			if( d < bestDistance ) {
				bestDistance = d;
				bestIndex = i;
			}
		}
		return bestIndex;
	}
455838
def closest_point(time)
      segment = segments.select { |s| s.contains_time?(time) }
      segment.first
    end
893607
def guess_segments_lines(segments, lines, nearline_tolerance=5.0):
    
    ys = segments[:, 1]
    closeness = numpy.abs(numpy.subtract.outer(ys, lines))  # each row a y, each collumn a distance to each line
    line_of_y = numpy.argmin(closeness, axis=1)
    distance = numpy.min(closeness, axis=1)
    bad = distance > numpy.mean(distance) + nearline_tolerance * numpy.std(distance)
    line_of_y[bad] = -1
    return line_of_y
1008078
public float distanceSquared(Vector2f point) {
		getClosestPoint(point, closest);
		closest.sub(point);

		float result = closest.lengthSquared();

		return result;
	}
1107282
def to_vec4(self, isPoint):
        """"""
        vec4 = Vector4()
        vec4.x = self.x
        vec4.y = self.y
        vec4.z = self.z
        if isPoint:
            vec4.w = 1
        else:
            vec4.w = 0

        return vec4
355917
function(p, opt) {

            var segments = this.segments;
            var numSegments = segments.length;
            if (numSegments === 0) return null; // if segments is an empty array

            opt = opt || {};
            var precision = (opt.precision === undefined) ? this.PRECISION : opt.precision;
            var segmentSubdivisions = (opt.segmentSubdivisions === undefined) ? this.getSegmentSubdivisions({ precision: precision }) : opt.segmentSubdivisions;
            // not using localOpt

            var closestPointT;
            var minSquaredDistance = Infinity;
            for (var i = 0; i < numSegments; i++) {

                var segment = segments[i];
                var subdivisions = segmentSubdivisions[i];

                if (segment.isVisible) {
                    var segmentClosestPointT = segment.closestPointT(p, { precision: precision, subdivisions: subdivisions });
                    var segmentClosestPoint = segment.pointAtT(segmentClosestPointT);
                    var squaredDistance = (new Line(segmentClosestPoint, p)).squaredLength();

                    if (squaredDistance < minSquaredDistance) {
                        closestPointT = { segmentIndex: i, value: segmentClosestPointT };
                        minSquaredDistance = squaredDistance;
                    }
                }
            }

            if (closestPointT) return closestPointT;

            // if no visible segment, return end of last segment
            return { segmentIndex: numSegments - 1, value: 1 };
        }