Editorial for DMOPC '19 Contest 6 P1 - Grade 9 Math


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Tzak

As vertical lines have to be considered, it is best to avoid slope-intercept form and instead use the standard form of the line Ax+By+C=0 to describe the lines in the input. To determine the coefficients A, B, and C describing the line from (x1,y1) and (x2,y2), consider how the terms m and b would be determined in slope-intercept form y=mx+b:

By definition, m=y2y1x2x1:

y=y2y1x2x1x+b

Then, substituting y=y1 and x=x1:

y1=(y2y1)x1x2x1+b

Solving for b:

b=y1(y2y1)x1x2x1=(x2x1)y1(y2y1)x1x2x1

Putting this together gives the equation which describes the slope-intercept form of a line from two points:

y=y2y1x2x1x+(x2x1)y1(y2y1)x1x2x1

Now, rearrange the standard form of the line in terms of y:

By=AxCy=ABx+CB

Comparing this with the expanded point-slope from before, it is seen that: A=y1y2, B=x2x1, and C=(Ax1+By1).

If two lines are parallel, m1=m2 in their slope-intercept forms, meaning A1B1=A2B2 or equivalently, A1B2=A2B1 in their standard forms.

If two lines are coincident, they must be parallel and have b1=b2 in their slope-intercept forms, meaning that C1B1=C2B2 or equivalently, B1C2=B2C1 in their standard forms. Otherwise, they will intersect at one unique point.

To find the x-coordinate of the intersection, multiply A1x+B1y+C1=0 by B2 to get B2A1x+B2B1y+B2C1=0, and A2x+B2y+C2=0 by B1 to get B1A2x+B1B2y+B1C2=0. Then, subtract the second equation from the first, and solve for x:

B2A1xB1A2x+B2C1B1C2=0x=B1C2B2C1B2A1B1A2

The same process can be applied to cancel the Ax term and find y=A1C2A2C1A2B1A1B2.

Pseudocode:

Copy
read x1, y1, x2, y2, x3, y3, x4, y4
A1 is y1 - y2, B1 is x2 - x1, C1 is -(A1 * X1 + B1 * Y1)
A2 is y3 - y4, B2 is x4 - x3, C2 is -(A2 * X3 + B2 * Y3)
if A1 * B2 == A2 * B1
    if B1 * C2 == B2 * C1 print "coincident"
    else print "parallel"
else
    x is (B1 * C2 - B2 * C1) / (B2 * A1 - B1 * A2)
    y is (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2)

Time complexity: O(1)


Comments

There are no comments at the moment.