#include"opencv2\opencv.hpp"
#include<iostream>
#include<math.h>

#define M_PI 3.141592653  /*PI*/
using namespace std;
using namespace cv;

//각도 알고리즘. 
double getAngle(Point start, Point end) {
	double dy = end.y - start.y;
	double dx = end.x - start.x;

	return atan2(dy, dx) * (180.0 / M_PI);
}

//두 점 사이의 거리 알고리즘.
double Distance(const Point&p1, const Point&p2)
{
	double distance;

	//피타고라스의 정리.
	//pow(x,2) : x의 2승. sqrt(): 제곱근
	distance = sqrt(pow(p1.x - p2.x, 2) + pow((p1.y - p2.y), 2));
	return distance;
}

//기울기 알고리즘.
double Gradient(Point start, Point end)
{
	double dy = end.y - start.y;
	double dx = end.x - start.x;
	return dy / dx; //탄젠트. 
}


void drawLines()
{
	Mat img(600, 600, CV_8UC3, Scalar(255, 255, 255));

	Point P1 = Point(0, 0); //S
	Point P2 = Point(300, 0);
	Point P3 = Point(0, 300);
	Point P4 = Point(300, 300); //E


	line(img, P1, P2, Scalar(0, 0, 255)); // 1
	line(img, P1, P3, Scalar(0, 0, 255)); // 2
	line(img, P3, P4, Scalar(0, 0, 255)); //3
	line(img, P2, P4, Scalar(0, 0, 255)); //4
	line(img, P1, P4, Scalar(0, 0, 255));

	cout << "P4.x: " << P4.x << endl;
	cout << "P1.x: " << P1.x << endl;
	cout << "P4.y: " << P4.y << endl;
	cout << "P1.y: " << P1.y << endl;

	//기울기 공식/
	float gradient = Gradient(P1, P4);
	cout << "기울기: " << gradient << endl;

	float angle = 0;
	//각도 알고리즘.
	angle = getAngle(P1, P4);
	cout << "각도: " << angle << endl;

	//거리 알고리즘.
	float distance = Distance( P1, P2);
	cout << "P1과 P2간의 거리: " << distance << endl;

	string g = to_string(gradient);
	string a = to_string(angle);

	putText(img, "Gradient: " + g, Point(10, 10), FONT_HERSHEY_COMPLEX_SMALL, 1, 255, 1);
	putText(img, "angle: " + a, Point(20, 20), FONT_HERSHEY_COMPLEX_SMALL, 3, 255, 1);
	imshow("img", img);
	waitKey(0);

	destroyAllWindows();
}
int main(void)
{
	drawLines();
}

+ Recent posts