Intersection Point

intersection-point-two-linesThe aim of this challenge is to write a script that allows the uer to input the equation of two straight lines (Line 1: y=ax+b, Line 2: y=cx+d). The program will then calculate the coordinates of the intersection point if such a point exists!

Flowchart


intersection-point-flowchart

Your Code


Complete your code here, using the steps described on the above flowchart.

Testing


Once your code is complete, you can check it works by completing the following tests:

Test # Input Values Expected Output Actual Output
#1 a = 5
b = -10
c = -3
d = 22
(4,10)
#2 a = 0
b = 20
c = 2
d = -50
(35,20)
#3 a = -1
b = 0
c = 2
d = 0
(0,0)
#4 a = 5
b = 10
c = 5
d = 30
Both lines are parallel

Using Python Turtle to trace the lines!


The following code uses Python Turtle to trace both lines.

Extension Task


Can you write a computer program that takes the (x,y) coordinates of two different points and calculates the linear equation (y = ax + b) of the straight line joining these two dots.

To do so you will need to:

  • Retrieve four inputs: x1, y1, x2, y2 (The coordinates of the two points to join)
  • Calculate the slope of the straight line: a = (y2 – y1) / (x2 – x1)
  • Calculate b = y1 – ax1
  • Display the linear equation: y = ax + b

Note that in case of a vertical line (if x1 == x2), you will not be able to calculate the slope a. The equation of your straight line would then be x = x1.

Share Button