Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Wednesday, May 8, 2013

Triangle


Problem: Check whether given three 2-dimensional points form a triangle.


Solution in Python:

from __future__ import division

def check_triangle(x1, y1, x2, y2, x3, y3, eps=1e-16):
    """
    Author: Mayur P Srivastava
    """

    dx12 = x1 - x2
    dy12 = y1 - y2
    dx23 = x2 - x3
    dy23 = y2 - y3

    if dx12 == 0 and dx23 == 0:
        return False
    if abs(dx12) < eps and abs(dx23) < eps:
        return False
    if dx12 == 0 or abs(dx12) < eps:
        return True
    if dx23 == 0 or abs(dx23) < eps:
        return True

    slope12 = dy12 / dx12
    slope23 = dy23 / dx23

    return not (abs(slope12 - slope23) < eps)

Concepts Learned: Maths.

No comments:

Post a Comment