1
votes

I'm creating Concave Hull algorithm and I almost finished it. The problem is I need to change some fragment of my code that calculates angles between consecutive lines.

Angles

Starting from horizontal line, clockwise direction. I have written a method that returns an angle between this horizontal line and my line. It works fine. But how to modify it to obtain an angle between two my lines? Please, note that the coordinate system is from top-left corner (pixels on the screen) and the consecutive lines will create a polygon as a result so they are connecting each other in different order (so the line could be tinted turned in different directions).

My code that counts current angles betweet a horizontal line and my line:

private static double Angle(Vertex v1, Vertex v2, double offsetInDegrees = 0.0)
{
    return (RadianToDegree(Math.Atan2(-v2.Y + v1.Y, -v2.X + v1.X)) + offsetInDegrees)%360.0;
}

public static double RadianToDegree(double radian)
{
    var degree = radian * (180.0 / Math.PI);
    if (degree < 0)
        degree = 360 + degree;

    return degree;
}
1
could you calculate both and add them?Jeroen van Langen
Deleted my answer - ignore my comments - I was thinking convex hull!weston
Not exactly, I think:PPPNickon
@Nickon - Why not? Each of your lines is a 2d vector, and you want to find the angle between each pair of consecutive lines, hence your problem boils down to "how do I find the angle between two 2D vectors", right?mbeckish

1 Answers

3
votes

You could find the angle between each of the two lines and the horizontal line, and then subtract both of those from 180 degrees.

That is (assuming degrees here)

double priorAngle = Angle(p1,p2);
double nextAngle = Angle(p2,p3);
double angleInBetween = 180.0 - (priorAngle + nextAngle);