Nickon
Nickon

Reputation: 10156

Calculate angles between consecutive lines on the computer screen

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;
}

Upvotes: 1

Views: 2269

Answers (1)

Christopher Stevenson
Christopher Stevenson

Reputation: 2861

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);

Upvotes: 3

Related Questions