From d3ebe8e5fb01015d27b5076a1430b872c779c6b0 Mon Sep 17 00:00:00 2001 From: Akash Saravanan Date: Fri, 16 Oct 2020 23:48:19 +0530 Subject: [PATCH 1/5] Add intersection of two lines. --- mathgenerator/mathgen.py | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index bd22657..a85d7c3 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -1,4 +1,5 @@ import random +import fractions genList = [] @@ -464,6 +465,60 @@ def commonFactorsFunc(maxVal=100): problem = f"Common Factors of {a} and {b} = " solution = arr return problem, solution + +def intersectionOfTwoLinesFunc( + minM=-10, maxM=10, minB=-10, maxB=10, minDenominator=1, maxDenominator=6 +): + def generateEquationString(m, b): + """ + Generates an equation given the slope and intercept. + It handles cases where m is fractional. + It also ensures that we don't have weird signs such as y = mx + -b. + """ + if m[1] == 1: + m = m[0] + else: + m = f"{m[0]}/{m[1]}" + base = f"y = {m}x" + if b > 0: + return f"{base} + {b}" + elif b < 0: + return f"{base} - {b * -1}" + else: + return base + + def fractionToString(x): + """ + Converts the given fractions.Fraction into a string. + """ + if x.denominator == 1: + x = x.numerator + else: + x = f"{x.numerator}/{x.denominator}" + return x + + m1 = (random.randint(minM, maxM), random.randint(minDenominator, maxDenominator)) + m2 = (random.randint(minM, maxM), random.randint(minDenominator, maxDenominator)) + b1 = random.randint(minB, maxB) + b2 = random.randint(minB, maxB) + equation1 = generateEquationString(m1, b1) + equation2 = generateEquationString(m2, b2) + problem = "Find the point of intersection of the two lines: " + problem += f"{equation1} and {equation2}" + m1 = fractions.Fraction(*m1) + m2 = fractions.Fraction(*m2) + # if m1 == m2 then the slopes are equal + # This can happen if both line are the same + # Or if they are parallel + # In either case there is no intersection + if m1 == m2: + solution = "No Solution" + else: + intersection_x = (b1 - b2) / (m2 - m1) + intersection_y = ((m2 * b1) - (m1 * b2)) / (m2 - m1) + solution = f"({fractionToString(intersection_x)}, {fractionToString(intersection_y)})" + return problem, solution + # || Class Instances #Format is: @@ -502,3 +557,4 @@ angleRegularPolygon = Generator("Angle of a Regular Polygon",29,"Find the angle combinations = Generator("Combinations of Objects",30, "Combinations available for picking 4 objects at a time from 6 distinct objects ="," 15", combinationsFunc) factorial = Generator("Factorial", 31, "a! = ", "b", factorialFunc) commonFactors = Generator("Common Factors", 32, "Common Factors of {a} and {b} = ","[c, d, ...]",commonFactorsFunc) +intersectionOfTwoLines = Generator("Intersection of Two Lines", 33, "Find the point of intersection of the two lines: y = m1*x + b1 and y = m2*x + b2", "(x, y)", intersectionOfTwoLinesFunc) From 3430dfeac665d86b553cc6b75edd69a75cff5a36 Mon Sep 17 00:00:00 2001 From: Akash Saravanan Date: Fri, 16 Oct 2020 23:48:19 +0530 Subject: [PATCH 2/5] Add intersection of two lines. --- mathgenerator/mathgen.py | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 917fccc..1b900ad 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -1,5 +1,9 @@ import random +<<<<<<< HEAD import math +======= +import fractions +>>>>>>> Add intersection of two lines. genList = [] @@ -531,6 +535,60 @@ def commonFactorsFunc(maxVal=100): problem = f"Common Factors of {a} and {b} = " solution = arr return problem, solution + +def intersectionOfTwoLinesFunc( + minM=-10, maxM=10, minB=-10, maxB=10, minDenominator=1, maxDenominator=6 +): + def generateEquationString(m, b): + """ + Generates an equation given the slope and intercept. + It handles cases where m is fractional. + It also ensures that we don't have weird signs such as y = mx + -b. + """ + if m[1] == 1: + m = m[0] + else: + m = f"{m[0]}/{m[1]}" + base = f"y = {m}x" + if b > 0: + return f"{base} + {b}" + elif b < 0: + return f"{base} - {b * -1}" + else: + return base + + def fractionToString(x): + """ + Converts the given fractions.Fraction into a string. + """ + if x.denominator == 1: + x = x.numerator + else: + x = f"{x.numerator}/{x.denominator}" + return x + + m1 = (random.randint(minM, maxM), random.randint(minDenominator, maxDenominator)) + m2 = (random.randint(minM, maxM), random.randint(minDenominator, maxDenominator)) + b1 = random.randint(minB, maxB) + b2 = random.randint(minB, maxB) + equation1 = generateEquationString(m1, b1) + equation2 = generateEquationString(m2, b2) + problem = "Find the point of intersection of the two lines: " + problem += f"{equation1} and {equation2}" + m1 = fractions.Fraction(*m1) + m2 = fractions.Fraction(*m2) + # if m1 == m2 then the slopes are equal + # This can happen if both line are the same + # Or if they are parallel + # In either case there is no intersection + if m1 == m2: + solution = "No Solution" + else: + intersection_x = (b1 - b2) / (m2 - m1) + intersection_y = ((m2 * b1) - (m1 * b2)) / (m2 - m1) + solution = f"({fractionToString(intersection_x)}, {fractionToString(intersection_y)})" + return problem, solution + # || Class Instances #Format is: From e2dddff7ece1dd12d7d1668dacc9406488ea3709 Mon Sep 17 00:00:00 2001 From: Akash Saravanan Date: Fri, 16 Oct 2020 23:58:34 +0530 Subject: [PATCH 3/5] fix numbering of generators --- mathgenerator/mathgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 5575303..4403964 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -640,4 +640,4 @@ volumeCylinderGen = Generator("Volume of cylinder", 37, "Volume of cylinder with surfaceAreaConeGen = Generator("Surface Area of cone", 38, "Surface area of cone with height = a units and radius = b units is","c units^2", surfaceAreaCone) volumeConeGen = Generator("Volume of cone", 39, "Volume of cone with height = a units and radius = b units is","c units^3", volumeCone) commonFactors = Generator("Common Factors", 40, "Common Factors of {a} and {b} = ","[c, d, ...]",commonFactorsFunc) -intersectionOfTwoLines = Generator("Intersection of Two Lines", 33, "Find the point of intersection of the two lines: y = m1*x + b1 and y = m2*x + b2", "(x, y)", intersectionOfTwoLinesFunc) +intersectionOfTwoLines = Generator("Intersection of Two Lines", 41, "Find the point of intersection of the two lines: y = m1*x + b1 and y = m2*x + b2", "(x, y)", intersectionOfTwoLinesFunc) From ce3ec7a3a33bd132882c11bddd1cbd881d1b9447 Mon Sep 17 00:00:00 2001 From: Luke Weiler Date: Fri, 16 Oct 2020 14:30:09 -0400 Subject: [PATCH 4/5] Update mathgen.py --- mathgenerator/mathgen.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 4403964..64e935f 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -1,13 +1,6 @@ import random -<<<<<<< HEAD -<<<<<<< HEAD import math -======= import fractions ->>>>>>> Add intersection of two lines. -======= -import fractions ->>>>>>> d3ebe8e5fb01015d27b5076a1430b872c779c6b0 genList = [] From 9890ae0edf7ff329e3f1c73171304f8f954a7504 Mon Sep 17 00:00:00 2001 From: Luke Weiler Date: Fri, 16 Oct 2020 14:30:56 -0400 Subject: [PATCH 5/5] Update mathgen.py --- mathgenerator/mathgen.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mathgenerator/mathgen.py b/mathgenerator/mathgen.py index 64e935f..940dd90 100644 --- a/mathgenerator/mathgen.py +++ b/mathgenerator/mathgen.py @@ -623,7 +623,6 @@ fractionMultiplication = Generator("Fraction Multiplication", 28, "(a/b)*(c/d)=" angleRegularPolygon = Generator("Angle of a Regular Polygon",29,"Find the angle of a regular polygon with 6 sides","120",regularPolygonAngleFunc) combinations = Generator("Combinations of Objects",30, "Combinations available for picking 4 objects at a time from 6 distinct objects ="," 15", combinationsFunc) factorial = Generator("Factorial", 31, "a! = ", "b", factorialFunc) -<<<<<<< HEAD surfaceAreaCubeGen = Generator("Surface Area of Cube", 32, "Surface area of cube with side a units is","b units^2", surfaceAreaCube) surfaceAreaCuboidGen = Generator("Surface Area of Cuboid", 33, "Surface area of cuboid with sides = a units, b units, c units is","d units^2", surfaceAreaCuboid) surfaceAreaCylinderGen = Generator("Surface Area of Cylinder", 34, "Surface area of cylinder with height = a units and radius = b units is","c units^2", surfaceAreaCylinder)