KChart

TernaryPoint.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "TernaryPoint.h"
10#include "TernaryConstants.h"
11
12#include <limits>
13
14#include <QChar>
15#include <QTextStream>
16
17TernaryPoint::TernaryPoint()
18 : m_a( -1.0 )
19 , m_b( -1.0 )
20{
21 Q_ASSERT( !isValid() );
22}
23
24TernaryPoint::TernaryPoint( qreal a, qreal b )
25 : m_a( -1.0 )
26 , m_b( -1.0 )
27{
28 set( a, b );
29}
30
31void TernaryPoint::set( qreal a, qreal b )
32{
33 if ( a >= 0.0 && a <= 1.0
34 && b >= 0.0 && b <= 1.0
35 && 1.0 - a - b >= -2.0 * std::numeric_limits<qreal>::epsilon() ) {
36 m_a = a;
37 m_b = b;
38 Q_ASSERT( isValid() ); // more a test for isValid
39 } else {
40 m_a = -1.0;
41 m_b = -1.0;
42 Q_ASSERT( ! isValid() );
43 }
44}
45
46bool TernaryPoint::isValid() const
47{
48 return
49 m_a >= 0.0 && m_a <= 1.0
50 && m_b >= 0.0 && m_b <= 1.0
51 && 1.0 - m_a + m_b >= - std::numeric_limits<qreal>::epsilon();
52}
53
54QDebug operator<<( QDebug stream, const TernaryPoint& point )
55{
56 QString string;
57 QTextStream text( &string );
58 text << "[TernaryPoint: ";
59 if ( point.isValid() ) {
60 text.setFieldWidth( 2 );
61 text.setPadChar( QLatin1Char( '0' ) );
62 text << ( int ) ( point.a() * 100.0 ) << "%|"
63 << ( int ) ( point.b() * 100.0 ) << "%|"
64 << ( int ) ( point.c() * 100.0 ) << "%]";
65 } else {
66 text << "a=" << point.a() << " - b=" << point.b() << " - INVALID]";
67 }
68 stream << string;
69 return stream;
70}
71
72QPointF translate( const TernaryPoint& point )
73{
74 if ( point.isValid() ) {
75 // the position is calculated by
76 // - first moving along the B-C line to the function that b
77 // selects
78 // - then traversing the selected function until we meet with
79 // the function that A selects (which is a parallel of the B-C
80 // line)
81 QPointF bPosition( 1.0 - point.b(), 0.0 );
82 QPointF aPosition( point.a() * AxisVector_C_A );
83 QPointF result( bPosition + aPosition );
84 return result;
85 } else {
86 qWarning() << "TernaryPoint::translate(TernaryPoint): cannot translate invalid ternary points:"
87 << point;
88 return QPointF();
89 }
90}
TernaryPoint defines a point within a ternary coordinate plane.
bool isValid(QStringView ifopt)
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:18:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.