Python point on line

16 Python code examples are found related to " point on line". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: geometry.py    From RAFCON with Eclipse Public License 1.0 6 votes vote down vote up
def point_on_line(point, line_start, line_end, accuracy=50.):
    """Checks whether a point lies on a line

    The function checks whether the point "point" (P) lies on the line defined by its starting point line_start (A) and
    its end point line_end (B).
    This is done by comparing the distance of [AB] with the sum of the distances [AP] and [PB]. If the difference is
    smaller than [AB] / accuracy, the point P is assumed to be on the line. By increasing the value of accuracy (the
    default is 50), the tolerance is decreased.
    :param point: Point to be checked (tuple with x any y coordinate)
    :param line_start: Starting point of the line (tuple with x any y coordinate)
    :param line_end: End point of the line (tuple with x any y coordinate)
    :param accuracy: The higher this value, the less distance is tolerated
    :return: True if the point is one the line, False if not
    """
    length = dist(line_start, line_end)
    ds = length / float(accuracy)
    if -ds < (dist(line_start, point) + dist(point, line_end) - length) < ds:
        return True

    return False 
Example 2
Source File: Graphics.py    From nionswift with GNU General Public License v3.0 6 votes vote down vote up
def get_closest_point_on_line(start, end, p):
    c = (p[0] - start[0], p[1] - start[1])
    v = (end[0] - start[0], end[1] - start[1])
    length = math.sqrt(pow(v[0], 2) + pow(v[1], 2))
    if length > 0:
        v = (v[0] / length, v[1] / length)
        t = v[0] * c[0] + v[1] * c[1]
        if t < 0:
            return start
        if t > length:
            return end
        return (start[0] + v[0] * t, start[1] + v[1] * t)
    else:
        return start


# test whether points are close 
Example 3
Source File: util.py    From neuropythy with GNU Affero General Public License v3.0 5 votes vote down vote up
def point_on_line(ab, c, atol=1e-8):
    '''
    point_on_line((a,b), c) yields True if point x is on line (a,b) and False otherwise.
    '''
    (a,b) = ab
    abc = [np.asarray(u) for u in (a,b,c)]
    if any(len(u.shape) == 2 for u in abc): (a,b,c) = [np.reshape(u,(len(u),-1)) for u in abc]
    else:                                   (a,b,c) = abc
    vca = a - c
    vcb = b - c
    uba = czdivide(vba, np.sqrt(np.sum(vba**2, axis=0)))
    uca = czdivide(vca, np.sqrt(np.sum(vca**2, axis=0)))
    return (np.isclose(np.sqrt(np.sum(vca**2, axis=0)), 0, atol=atol) |
            np.isclose(np.sqrt(np.sum(vcb**2, axis=0)), 0, atol=atol) |
            np.isclose(np.abs(np.sum(uba*uca, axis=0)), 1, atol=atol)) 
Example 4
Source File: mathhelper.py    From catch-the-pp with GNU General Public License v3.0 5 votes vote down vote up
def point_on_line(p0, p1, length):
    full_length = pow(pow(p1.x - p0.x, 2) + pow(p1.y - p0.y, 2), 0.5)
    n = full_length - length

    if full_length == 0: #Fix for something that seems unknown... (We warn if this happens)
        print("full_length was forced to 1!")
        full_length = 1

    x = (n * p0.x + length * p1.x) / full_length
    y = (n * p0.y + length * p1.y) / full_length
    return Vec2(x, y) 
Example 5
Source File: geometry.py    From scene-representation-networks with MIT License 5 votes vote down vote up
def project_point_on_line(projection_point, line_direction, point_on_line, dim):
    '''Projects a batch of points on a batch of lines as defined by their direction and a point on each line. '''
    assert torch.allclose((line_direction ** 2).sum(dim=dim, keepdim=True).cuda(), torch.Tensor([1]).cuda())
    return point_on_line + ((projection_point - point_on_line) * line_direction).sum(dim=dim,
                                                                                     keepdim=True) * line_direction 
Example 6
Source File: bundle_edges.py    From qgis-edge-bundling with GNU General Public License v2.0 5 votes vote down vote up
def project_point_on_line(pt, line):
        """ Projects point onto line, needed for compatibility computation """
        v0 = line.vertexAt(0)
        v1 = line.vertexAt(1)
        length = max(line.length(), 10**(-6))
        r = ((v0.y() - pt.y()) * (v0.y() - v1.y()) - 
             (v0.x() - pt.x()) * (v1.x() - v0.x())) / (length**2)
        return QgsPoint(v0.x() + r * (v1.x() - v0.x()), v0.y() + r * (v1.y() - v0.y()))


# ------------------------------------ EDGE-CLUSTER  ------------------------------------ # 
Example 7
Source File: triangles_two.py    From generative-art with MIT License 5 votes vote down vote up
def point_on_line(p1, p2):
    (x1, y1) = p1
    (x2, y2) = p2
    slope = (y2 - y1)/(x2 - x1)
    x3 = random.uniform(x1, x2)
    y3 = slope * (x3 - x1) + y1
    return (x3, y3) 
Example 8
Source File: __init__.py    From pyautogui with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def getPointOnLine(x1, y1, x2, y2, n):
    """
    Returns an (x, y) tuple of the point that has progressed a proportion ``n`` along the line defined by the two
    ``x1``, ``y1`` and ``x2``, ``y2`` coordinates.

    This function was copied from pytweening module, so that it can be called even if PyTweening is not installed.
    """
    x = ((x2 - x1) * n) + x1
    y = ((y2 - y1) * n) + y1
    return (x, y) 
Example 9
Source File: delaunay.py    From Delaunay_Triangulation with ISC License 5 votes vote down vote up
def pointOnLine(p, t):
    if pointOnLine2(p, t[0], t[1]):
        return 2
    if pointOnLine2(p, t[1], t[2]):
        return 0
    if pointOnLine2(p, t[2], t[0]):
        return 1
    return -1

# prueft, ob t p1 und p2 und not p3 matched. 
Example 10
Source File: geometry.py    From scikit-hep with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def point_on_line (  self , mu ) :
        """ Get the point on line, that  corresponds to parameter 'mu'.

        Example
        -------
        >>> line = ...
        >>> p0 = line.point_on_line ( 0 ) ## should be equal to line.point
        >>> p1 = line.point_on_line ( 1 ) ## shodul be equal to line.point+line.vector
        """
        return self.point + mu * self.direction 
Example 11
Source File: patmaker.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def point_on_line(self, point, tolerance=ZERO_TOL):
        a = self.start_point
        b = self.end_point
        c = point
        if 0.0 <= abs((a.u - c.u) * (b.v - c.v)
                      - (a.v - c.v) * (b.u - c.u)) <= tolerance:
            return True
        else:
            return False 
Example 12
Source File: track.py    From fssim with MIT License 5 votes vote down vote up
def add_point_on_middle_line(self, point):

        self.control_points = np.vstack((self.control_points, point))
        self.control_points_s = np.append(self.control_points_s,
                                          self.control_points_s[-1] + np.linalg.norm(self.control_points[-2, :] -
                                                                                     self.control_points[-1, :]))
        return True

        # self._draw_line(self.control_points[-2, :], self.control_points[-1, :]) 
Example 13
Source File: __init__.py    From FreeCAD_drawing_dimensioning with GNU Lesser General Public License v2.1 5 votes vote down vote up
def pointOnLine(self, c, tol=10**-3):
        d = norm( self.endPoint.posProjection - self.startPoint.posProjection )
        d1 = norm( self.endPoint.posProjection - c )
        d2 = norm( c - self.startPoint.posProjection )
        return abs(d - (d1+d2)) < tol 
Example 14
Source File: geo_math.py    From vissim with MIT License 5 votes vote down vote up
def getNearestPointOnLine(point1, line):
    """
	point1 is a given point
	line is rep by 2 points
	find the point on line where perp drawn from point1 meets the line
    """
    # first convert line to normalized unit vector
    ptA = (np.array(line[0])).astype(np.float)
    ptB = (np.array(line[1])).astype(np.float)
    x1,y1,z1 = ptA
    x2,y2,z2 = ptB
    #
    sepVec = ptA - ptB
    x3,y3,z3 = (np.array(point1).astype(np.float))
    mag = np.sqrt(sepVec.dot(sepVec))
    dx=sepVec[0]
    dy=sepVec[1]
    dz=sepVec[2]
    dx = dx/mag
    dy = dy/mag
    dz = dz/mag
    # translate the point and get the dot product
    lambda1 = dx * (x3 - x1) 
    lambda2 = dy * (y3 - y1)
    lambda3 = dz * (z3 - z1)
    lam = lambda1 + lambda2 + lambda3
    x4 = (dx * lam) + x1;
    y4 = (dy * lam) + y1;
    z4 = (dz * lam) + z1;
    return x4,y4,z4