cid#1640468 Dereference after null check
[LibreOffice.git] / include / basegfx / tuple / b2dtuple.hxx
bloba4558f0d34695a146c3d2facc0f46382b8d22205
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <sal/types.h>
23 #include <basegfx/basegfxdllapi.h>
24 #include <basegfx/tuple/Tuple2D.hxx>
26 namespace basegfx
28 class B2ITuple;
30 /** Base class for all Points/Vectors with two double values
32 This class provides all methods common to Point
33 and Vector classes which are derived from here.
35 @derive Use this class to implement Points or Vectors
36 which are based on two double values
38 class SAL_WARN_UNUSED B2DTuple : public Tuple2D<double>
40 public:
42 /** Create a 2D Tuple
44 The tuple is initialized to (0.0, 0.0)
46 B2DTuple()
47 : Tuple2D(0.0, 0.0)
50 /** Create a 2D Tuple
52 @param fX
53 This parameter is used to initialize the X-coordinate
54 of the 2D Tuple.
56 @param fY
57 This parameter is used to initialize the Y-coordinate
58 of the 2D Tuple.
60 B2DTuple(double fX, double fY)
61 : Tuple2D(fX, fY)
64 B2DTuple(Tuple2D<double> const& rTuple)
65 : Tuple2D(rTuple)
68 /** Create a copy of a 2D integer Tuple
70 @param rTup
71 The 2D Tuple which will be copied.
73 BASEGFX_DLLPUBLIC explicit B2DTuple(const B2ITuple& rTup);
75 // operators
77 B2DTuple operator-(void) const
79 return B2DTuple(-mnX, -mnY);
82 BASEGFX_DLLPUBLIC static const B2DTuple& getEmptyTuple();
85 // external operators
88 inline B2DTuple absolute(const B2DTuple& rTup)
90 B2DTuple aAbs(
91 fabs(rTup.getX()),
92 fabs(rTup.getY()));
93 return aAbs;
96 inline B2DTuple interpolate(const B2DTuple& rOld1, const B2DTuple& rOld2, double t)
98 if(rOld1 == rOld2)
100 return rOld1;
102 else if(0.0 >= t)
104 return rOld1;
106 else if(1.0 <= t)
108 return rOld2;
110 else
112 return B2DTuple(
113 ((rOld2.getX() - rOld1.getX()) * t) + rOld1.getX(),
114 ((rOld2.getY() - rOld1.getY()) * t) + rOld1.getY());
118 inline B2DTuple average(const B2DTuple& rOld1, const B2DTuple& rOld2)
120 return B2DTuple(
121 rtl_math_approxEqual(rOld1.getX(), rOld2.getX()) ? rOld1.getX() : (rOld1.getX() + rOld2.getX()) * 0.5,
122 rtl_math_approxEqual(rOld1.getY(), rOld2.getY()) ? rOld1.getY() : (rOld1.getY() + rOld2.getY()) * 0.5);
125 inline B2DTuple operator*(const B2DTuple& rTup, double t)
127 B2DTuple aNew(rTup);
128 aNew *= t;
129 return aNew;
132 inline B2DTuple operator*(double t, const B2DTuple& rTup)
134 B2DTuple aNew(rTup);
135 aNew *= t;
136 return aNew;
139 inline B2DTuple operator/(const B2DTuple& rTup, double t)
141 B2DTuple aNew(rTup);
142 aNew /= t;
143 return aNew;
146 /** Round double to nearest integer for 2D tuple
148 @return the nearest integer for this tuple
150 BASEGFX_DLLPUBLIC B2ITuple fround(const B2DTuple& rTup);
151 } // end of namespace basegfx
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */