用Python求解已知两条线段的顶点坐标,求两条线的夹角的方法

版权声明:原创 https://blog.csdn.net/qq_41058594/article/details/85887433

比如向量a和向量b,那么cos=ab/|ab|.

# -*- coding: utf-8 -*-
"""
Created on Sat Jan  5 14:30:10 2019

@author: dell
"""

#两条线段的夹角的计算
#向量思维

import math
import numpy as np

#得到向量的坐标以及向量的模长
class Point(object):
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

    def vector(self):
        c = (self.x1 - self.x2, self.y1 - self.y2)
        return c

    def length(self):
        d = math.sqrt(pow((self.x1 - self.x2), 2) + pow((self.y1 - self.y2), 2))
        return d

#计算向量夹角
class Calculate(object):
    def __init__(self, x, y, m, n):
        self.x = x
        self.y = y
        self.m = m
        self.n = n

    def Vector_multiplication(self):
        self.mu = np.dot(self.x, self.y)
        return self.mu

    def Vector_model(self):
        self.de = self.m * self.n
        return self.de

    def cal(self):
        result = Calculate.Vector_multiplication(self) / Calculate.Vector_model(self)
        return result

if __name__ == '__main__':

    first_point = Point(1, 2, 3, 2)
    two_point = Point(3, 5, 4, 2)
    ca = Calculate(first_point.vector(), two_point.vector(), first_point.length(), two_point.length())
    print('两向量的夹角', ca.cal())

猜你喜欢

转载自blog.csdn.net/qq_41058594/article/details/85887433