zoukankan      html  css  js  c++  java
  • 计算直线和直线的交点 2维

    简介

    说来简单,实现难
    使用直线的一般式方程 (Ax+By+C=0) 联立方程组求解

    TIPS 可以只用 scipy linalg 求解线性方程组

    code

    # coding=utf-8
    from scipy.optimize import fsolve #导入求解方程组的函数 非线性方程组
    import numpy as np
    from scipy import linalg
    class Point2D:
        # AX+BY+C = 0
        # 二维的一般式方程
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
    class Line2D:
        # AX+BY+C = 0
    #   # 二维的一般式方程
        def __init__(self, A, B, C):
            self.A = A
            self.B = B
            self.C = C
        def init_from_two_point2d(self, a, b):
            self.A = b.y - a.y
            self.B = a.x - b.x
            self.C = b.x * a.y - a.x * b.y
        def getABC(self):
            return self.A, self.B, self.C
    
    class Point3D:
        def __init__(self, x, y, z):
            self.x = x
            self.y = y
            self.z = z
    
    class Vec3D:
        '''
        向量
        '''
        def __init__(self, x, y, z):
            self.x = x
            self.y = y
            self.z = z
    
    
    class Line3D:
        # 点向式
        # 二维的一般式方程
        def __init__(self, a, b, c, x_0, y_0, z_0):
            self.a = a
            self.b = b
            self.c = c
            self.x_0 = x_0
            self.y_0 = y_0
            self.z_0 = z_0
    
    
    def f(x):
        A = x[0]
        B = x[1]
        C = x[2]
        AA = x[3]
        BB = x[4]
        CC = x[5]
        return [A]
    
    def intersection_line2D_line2D(line1, line2):
        A,B,C = line1.getABC()
        D,E,F = line2.getABC()
        AA = np.array([[A, B], [D, E]])
        BB = np.array([-C, -F])
        x = linalg.solve(AA, BB)
        print("[DEBUG] intersection on the point[x,y]", x)
    
    if __name__ == "__main__":
        a = Point2D(0,0)
        b = Point2D(1,1)
        c = Point2D(0,1)
        d = Point2D(1,0)
    
        l = Line2D(0,0,0)
        l.init_from_two_point2d(a, b)
        n = Line2D(0,0,0)
        n.init_from_two_point2d(c, d)
        intersection_line2D_line2D(l, n)
    

    TIPS2

    细心的小伙伴已经发现了,如果是平行的两条2维直线,或者是两条重合的二维直线,那么就会报一个错误 Matrix is singular
    需要在程序中排除

    def intersection_line2D_line2D(line1, line2):
        A,B,C = line1.getABC()
        D,E,F = line2.getABC()
        # 判断这两条直线是否是重合的 或者平行的
        w = np.array([[A, B, C], [D, E, F]])
        if(np.linalg.matrix_rank(w) != 2):
            print('[ERROR] coincide  重合')
            return
        ww = np.array([[A, B], [D, E]])
        if(np.linalg.matrix_rank(ww) == 1 and C != F):
            print('[ERROR] parallel 平行')
            return
        AA = np.array([[A, B], [D, E]])
        BB = np.array([-C, -F])
        x = linalg.solve(AA, BB)
        print("[DEBUG] intersection on the point[x,y]", x)
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    新巴巴运动网 项目第二天
    新巴巴运动网 项目第一天
    pandas.read_csv()参数(转载)
    from sklearn.datasets import make_classification创建分类数据集
    【剑指offer】08二叉树的下一个节点,C++实现
    【剑指offer】滑动窗口的最大值,C++实现
    【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
    步入element-ui踩坑记
    步入vue.js踩坑记
    nvm安装与使用
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13718737.html
Copyright © 2011-2022 走看看