Method to return the equation of a straight line given two points

后端 未结 5 997
清歌不尽
清歌不尽 2020-12-29 06:17

I have a class Point, consisting of a point with x and y coordinates, and I have to write a method that computes and returns the equation of a straight line joi

5条回答
  •  悲&欢浪女
    2020-12-29 06:43

    I think you are making pretty advanced code, but you are making it complicated. Here is a function that can do that:

    from decimal import Decimal
    
    
    def lin_equ(l1, l2):
        """Line encoded as l=(x,y)."""
        m = Decimal((l2[1] - l1[1])) / Decimal(l2[0] - l1[0])
        c = (l2[1] - (m * l2[0]))
        return m, c
    
    # Example Usage:
    lin_equ((-40, 30,), (20, 45))
    
    # Result: (Decimal('0.25'), Decimal('40.00'))
    

提交回复
热议问题