KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
TernaryPoint.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "TernaryPoint.h"
24 #include "TernaryConstants.h"
25 
26 #include <limits>
27 
28 #include <QChar>
29 #include <QTextStream>
30 
32  : m_a( -1.0 )
33  , m_b( -1.0 )
34 {
35  Q_ASSERT( !isValid() );
36 }
37 
38 TernaryPoint::TernaryPoint( qreal a, qreal b )
39  : m_a( -1.0 )
40  , m_b( -1.0 )
41 {
42  set( a, b );
43 }
44 
45 void TernaryPoint::set( qreal a, qreal b )
46 {
47  if ( a >= 0.0 && a <= 1.0
48  && b >= 0.0 && b <= 1.0
49  && 1.0 - a - b >= -2.0 * std::numeric_limits<qreal>::epsilon() ) {
50  m_a = a;
51  m_b = b;
52  Q_ASSERT( isValid() ); // more a test for isValid
53  } else {
54  m_a = -1.0;
55  m_b = -1.0;
56  Q_ASSERT( ! isValid() );
57  }
58 }
59 
61 {
62  return
63  m_a >= 0.0 && m_a <= 1.0
64  && m_b >= 0.0 && m_b <= 1.0
65  && 1.0 - m_a + m_b >= - std::numeric_limits<qreal>::epsilon();
66 }
67 
68 QDebug operator<<( QDebug stream, const TernaryPoint& point )
69 {
70  QString string;
71  QTextStream text( &string );
72  text << "[TernaryPoint: ";
73  if ( point.isValid() ) {
74  text.setFieldWidth( 2 );
75  text.setPadChar( QLatin1Char( '0' ) );
76  text << ( int ) ( point.a() * 100.0 ) << "%|"
77  << ( int ) ( point.b() * 100.0 ) << "%|"
78  << ( int ) ( point.c() * 100.0 ) << "%]";
79  } else {
80  text << "a=" << point.a() << " - b=" << point.b() << " - INVALID]";
81  }
82  stream << string;
83  return stream;
84 }
85 
86 QPointF translate( const TernaryPoint& point )
87 {
88  if ( point.isValid() ) {
89  // the position is calculated by
90  // - first moving along the B-C line to the function that b
91  // selects
92  // - then traversing the selected function until we meet with
93  // the function that A selects (which is a parallel of the B-C
94  // line)
95  QPointF bPosition( 1.0 - point.b(), 0.0 );
96  QPointF aPosition( point.a() * AxisVector_C_A );
97  QPointF result( bPosition + aPosition );
98  return result;
99  } else {
100  qWarning() << "TernaryPoint::translate(TernaryPoint): cannot translate invalid ternary points:"
101  << point;
102  return QPointF();
103  }
104 }

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/