음악, 삶, 개발

Juce 로 만드는 GUI 파트7 - Line 클래스 (2개의 Point) 본문

개발 공부/Juce 공부방

Juce 로 만드는 GUI 파트7 - Line 클래스 (2개의 Point)

Lee_____ 2020. 10. 31. 01:36

< Line 클래스 : 두개의 Point 객체를 멤버로! >

 

Point 클래스가 하나의 점인 x ,y 를 의미하는 반면,

Line 클래스는 2개의 점, 두개의 Point 객체, 즉 x1, y1, x2 ,y2 를 나타내는 클래스이다.

Line 클래스는 Line<ValueType> 인 template 클래스인데,

ValueType 으로는 float 또는 double 만을 사용하는것이 좋다.

공식문서에 따르면, int 를 사용할 경우 몇몇 멤버 함수를 사용할때 컴파일되지않을수도있다고한다.

아래에서 보겠지만, Line 클래스는 생성자로 두개의 Point 객체를 인자로 사용한다.


< 공식 문서 >

 

Line<ValueType> Class Template Reference

 

Represents a line.

This class contains a bunch of useful methods for various geometric tasks.

The ValueType template parameter should be a primitive type - float or double are what it's designed for.

Integer types will work in a basic way, but some methods that perform mathematical operations may not compile,

or they may not produce sensible results.

 

See also Point, Rectangle, Path, Graphics::drawLine

 

In function argument :

Point

/* Constructor */
Line ()=default
Line (const Line& )=default
Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
Line (Point<ValueType> startPoint, Point<ValueType> endPoint) noexcept
/* Destructor */
~Line ()=default
/* Operator */
Line & 	operator= (const Line &)=default
bool 	operator== (Line other) const noexcept
bool 	operator!= (Line other) const noexcept
/* Get */
ValueType 	        getStartX                           ()                                                              const noexcept
ValueType 	        getStartY                           ()                                                              const noexcept
ValueType 	        getEndX                             ()                                                              const noexcept
ValueType 	        getEndY                             ()                                                              const noexcept
ValueType 	        getLength                           ()                                                              const noexcept
ValueType 	        getLengthSquared                    ()                                                              const noexcept
ValueType 	        getDistanceFromPoint                (Point<ValueType> targetPoint, Point<ValueType>& pointOnLine)   const noexcept
ValueType 	        findNearestProportionalPositionTo   (Point<ValueType> point)                                        const noexcept
Point<ValueType> 	findNearestPointTo                  (Point<ValueType> point)                                        const noexcept
Point<ValueType> 	getStart                            ()                                                              const noexcept
Point<ValueType> 	getEnd                              ()                                                              const noexcept
Point<ValueType> 	getIntersection                     (Line line)                                                     const noexcept
Point<ValueType> 	getPointAlongLine                   (ValueType distanceFromStart)                                   const noexcept
Point<ValueType> 	getPointAlongLine                   (ValueType distanceFromStart, ValueType perpendicularDistance)  const noexcept
Point<ValueType> 	getPointAlongLineProportionally     (typename Point< ValueType >::FloatType proportionOfLength)     const noexcept
Point<ValueType >::FloatType 	getAngle                ()             
/* Set */
void 	setStart        (ValueType newStartX, ValueType newStartY)  noexcept
void 	setEnd          (ValueType newEndX, ValueType newEndY)      noexcept
void 	setStart        (const Point<ValueType> newStart)           noexcept
void 	setEnd          (const Point<ValueType> newEnd)             noexcept
void 	applyTransform  (const AffineTransform& transform)          noexcept
/* Bool */
bool 	            isVertical                          ()                                                              const noexcept
bool 	            isHorizontal                        ()                                                              const noexcept
bool 	            isPointAbove                        (Point<ValueType> point)                                        const noexcept
bool 	            intersects                          (Line line, Point<ValueType>& intersection)                     const noexcept
bool 	            intersects                          (Line other)                                                    const noexcept
/* Create */
Line 	        withShortenedStart  (ValueType distanceToShortenBy) const noexcept
Line 	        withShortenedEnd    (ValueType distanceToShortenBy) const noexcept
Line 	        reversed            ()                              const noexcept
Line<float>     toFloat             ()                              const noexcept
Line<double>    toDouble            ()                              const noexcept
/* Static */
static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept

< Line 의 크기와 복사 여부 : 가벼움! 가능! >

DBG(sizeof(juce::Point<float>));    // 8 bytes  = 64 bits
DBG(sizeof(juce::Line<float>));     // 16 bytes = 128 bits

DBG(sizeof(juce::Point<double>));   // 16 bytes = 128 bits
DBG(sizeof(juce::Line<double>));    // 32 bytes = 256 bits

DBG 를 사용하여, Point 클래스와 Line 클래스 모두를 찍어보았다.

보이는것처럼, Line 이 Point 보다 딱 2배 큰 크기인것을 확인할수있다.

Point 는 x, y 좌표인반면, Line 은 두개의 점 x1, y,1, x2, y2 이기때문이다.

juce::Line<double> 의 경우 매우 작다고 볼수는 없지만, 걱정할만한 크기는 아니다.

또한 우리는 대부분의 경우 juce::Line<float> 을 사용할것이다.

 

Line 은 가볍기때문에, 창조자는 복사가 가능하게끔 해놓았다.

Line (const Line& )=default              // Copy Constructor
Line & 	operator= (const Line &)=default // Copy Assignment

< Line 생성자 >

/* Constructor */
Line ()=default
Line (const Line& )=default
Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
Line (Point<ValueType> startPoint, Point<ValueType> endPoint) noexcept

Line 클래스는 위와 같이 4개의 생성자를 지원한다.

우리는 대부분의 경우 Point 객체를 인자로 사용하는 생성자를 사용할것이다.

Line (Point<ValueType> startPoint, Point<ValueType> endPoint) noexcept

앞서 배운 Point 클래스를 사용하여 Line 객체를 만들어보자.

const juce::Point<float> p1 {0.0, 4.0};
const juce::Point<float> p2 {10.0, 5.0};

const juce::Line<float> line        {p1, p2};   // x1 is 0.0, y1 is 4.0, x2 is 10.0, y2 is 5.0
const juce::Line<float> lineCopied  {line};     // Used Copy Constructor

위의 코드의 마지막줄은, Line 클래스의 Copy Constructor 를 사용하였다.

언제낭 우리는 창조자가 정의해놓은것만을 사용해야한다는것을 기억하자.


< Line 연산자 >

/* Operator */
Line & 	operator= (const Line &)=default        // Copy Assignment
bool 	operator== (Line other) const noexcept	// Compare!
bool 	operator!= (Line other) const noexcept	// Compare!

연산자로는, 

대입연산자와 비교 연산자만을 지원한다. 

비교연산자도 같은지와 다른지만 비교할수있다. 

아래와 같이 사용한다.

const juce::Point<float> p1 {0.0, 4.0};
const juce::Point<float> p2 {10.0, 5.0};

const juce::Line<float> line1 {p1, p2};
const juce::Line<float> line2 {0.0, 4.0, 10.0, 5.0};

DBG((line1 == line2 ? "true" : "false")); // 같아? true
DBG((line1 != line2 ? "true" : "false")); // 달라? false

< Line 의 get 함수들 >

/* Get */
ValueType 	        getStartX                           ()                                                              const noexcept
ValueType 	        getStartY                           ()                                                              const noexcept
ValueType 	        getEndX                             ()                                                              const noexcept
ValueType 	        getEndY                             ()                                                              const noexcept
ValueType 	        getLength                           ()                                                              const noexcept
ValueType 	        getLengthSquared                    ()                                                              const noexcept
ValueType 	        getDistanceFromPoint                (Point<ValueType> targetPoint, Point<ValueType>& pointOnLine)   const noexcept
ValueType 	        findNearestProportionalPositionTo   (Point<ValueType> point)                                        const noexcept
Point<ValueType> 	findNearestPointTo                  (Point<ValueType> point)                                        const noexcept
Point<ValueType> 	getStart                            ()                                                              const noexcept
Point<ValueType> 	getEnd                              ()                                                              const noexcept
Point<ValueType> 	getIntersection                     (Line line)                                                     const noexcept
Point<ValueType> 	getPointAlongLine                   (ValueType distanceFromStart)                                   const noexcept
Point<ValueType> 	getPointAlongLine                   (ValueType distanceFromStart, ValueType perpendicularDistance)  const noexcept
Point<ValueType> 	getPointAlongLineProportionally     (typename Point< ValueType >::FloatType proportionOfLength)     const noexcept
Point<ValueType >::FloatType 	getAngle                ()             

추후 업데이트하기.


< Line 의 set 함수들 >

/* Set */
void 	setStart        (ValueType newStartX, ValueType newStartY)  noexcept
void 	setEnd          (ValueType newEndX, ValueType newEndY)      noexcept
void 	setStart        (const Point<ValueType> newStart)           noexcept
void 	setEnd          (const Point<ValueType> newEnd)             noexcept
void 	applyTransform  (const AffineTransform& transform)          noexcept

추후 업데이트하기.


< Line 의 bool 함수들 >

/* Bool */
bool 	            isVertical                          ()                                                              const noexcept
bool 	            isHorizontal                        ()                                                              const noexcept
bool 	            isPointAbove                        (Point<ValueType> point)                                        const noexcept
bool 	            intersects                          (Line line, Point<ValueType>& intersection)                     const noexcept
bool 	            intersects                          (Line other)                                                    const noexcept

< Line 의 Create 함수들 >

/* Create */
Line 	        withShortenedStart  (ValueType distanceToShortenBy) const noexcept
Line 	        withShortenedEnd    (ValueType distanceToShortenBy) const noexcept
Line 	        reversed            ()                              const noexcept
Line<float>     toFloat             ()                              const noexcept
Line<double>    toDouble            ()                              const noexcept

추후 업데이트하기.


< Line 의 Static 함수 >

/* Static */
static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept

추후 업데이트하기.


< Line 을 그려주는 Graphics 클래스의 함수들  > 

/* Graphics 클래스의 함수들 */

void drawLine   (Line<float> line)                                                                      const
void drawLine   (Line<float> line, float lineThickness)                                                 const
void drawArrow  (Line<float> line, float lineThickness, float arrowheadWidth, float arrowheadLength)    const

추후 업데이트하기.