Ladies and gentlemen, it's time for another programming challenge. This one deals with circles and lines, so some math will be required.
A picture is worth a thousand words, so I'll start with an illustration I drew.
Your mission: Find the length of the bold curve.
Input: Your program should read a file called "INPUT," which is in the following format:
x_i
y_i
x_f
y_f
n
x_1 y_1 r_1
x_2 y_2 r_2
...
x_n y_n r_n
Example:
1
10
10
1
1
5 5 4
(x_i denotes the x-coordinate of the BEGINNING of the curve; y_i denotes of y-coordinate of the BEGINNING of the curve; x_f denotes the x-coordinate of the END of the curve; y_f denotes the y-coordinate of the END of the curve; n denotes the number of circles there are; x_j denotes the x-coordinate of the center of circle j; y_j denotes the y-coordinate of the center of circle j; r_j denotes the radius of circle j.)
None of the circles touch or intersect, and all the figures belong to the first quadrant/the two axes.
Output: your program should output the length of the curve to a file called "OUTPUT."
The following math things may prove useful.
The equation of a line with slope a and y-intercept b is y = ax + b.
The slope can be found by taking two points which belong to the line, (x_1, y_1) and (x_2, y_2), and performing the following operation:
y_2 - y_1
a = ---------
x_2 - x_1
The distance between two points with coordiantes (x_1, y_1) and (x_2, y_2) equals sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2)
The standard equation of a circle of radius r with center at (x_0, y_o) is::
(x - x_0)^2 + (y - y_0)^2 = r^2
Note that any positive number has TWO square roots. E.g. sqrt(4) = {-2, 2}, not just 2.
A quadratic equation is an equation in the form ax^2 + bx + c = 0. The solution is this:
-b - sqrt(b^2 - 4ac)
x_1 = --------------------
2a
-b + sqrt(b^2 - 4ac)
x_2 = --------------------
2a
Note that if b^2 - 4ac > 0, then there are two solutions; if b^2 - 4ac = 0, there is one solution; if b^2 - 4ac < 0, then there are no real solutions. (The expression b^2 - 4ac is called the discriminate, and is usually represented by the capital letter D.)
When making algebraic manipulations, don't forget about the domain! For example, suppose you have to solve the following equation:
(sqrt(x))^2 = -15
The answer is "no solution"--NOT -15. The square root function has the domain [0, +inf)!
The formula for the length of an arc is:
s = Ar
Where A is the measure of the angle in radians, and r is the radius of the circle.
In addition, I suggest looking up the Law of Cosines (or, alternatively, Pythagorean