Codeforces: A. Where do I Turn?


The problem is from codeforces: http://www.codeforces.com/problemset/problem/227/A

227A_1 Codeforces: A. Where do I Turn? beginner codeforces geometry math python

227A_2 Codeforces: A. Where do I Turn? beginner codeforces geometry math python

The picture vividly illustrates the problem statement. Given 3 Points A, B and C, you are required to find out the direction. Read from keyboard the three coordinates as three complex numbers and compute the complex number tex_1b526189023f0d6e31293c8b2682f1eb Codeforces: A. Where do I Turn? beginner codeforces geometry math python . The method conjugate() applies to the complex number. It computes the complex number that has the identical length.

>>> a=3+4j
>>> a.conjugate()
(3-4j)
>>> a.conjugate().conjugate()
(3+4j)
>>> a.conjugate().imag
-4.0
>>> a.conjugate().real
3.0

The two-line Python solution is as follows.

a, b, c = [eval(raw_input().replace(' ', '+') + 'j') for i in xrange(3)]
print ['TOWARDS', 'RIGHT', 'LEFT'][cmp(((b - a) * (c - b).conjugate()).imag, 0)]

The first line returns a, b and c as complex numbers e.g. 1+2j. The second line will print the corresponding direction, based on the cmp value which has three possible outcomes, -1, 0 and 1.

The pure math solution, not using complex numbers is given below.

#!/usr/bin/env python

def read():
    return map(int, raw_input().split(' '))

x1, y1 = read()
x2, y2 = read()
x3, y3 = read()

k = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)
print ['TOWARDS','RIGHT','LEFT'][cmp(k, 0)]

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
385 words
Last Post: Codeforces: A. Is your horseshoe on the other hoof?
Next Post: Codeforces: B. T-primes

The Permanent URL is: Codeforces: A. Where do I Turn?

Leave a Reply