diff --git a/mathgenerator/funcs/geometry/__init__.py b/mathgenerator/funcs/geometry/__init__.py index 57e66ec..cf0703b 100644 --- a/mathgenerator/funcs/geometry/__init__.py +++ b/mathgenerator/funcs/geometry/__init__.py @@ -9,6 +9,7 @@ from .basic_trigonometry import * from .circumference import * from .curved_surface_area_cylinder import * from .degree_to_rad import * +from .equation_of_line_from_two_points import * from .fourth_angle_of_quadrilateral import * from .perimeter_of_polygons import * from .pythagorean_theorem import * diff --git a/mathgenerator/funcs/geometry/equation_of_line_from_two_points.py b/mathgenerator/funcs/geometry/equation_of_line_from_two_points.py new file mode 100644 index 0000000..002e708 --- /dev/null +++ b/mathgenerator/funcs/geometry/equation_of_line_from_two_points.py @@ -0,0 +1,50 @@ +from .__init__ import * + + +def gen_func(maxCoordinate=20, minCoordinate=-20, format='string'): + def greatest_common_divisor(num1,num2): + if num2==0: + return num1 + else: + return greatest_common_divisor(num2 , num1%num2) + + x1 = random.randint(minCoordinate, maxCoordinate) + x2 = random.randint(minCoordinate, maxCoordinate) + + y1 = random.randint(minCoordinate, maxCoordinate) + y2 = random.randint(minCoordinate, maxCoordinate) + + coeff_y = (x2-x1) + coeff_x = (y2-y1) + constant = y2*coeff_y - x2*coeff_x + + gcd = greatest_common_divisor(abs(coeff_x), abs(coeff_y)) + + if gcd !=1: + if coeff_y > 0: coeff_y //= gcd + if coeff_x > 0: coeff_x //= gcd + if constant > 0: constant //= gcd + if coeff_y < 0: coeff_y = -(-coeff_y//gcd) + if coeff_x < 0: coeff_x = -(-coeff_x//gcd) + if constant < 0: constant = -(-constant//gcd) + if coeff_y < 0 : + coeff_y = -(coeff_y) + coeff_x = -(coeff_x) + constant = -(constant) + + if format == 'string': + problem = f"What is the equation of the line between points ({x1},{y1}) and ({x2},{y2}) in slope-intercept form?" + if constant > 0: + solution = str(coeff_y)+"y = " + str(coeff_x)+ "x + " + str(constant) + else: + solution = str(coeff_y)+"y = " + str(coeff_x)+ "x - " + str(-constant) + return problem, solution + elif format == 'latex': + return 'Latex unavailable' + else: + return x1, x2, y1, y2, coeff_x, coeff_y, constant + + +equation_of_line_from_two_points = Generator( + "Equation of line from two points", , gen_func, + ["maxCoordinate=20", "minCoordinate=-20"])