开发者

Detecting if a triangle flips when changing a point

开发者 https://www.devze.com 2023-04-04 08:28 出处:网络
I need to change a triangle by replacing one of its points. However, I need to detect if doing so would cause the triangle to flip.

I need to change a triangle by replacing one of its points. However, I need to detect if doing so would cause the triangle to flip.

For example, the triangle defined by the points:

[(1.0,1.0), (2.0,3.0), (3.0,1.0)]

would look like this:

Detecting if a triangle flips when changing a point

If I change the third point from (3.0,1.0) to (1.0,2.0), it flips, as shown here:

Detecting if a triangle flips when changing a point

I've written a function that detects if a triangle is flipped by calculating the equation for the stationary points and detecting a sign difference in the y-intercept:

def would_flip(stationary, orig_third_point, candidate_third_point):

    #m = (y2-y1)/(x2-x1)
    slope = (stationary[1][3] - stationary[0][4]) / (stationary[1][0] - stationary[0][0])

    #y = mx + b
    #b = y-mx
    yint = stationary[0][5] - slope * stationary[0][0]

    orig_atline = slope * orig_third_point[0] + yint
    candidate_atline = slope * candidate_third_point[0] + yint

    if orig_atline > orig_third_point[1] and not(candidate_atline > candidate_third_point[1]) or \
        orig_atline < orig_third_point[1] and not(candidate_atline < candidate_third_point[1]):
        return True

    return False

This works nicely for most cases:

>>> would_flip([(1.0,1.0), (2.0,3.0)], (3.0,1.0), (1.0,2.0))
True
>>> would_flip([(1.0,1.0), (2.0,3.0)], (3.0,1.0), (4.0,2.0))
False

The problem I have is that if the stationary points are vertical, the slope is infinite:

>>> would_flip([(1.0,1.0), (1.0,3.0)], (3.0,1.0), (4.0,2.0))
ZeroDivisionError: float division by zero

Is there a better/faster way to detect a triangle flip that is robust to the stationary points being a vertical line? The fact that it's written in python is not important. I will accept an answer that is just a formula or well-described technique.

EDIT: More information on what it means for a triangle to "flip"

Consider the four triangles below:

Detecting if a triangle flips when changing a point

The top-left is the original triangle. The red line (same in all four) are the two stationary points. The rest of the three triangles replace the third point. The top-right and bottom-left triangles are not flipped, while the triangle in the bottom-right is flipped. Essentially, the triangle is "flipped" if the third point ends up on the opposite side of the imaginary line formed by the two stationary points.

UPDATE2: Working function using cross product:

def would_flip2(stationary, orig_third_point, candidate_third_point):
    vec1 = numpy.array([stationary[1][0] - stationary[0][0], stationary[1][1] - stationary[0][1], 0])
    vec2_orig = numpy.array([orig_third_point[0] - stationary[0][0], orig_third_point[1] - stationary[0][1], 0])
    vec2_candidate = numpy.array([candidate_third_point[0] - stationary[0][0], candidate_third_point[1] - stationary[0][1], 0])
    orig_direction = numpy.cross(vec1, vec2_orig)[2]
    candidate_direction = numpy.cross(vec1, vec2_candidate)[2]
    if orig_direction > 0 and not(candidate_direction > 0) or \
        orig_direction < 0 and not(candidate_direction < 0):
开发者_如何学编程        return True
    return False


Compute the cross-product of two vectors generated from your three points. If the direction of the cross product changes sign, the triangle has flipped.

For example:

Given [(1.0,1.0), (2.0,3.0), (3.0,1.0)]: Form two (3D) vectors

(2-1,3-1,0) = (1,2,0) and (3-1,1-1,0) = (2,0,0)

Take their cross product:

(1,2,0) x (2,0,0) = (0,0,0-4) = (0,0,-4)

Or, using numpy:

import numpy as  np
np.cross([1,2,0],[2,0,0])
# array([ 0,  0, -4])

While when given [(1.0,1.0), (2.0,3.0), (1.0,2.0)]: We form the two (3D) vectors:

(2-1,3-1,0) = (1,2,0) and (1-1,2-1,0) = (0,1,0)

And again take their cross product:

np.cross([1,2,0],[0,1,0])
# array([0, 0, 1])

Since the vector (0,0,-4) points "down" and the vector (0,0,1) points "up", the triangle has flipped.


You don't really need numpy for this. If you work out the math on paper, it turns out that if the points are given by (x1,y1), (x2,y2) and (x3,y3), then the key number in the cross product is given by

(y2-y1)*(x2-x1) - (y3-y1)*(x2-x1)

You just have to compute that value and watch for changes in its sign. (The three points are co-linear if and only if the expression above equals 0.)


You could start your would_flip function with an is_straight_line function, and the rest of the code only executes if it isn't a straight line.

0

精彩评论

暂无评论...
验证码 换一张
取 消