반응형

두 점으로 이루어지는 선분과 한 점 사이의 거리 및 수선의 발을 찾아보자.

 

#include <opencv2/opencv.hpp>
#include <iostream>

float distPointToSegment(cv::Point2f p, cv::Point2f a, cv::Point2f b) {
	float l2 = std::powf(a.x - b.x, 2) + std::powf(a.y - b.y, 2); // 선분의 길이 제곱
	if (l2 == 0.0f) // a와 b가 같은 경우
		return std::sqrt(std::powf(p.x - a.x, 2) + std::powf(p.y - a.y, 2)); // 점과 a 사이 거리

	// 선분 위로 점 p의 사영(Projection) 위치 t 계산(투영된 지점의 비율)
	// t = [(p-a) dot (b-a)] / |b-a|^2
	float t = ((p.x - a.x) * (b.x - a.x) + (p.y - a.y) * (b.y - a.y)) / l2;

	// t가 0~1 사이면 선분 위(0이면 a, 1이면 b), 그렇지 않으면 선분 밖
	// 0보다 작으면 a쪽, 1보다 크면 b쪽
	if (t < 0.0f)
		return std::sqrt(std::powf(p.x - a.x, 2) + std::powf(p.y - a.y, 2));
	if (t > 1.0f)
		return std::sqrt(std::powf(p.x - b.x, 2) + std::powf(p.y - b.y, 2));

	// 선분 위에 사영된 점 위치 계산
	cv::Point2f projection = a + t * (b - a);
	std::cout << "Projection Point: (" << projection.x << ", " << projection.y << ")" << std::endl;

	// 점 p와 사영된 점 사이의 거리 계산
	return std::sqrt(std::powf(p.x - projection.x, 2) + std::powf(p.y - projection.y, 2));
}

int main() {
	cv::Point2f p(100, 0); // 점 p
	cv::Point2f a(0, 0);   // 선분 시작점 a
	cv::Point2f b(100, 100); // 선분 끝점 b

	float distance = distPointToSegment(p, a, b);
	std::cout << "Point to Segment Distance: " << distance << std::endl;

	return 0;
}

 

 

반응형
Posted by J-sean
:
반응형

It describes how to keep the aspect ratio.

OpenGL에서 종횡비를 유지하기 위해서는 Viewport와 Projection의 비율을 같게 해야 한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <gl/glut.h>
 
const int InitWidth = 300;
const int InitHeight = 300;
 
void MyDisplay();
void MyReshape(int NewWidth, int NewHeight);
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
 
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(InitWidth, InitHeight);
    glClearColor(0.0f0.0f0.0f0.0f);
    glutCreateWindow("OpenGL Aspect Ratio");
    
    glutReshapeFunc(MyReshape);
    // glutReshapeFunc sets the reshape callback for the current window.
    glutDisplayFunc(MyDisplay);
    
    glutMainLoop();
 
    return 0;
}
 
void MyDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
 
    glBegin(GL_TRIANGLES);
    glColor3f(0.0f0.0f1.0f);
    glVertex3f(0.0f0.5f0.0f);
    glColor3f(1.0f0.0f0.0f);
    glVertex3f(-0.5f-0.5f0.0f);
    glColor3f(0.0f1.0f0.0f);
    glVertex3f(0.5f-0.5f0.0f);
    glEnd();
 
    glFlush();
}
 
void MyReshape(int NewWidth, int NewHeight)
{
    glViewport(00, NewWidth, NewHeight);
    // glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates.
 
    GLfloat WidthFactor = (GLfloat)NewWidth / (GLfloat)InitWidth;
    GLfloat HeightFactor = (GLfloat)NewHeight / (GLfloat)InitHeight;
    
    glMatrixMode(GL_PROJECTION);    // glMatrixMode sets the current matrix mode.
    glLoadIdentity();    // glLoadIdentity replaces the current matrix with the identity matrix.
    glOrtho(-1.0 * WidthFactor, 1.0 * WidthFactor, -1.0 * HeightFactor, 1.0 * HeightFactor, -1.01.0);
    // glOrtho describes a transformation that produces a parallel projection.
}
 

 

Run the program.

 

Resize the window.

 

Resize the window.

 

반응형
Posted by J-sean
: