Krita Source Code Documentation
Loading...
Searching...
No Matches
Ellipse Class Reference

#include <Ellipse.h>

Public Member Functions

QRectF boundingRect () const
 
 Ellipse ()
 
 Ellipse (const QPointF &p1, const QPointF &p2, const QPointF &p3)
 
const QTransform & getInverse () const
 
const QTransform & getTransform () const
 
const QPointF & major1 () const
 
const QPointF & major2 () const
 
const QPointF & point () const
 
QPointF project (const QPointF &) const
 
qreal semiMajor () const
 
qreal semiMinor () const
 
bool set (const QPointF &m1, const QPointF &m2, const QPointF &p)
 
bool setMajor1 (const QPointF &p)
 
bool setMajor2 (const QPointF &p)
 
bool setPoint (const QPointF &p)
 
 ~Ellipse ()
 

Private Member Functions

bool changeMajor ()
 
bool changeMinor ()
 

Private Attributes

qreal a
 
qreal b
 
QTransform inverse
 
QTransform matrix
 
QPointF p1
 
QPointF p2
 
QPointF p3
 

Detailed Description

Definition at line 13 of file Ellipse.h.

Constructor & Destructor Documentation

◆ Ellipse() [1/2]

Ellipse::Ellipse ( )

Definition at line 12 of file Ellipse.cc.

12 : a(-1), b(-1)
13{
14}
qreal b
Definition Ellipse.h:43
qreal a
Definition Ellipse.h:42

◆ Ellipse() [2/2]

Ellipse::Ellipse ( const QPointF & p1,
const QPointF & p2,
const QPointF & p3 )

Definition at line 15 of file Ellipse.cc.

15 : p1(_p1), p2(_p2), p3(_p3) {
17}
QPointF p2
Definition Ellipse.h:47
QPointF p1
Definition Ellipse.h:46
bool changeMajor()
Definition Ellipse.cc:87
QPointF p3
Definition Ellipse.h:48

References changeMajor().

◆ ~Ellipse()

Ellipse::~Ellipse ( )

Definition at line 19 of file Ellipse.cc.

20{
21}

Member Function Documentation

◆ boundingRect()

QRectF Ellipse::boundingRect ( ) const

Definition at line 62 of file Ellipse.cc.

63{
64 const QPointF d = rotate90((p2 - p1) * 0.5 * b / a);
65 const QPointF pts[4] = {
66 p1 + d,
67 p1 - d,
68 p2 + d,
69 p2 - d
70 };
71 QRectF ret;
72 for (int i = 0; i < 4; ++i) {
73 ret = ret.united(QRectF(pts[i], QSizeF(0.0001, 0.0001)));
74 }
75 return ret;
76}
QPointF rotate90(const QPointF &p)
Definition Ellipse.cc:58

References a, b, p1, p2, and rotate90().

◆ changeMajor()

bool Ellipse::changeMajor ( )
private

Definition at line 87 of file Ellipse.cc.

88{
89 a = length(p1 - p2) * 0.5;
90
91 /*
92 * calculate transformation matrix
93 * x' = m11*x + m21*y + dx
94 * y' = m22*y + m12*x + dy
95 * m11 = m22, m12 = -m21 (rotations and translations only)
96 * x' = m11*x - m12*y + dx
97 * y' = m11*y + m12*x + dy
98 *
99 * then, transforming (x1, y1) to (x1', y1') and (x2, y2) to (x2', y2'):
100 *
101 * m11 = (y2*y2' + y1 * (y1'-y2') - y2*y1' + x2*x'2 - x1*x'2 + (x1-x2)*x1')
102 * ------------------------------------------------------------------
103 * (y2^2 - 2*y1*y2 + y1^2 + x2^2 - 2*x1*x2 + x1^2)
104 * m12 = -(x1*(y2'-y1') - x2*y2' + x2*y1' + x2'*y2 - x1'*y2 + (x1'-x2')*y1)
105 * ------------------------------------------------------------------
106 * (y2^2 - 2*y1*y2 + y1^2 + x2^2 - 2*x1*x2 + x1^2)
107 * dx = (x1*(-y2*y2' + y2*y1' - x2*x2') + y1*( x2*y2' - x2*y1' - x2'*y2 - x1'*y2) + x2'*y1^2 + x1^2*x2' + x1'*(y2^2 + x2^2 - x1*x2))
108 * ----------------------------------------------------------------------------------------------------------------------------
109 * (y2^2 - 2*y1*y2 + y1^2 + x2^2 - 2*x1*x2 + x1^2)
110 * dy = (x1*(-x2*y2' - x2*y1' + x2'*y2) + y1*(-y2*y2' - y2*y1' - x2*x2' + x2*x1') + y2'*y1^2 + x1^2*y2' + y1'(y2^2 + x2^2) - x1*x1'*y2)
111 * -------------------------------------------------------------------------------------------------------------------------------
112 * (y2^2 - 2*y1*y2 + y1^2 + x2^2 - 2*x1*x2 + x1^2)
113 *
114 * in our usage, to move the ellipse into canonical position:
115 *
116 * (x1, y1) = p1
117 * (x2, y2) = p2
118 * (x1', y1') = (-a, 0)
119 * (x2', y2') = (a, 0)
120 */
121
122 const qreal
123 x1 = p1.x(),
124 x2 = p2.x(),
125 y1 = p1.y(),
126 y2 = p2.y(),
127 x1p = -a,
128 x2p = a,
129 x1sqr = x1 * x1,
130 x2sqr = x2 * x2,
131 y1sqr = y1 * y1,
132 y2sqr = y2 * y2,
133 factor = 1.0 / (x1sqr + y1sqr + x2sqr + y2sqr - 2.0 * y1 * y2 - 2.0 * x1 * x2),
134 m11 = (x2*x2p - x1*x2p + (x1-x2)*x1p) * factor,
135 m12 = -(x2p*y2 - x1p*y2 + (x1p-x2p)*y1) * factor,
136 dx = (x1*(-x2*x2p) + y1*(-x2p*y2 - x1p*y2) + x2p*y1sqr + x1sqr*x2p + x1p*(y2sqr + x2sqr - x1*x2)) * factor,
137 dy = (x1*(x2p*y2) + y1*(-x2*x2p + x2*x1p) - x1*x1p*y2) * factor;
138
139 matrix = QTransform(m11, m12, -m12, m11, dx, dy);
140 inverse = matrix.inverted();
141
142 return changeMinor();
143}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
QTransform inverse
Definition Ellipse.h:41
bool changeMinor()
Definition Ellipse.cc:145
QTransform matrix
Definition Ellipse.h:40

References a, changeMinor(), inverse, length(), matrix, p1, and p2.

◆ changeMinor()

bool Ellipse::changeMinor ( )
private

Definition at line 145 of file Ellipse.cc.

146{
147 QPointF p = matrix.map(p3);
148
149 /*
150 * ellipse formula:
151 * x^2/a^2 + y^2/b^2 = 1
152 * b = sqrt(y^2 / (1 - x^2/a^2))
153 */
154 const qreal
155 asqr = a * a,
156 xsqr = p.x() * p.x(),
157 ysqr = p.y() * p.y(),
158 divisor = (1.0 - xsqr / asqr);
159 if (divisor <= 0) {
160 // division by zero!
161 b = -1;
162 return false;
163 }
164 b = sqrt(ysqr / divisor);
165 return true;
166}
const Params2D p

References a, b, matrix, p, and p3.

◆ getInverse()

const QTransform & Ellipse::getInverse ( ) const
inline

Definition at line 32 of file Ellipse.h.

32{ return inverse; }

References inverse.

◆ getTransform()

const QTransform & Ellipse::getTransform ( ) const
inline

Definition at line 31 of file Ellipse.h.

31{ return matrix; }

References matrix.

◆ major1()

const QPointF & Ellipse::major1 ( ) const
inline

Definition at line 25 of file Ellipse.h.

25{ return p1; }

References p1.

◆ major2()

const QPointF & Ellipse::major2 ( ) const
inline

Definition at line 27 of file Ellipse.h.

27{ return p2; }

References p2.

◆ point()

const QPointF & Ellipse::point ( ) const
inline

Definition at line 29 of file Ellipse.h.

29{ return p3; }

References p3.

◆ project()

QPointF Ellipse::project ( const QPointF & pt) const

Definition at line 39 of file Ellipse.cc.

40{
41 if (a <= 0 || b <= 0) return pt; // not a valid ellipse
42 QPointF p = matrix.map(pt);
43 /*
44 * intersect line from (0,0) to p with the ellipse in canonical position
45 * the equation of the line is y = py/px x
46 * the equation of the ellipse is x^2/a^2 + y^2/b^2 = 1
47 * x=(a*b*px)/sqrt(a^2*py^2+b^2*px^2)
48 * y=(a*b*py)/sqrt(a^2*py^2+b^2*px^2)
49 */
50 const qreal divisor = sqrt(a * a * p.y() * p.y() + b * b * p.x() * p.x());
51 if (divisor <= 0) return inverse.map(QPointF(a, 0)); // give up
52 const qreal ab = a * b, factor = 1.0 / divisor;
53 QPointF ep(ab * p.x() * factor, ab * p.y() * factor);
54 return inverse.map(ep);
55/* return inverse.map(closest(matrix.map(pt)));*/
56}

References a, b, inverse, matrix, and p.

◆ semiMajor()

qreal Ellipse::semiMajor ( ) const
inline

Definition at line 33 of file Ellipse.h.

33{ return a; }

References a.

◆ semiMinor()

qreal Ellipse::semiMinor ( ) const
inline

Definition at line 34 of file Ellipse.h.

34{ return b; }

References b.

◆ set()

bool Ellipse::set ( const QPointF & m1,
const QPointF & m2,
const QPointF & p )

Definition at line 23 of file Ellipse.cc.

24{
25 bool changedMajor = m1 != p1 || m2 != p2,
26 changedMinor = !changedMajor && p != p3;
27 p1 = m1;
28 p2 = m2;
29 p3 = p;
30 if (changedMajor) {
31 return changeMajor();
32 } else if (changedMinor) {
33 return changeMinor();
34 } else {
35 return a > 0 && b > 0;
36 }
37}

References a, b, changeMajor(), changeMinor(), p, p1, p2, and p3.

◆ setMajor1()

bool Ellipse::setMajor1 ( const QPointF & p)

Definition at line 168 of file Ellipse.cc.

169{
170 p1 = p;
171 return changeMajor();
172}

References changeMajor(), p, and p1.

◆ setMajor2()

bool Ellipse::setMajor2 ( const QPointF & p)

Definition at line 173 of file Ellipse.cc.

173 {
174 p2 = p;
175 return changeMajor();
176}

References changeMajor(), p, and p2.

◆ setPoint()

bool Ellipse::setPoint ( const QPointF & p)

Definition at line 177 of file Ellipse.cc.

178{
179 p3 = p;
180 return changeMinor();
181}

References changeMinor(), p, and p3.

Member Data Documentation

◆ a

qreal Ellipse::a
private

Definition at line 42 of file Ellipse.h.

◆ b

qreal Ellipse::b
private

Definition at line 43 of file Ellipse.h.

◆ inverse

QTransform Ellipse::inverse
private

Definition at line 41 of file Ellipse.h.

◆ matrix

QTransform Ellipse::matrix
private

Definition at line 40 of file Ellipse.h.

◆ p1

QPointF Ellipse::p1
private

Definition at line 46 of file Ellipse.h.

◆ p2

QPointF Ellipse::p2
private

Definition at line 47 of file Ellipse.h.

◆ p3

QPointF Ellipse::p3
private

Definition at line 48 of file Ellipse.h.


The documentation for this class was generated from the following files: