I think what you're looking for is the inner product (you may also want to look over the dot product entry) of the two angles. In your case, that's given by:
float dx21 = x2-x1;
float dx31 = x3-x1;
float dy21 = y2-y1;
float dy31 = y3-y1;
float m12 = sqrt( dx21*dx21 + dy21*dy21 );
float m13 = sqrt( dx31*dx31 + dy31*dy31 );
float theta = acos( (dx21*dx31 + dy21*dy31) / (m12 * m13) );
Answer is in radians.
EDIT: Here's a complete implementation. Substitute the problematic values in p1, p2, and p3 and let me know what you get. The point p1 is the vertex where the two lines intersect, in accordance with your definition of the two lines.
#include <math.h>
#include <iostream>
template <typename T> class Vector2D
{
private:
T x;
T y;
public:
explicit Vector2D(const T& x=0, const T& y=0) : x(x), y(y) {}
Vector2D(const Vector2D<T>& src) : x(src.x), y(src.y) {}
virtual ~Vector2D() {}
// Accessors
inline T X() const { return x; }
inline T Y() const { return y; }
inline T X(const T& x) { this->x = x; }
inline T Y(const T& y) { this->y = y; }
// Vector arithmetic
inline Vector2D<T> operator-() const
{ return Vector2D<T>(-x, -y); }
inline Vector2D<T> operator+() const
{ return Vector2D<T>(+x, +y); }
inline Vector2D<T> operator+(const Vector2D<T>& v) const
{ return Vector2D<T>(x+v.x, y+v.y); }
inline Vector2D<T> operator-(const Vector2D<T>& v) const
{ return Vector2D<T>(x-v.x, y-v.y); }
inline Vector2D<T> operator*(const T& s) const
{ return Vector2D<T>(x*s, y*s); }
// Dot product
inline T operator*(const Vector2D<T>& v) const
{ return x*v.x + y*v.y; }
// l-2 norm
inline T norm() const { return sqrt(x*x + y*y); }
// inner angle (radians)
static T angle(const Vector2D<T>& v1, const Vector2D<T>& v2)
{
return acos( (v1 * v2) / (v1.norm() * v2.norm()) );
}
};
int main()
{
Vector2D<double> p1(215, 294);
Vector2D<double> p2(174, 228);
Vector2D<double> p3(303, 294);
double rad = Vector2D<double>::angle(p2-p1, p3-p1);
double deg = rad * 180.0 / M_PI;
std::cout << "rad = " << rad << "deg = " << deg << std::endl;
p1 = Vector2D<double>(153, 457);
p2 = Vector2D<double>(19, 457);
p3 = Vector2D<double>(15, 470);
rad = Vector2D<double>::angle(p2-p1, p3-p1);
deg = rad * 180.0 / M_PI;
std::cout << "rad = " << rad << "deg = " << deg << std::endl;
return 0;
}
The code above yields:
rad = 2.12667 deg = 121.849
rad = 0.0939257 deg = 5.38155
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…