Created
March 22, 2022 20:44
-
-
Save prendradjaja/c56b5da470f392e2a269aa8519248ce7 to your computer and use it in GitHub Desktop.
Linear equation in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class Line: | |
def __init__(self, m, b): | |
self.m = m | |
self.b = b | |
def __call__(self, x): | |
return self.m * x + self.b | |
@classmethod | |
def from_points(cls, p1, p2): | |
x1, y1 = p1 | |
x2, y2 = p2 | |
m = (y2 - y1) / (x2 - x1) | |
b = y1 - m * x1 | |
return cls(m, b) | |
@classmethod | |
def from_point_and_slope(cls, point, m): | |
b = point.y - m * point.x | |
return cls(m, b) | |
def __repr__(self): | |
m = self.m | |
b = self.b | |
return f'Line({m=}, {b=})' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment