一、问题
已知三角形的3个顶点p0
、p1
和p2
。现希望画出三角形,并计算3个角的大小。
二、分析
直线 a a a的直线方程:
y − y 1 y 2 − y 1 = x − x 1 x 2 − x 1 ⇒ { a x + b y + c = 0 a = − ( y 2 − y 1 ) b = x 2 − x 1 c = ( y 2 − y 1 ) x 1 − ( x 2 − x 1 ) y 1 \frac{y-y_1}{y_2-y_1} = \frac{x-x_1}{x_2-x_1} \Rightarrow \begin{cases} ax+by+c=0 \\a=-(y_2-y_1) \\ b = x_2-x_1 \\ c = (y_2-y_1)x_1 - (x_2-x_1) y_1\end{cases} y2−y1y−y1=x2−x1x−x1⇒⎩⎪⎪⎪⎨⎪⎪⎪⎧ax+by+c=0a=−(y2−y1)b=x2−x1c=(y2−y1)x1−(x2−x1)y1
已知3个顶点p0
、p1
和p2
,要构造三角形必须确保3个顶点不在同一直线上,即p0
不在直线 a a a上:
a * p0.x + b * p0.y + c != 0
三角形各边边长为:
a = ( p 2. x − p 1. x ) 2 + ( p 2. y − p 1. y ) 2 a = \sqrt{(p2.x-p1.x)^2+(p2.y-p1.y)^2} a=(p2.x−p1.x)2+(p2.y−p1.y)2
b = ( p 2. x − p 0. x ) 2 + ( p 2. y − p 0. y ) 2 b = \sqrt{(p2.x-p0.x)^2+(p2.y-p0.y)^2} b=(p2.x−p0.x)2+(p2.y−p0.y)2
c = ( p 1. x − p 0. x ) 2 + ( p 1. y − p 0. y ) 2 c = \sqrt{(p1.x-p0.x)^2+(p1.y-p0.y)^2} c=(p1.x−p0.x)2+(p1.y−p0.y)2
由余弦定理得:
a 2 = b 2 + c 2 − 2 b c cos θ ⇒ cos θ = b 2 + c 2 − a 2 2 b c ⇒ θ = arccos ( b 2 + c 2 − a 2 2 b c ) a^2=b^2+c^2-2bc \cos \theta \Rightarrow \cos \theta = \frac{b^2+c^2-a^2}{2bc} \Rightarrow \theta = \arccos (\frac{b^2+c^2-a^2}{2bc}) a2=b2+c2−2bccosθ⇒cosθ=2bcb2+c2−a2⇒θ=arccos(2bcb2+c2−a2)
三、实现
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
using namespace cv;
Point2d g_p0(200, 500), g_p1(400, 100), g_p2(600, 500);
// 判断3个点不在同一直线上
bool same_line(Point2d p0, Point2d p1, Point2d p2)
{
double a = -(p2.y - p1.y);
double b = p2.x - p1.x;
double c = (p2.y - p1.y) * p1.x - (p2.x - p1.x) * p1.y;
if (a * p0.x + b * p0.y + c != 0) // p0不在直线p1p2上
return true;
else
return false;
}
// 计算夹角
double calculate_angle(Point2d p0, Point2d p1, Point2d p2)
{
double a = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
double b = sqrt(pow(p2.x - p0.x, 2) + pow(p2.y - p0.y, 2));
double c = sqrt(pow(p1.x - p0.x, 2) + pow(p1.y - p0.y, 2));
double angle = acos((b * b + c * c - a * a) / (2 * b * c)) * 180 / CV_PI;
return angle;
}
// 画夹角
void draw_angle(Mat img, Point2d p0, Point2d p1, Point2d p2, double radius, const Scalar color, int thickness)
{
// 计算直线的角度
double angle1 = atan2(-(p1.y - p0.y), (p1.x - p0.x)) * 180 / CV_PI;
double angle2 = atan2(-(p2.y - p0.y), (p2.x - p0.x)) * 180 / CV_PI;
// 计算主轴的角度
double angle = angle1 <= 0 ? -angle1 : 360 - angle1;
// 计算圆弧的结束角度
double end_angle = (angle2 < angle1) ? (angle1 - angle2) : (360 - (angle2 - angle1));
if (end_angle > 180)
{
angle = angle2 <= 0 ? -angle2 : 360 - angle2;
end_angle = 360 - end_angle;
}
// 画圆弧
ellipse(img, p0, Size(radius, radius), angle, 0, end_angle, color, thickness);
}
// 鼠标回调函数
void mouse_callback(int event, int x, int y, int flags, void *param)
{
static Point2d p(0, 0), p1(0, 0), p2(0, 0);
static int n = -1;
switch (event)
{
case cv::EVENT_LBUTTONDOWN: // 鼠标左键点击
{
p1 = Point2d(x, y);
int w = 15, h = 15;
Rect r0(g_p0.x - w, g_p0.y - h, 2 * w, 2 * h);
Rect r1(g_p1.x - w, g_p1.y - h, 2 * w, 2 * h);
Rect r2(g_p2.x - w, g_p2.y - h, 2 * w, 2 * h);
if (r0.contains(p1)) // 鼠标落在中心点g_p0
{
n = 0;
p = g_p0;
}
else if (r1.contains(p1)) // 鼠标落在g_p1
{
n = 1;
p = g_p1;
}
else if (r2.contains(p1)) // 鼠标落在g_p2
{
n = 2;
p = g_p2;
}
break;
}
case cv::EVENT_MOUSEMOVE: // 鼠标移动
p2 = Point2d(x, y);
if (n == 0)
{
g_p0 = p + p2 - p1;
}
else if (n == 1)
{
g_p1 = p + p2 - p1;
}
else if (n == 2)
{
g_p2 = p + p2 - p1;
}
break;
case cv::EVENT_LBUTTONUP: // 鼠标左键释放
p1 = Point2d(0, 0);
p2 = Point2d(0, 0);
n = -1;
break;
default:
break;
}
}
// 主函数
int main()
{
string window_name = "image";
namedWindow(window_name, WINDOW_AUTOSIZE);
int w = 800, h = 600;
Mat image_original = Mat(h, w, CV_8UC3, Scalar(255, 255, 255));
cv::setMouseCallback(window_name, mouse_callback); // 调用鼠标回调函数
while (true)
{
Mat img = image_original.clone(); // 拷贝空白图片,方便重复画图
line(img, g_p1, g_p2, Scalar(255, 0, 0), 2); // 画蓝线a
line(img, g_p2, g_p0, Scalar(0, 255, 0), 2); // 画绿线b
line(img, g_p1, g_p0, Scalar(0, 0, 255), 2); // 画红线c
if (same_line(g_p0, g_p1, g_p2)) // 3个点不在同一条直线上
{
draw_angle(img, g_p0, g_p1, g_p2, 15, Scalar(255, 0, 0), 2); // 画蓝色夹角
double b = calculate_angle(g_p0, g_p1, g_p2);
string s2 = "p0: (" + to_string(g_p1.x) + ", " + to_string(g_p1.y) + ") " + to_string(b);
putText(img, s2, Point2d(10, 25), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(255, 0, 0));
draw_angle(img, g_p1, g_p2, g_p0, 15, Scalar(0, 255, 0), 2); // 画绿色夹角
double c = calculate_angle(g_p1, g_p2, g_p0);
string s3 = "p1: (" + to_string(g_p2.x) + ", " + to_string(g_p2.y) + ") " + to_string(c);
putText(img, s3, Point2d(10, 50), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 255, 0));
draw_angle(img, g_p2, g_p0, g_p1, 15, Scalar(0, 0, 255), 2); // 画红色夹角
double a = calculate_angle(g_p2, g_p0, g_p1);
string s1 = "p2: (" + to_string(g_p0.x) + ", " + to_string(g_p0.y) + ") " + to_string(a);
putText(img, s1, Point2d(10, 75), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 0, 255));
}
else
{
putText(img, "error", Point2d(10, 25), cv::FONT_HERSHEY_COMPLEX, 0.5, Scalar(0, 0, 255), 1);
}
imshow(window_name, img);
if (waitKey(3) > 0)
break;
}
return 0;
}
操作方法:
鼠标左键点击三角形任意一个顶点并按住移动,由此可修改三角形。在键盘输入任意的键,可退出程序。
运行结果: