1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3
4 /*
5 Rosegarden
6 A sequencer and musical notation editor.
7 Copyright 2000-2021 the Rosegarden development team.
8 See the AUTHORS file for more details.
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version. See the file
14 COPYING included with this distribution for more information.
15 */
16
17 #include "Equation.h"
18
19 namespace Rosegarden {
20
solve(Unknown u,double & y,double & m,double & x,double & c)21 void Equation::solve(Unknown u, double &y, double &m, double &x, double &c)
22 {
23 switch(u) {
24 case Y: y = m*x + c; break;
25 case M: m = (y - c) / x; break;
26 case X: x = (y - c) / m; break;
27 case C: c = y - m*x; break;
28 }
29 }
30
solve(Unknown u,int & y,double & m,int & x,int & c)31 void Equation::solve(Unknown u, int &y, double &m, int &x, int &c)
32 {
33 switch(u) {
34 case Y: y = static_cast<int>(m*x) + c; break;
35 case M: m = static_cast<double>(y - c) / static_cast<double>(x); break;
36 case X: x = static_cast<int>(static_cast<float>(y - c) / m); break;
37 case C: c = y - static_cast<int>(m*x); break;
38 }
39 }
40
solveForYByEndPoints(Point a,Point b,double x,double & y)41 void Equation::solveForYByEndPoints(Point a, Point b, double x, double &y)
42 {
43 double m, c, y1, x1;
44
45 m = static_cast<double>(b.y - a.y) / static_cast<double>(b.x - a.x);
46
47 x1 = a.x; y1 = a.y;
48 solve(C, y1, m, x1, c);
49 solve(Y, y, m, x, c);
50 }
51
solveForYByEndPoints(Point a,Point b,int x,int & y)52 void Equation::solveForYByEndPoints(Point a, Point b, int x, int &y)
53 {
54 double m;
55 int c;
56
57 m = static_cast<double>(b.y - a.y) / static_cast<double>(b.x - a.x);
58
59 solve(C, a.y, m, a.x, c);
60 solve(Y, y, m, x, c);
61 }
62
63 }
64