Debian Codegraph @

src/engine/math_tools.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2020 - 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "math_tools.h"
22
23 #include <algorithm>
24 #include <cmath>
25 #include <cstdlib>
26
27 double fheroes2::GetAngle( const Point & start, const Point & target )
28 {
29 const int dx = target.x - start.x;
30 const int dy = target.y - start.y;
31 double angle = atan2( -dy, dx ) * 180.0 / M_PI;
32 // we only care about two quadrants, normalize
33 if ( dx < 0 ) {
34 angle = ( dy <= 0 ) ? 180 - angle : -angle - 180;
35 }
36 return angle;
37 }
38
39 std::vector<fheroes2::Point> fheroes2::getLinePoints( const Point & pt1, const Point & pt2, const uint32_t step )
40 {
41 const int dx = pt2.x - pt1.x;
42 const int dy = pt2.y - pt1.y;
43 const uint32_t dist = static_cast<uint32_t>( std::hypot( dx, dy ) );
44 // Round up the integer division and avoid the division by zero in calculation of total line points.
45 const uint32_t intervalsBetweenPointsCount = ( step > 0 ) ? ( dist + step / 2 ) / step : 0;
46
47 if ( intervalsBetweenPointsCount < 2 ) {
48 // If there are no intervals than 'pt2' could be closer to 'pt1' than 'step'.
49 // In this case we put 'pt1' as the start of the line.
50 // And put 'pt2' as the end of the line only if 'pt1' is not equal to 'pt2'.
51 if ( pt1 == pt2 ) {
52 return { pt1 };
53 }
54
55 return { pt1, pt2 };
56 }
57
58 // Otherwise we calculate the euclidean line, using the determined parameters.
59 const double stepX = dx / static_cast<double>( intervalsBetweenPointsCount );
60 const double stepY = dy / static_cast<double>( intervalsBetweenPointsCount );
61
62 std::vector<Point> line;
63 line.reserve( intervalsBetweenPointsCount + 1 );
64
65 // Line always starts from `pt1`.
66 line.emplace_back( pt1 );
67
68 double offsetX = static_cast<double>( pt1.x );
69 double offsetY = static_cast<double>( pt1.y );
70
71 for ( uint32_t i = 1; i < intervalsBetweenPointsCount; ++i ) {
72 offsetX += stepX;
73 offsetY += stepY;
74
75 line.emplace_back( static_cast<int32_t>( offsetX ), static_cast<int32_t>( offsetY ) );
76 }
77
78 // And ends in `pt2`.
79 line.emplace_back( pt2 );
80
81 return line;
82 }
83
84 std::vector<fheroes2::Point> fheroes2::GetArcPoints( const Point & from, const Point & to, const int32_t arcHeight, const int32_t step )
85 {
86 std::vector<Point> res;
87 Point pt( from );
88 // The first projectile point is "from"
89 res.push_back( pt );
90
91 // Calculate the number of projectile trajectory points
92 const int32_t steps = ( to.x - from.x ) / step;
93
94 // Trajectory start point coordinates
95 const double x1 = from.x;
96 const double y1 = from.y;
97
98 // Distance to the destination point along the axes
99 const double dx = to.x - x1;
100 const double dy = to.y - y1;
101
102 // The movement of the projectile is determined according to the parabolic
103 // throwing approximation. The first two parabola points are "from" and
104 // "to" with an exception that the second ("to") point is at the same
105 // height as the start point. The parabola third point "y" coordinate is
106 // set using the "arcHeight" parameter, which determines the height of the
107 // parabola arc. And its "x" coordinate is taken equal to half the path
108 // from the start point to the end point. Using this three point
109 // coordinates, a system of three linear equations (y=a*x*x+b*x+c) in
110 // three variables is solved by substituting these points "x" and "y".
111 // Considering that on an isometric battlefield, the target location above
112 // or below corresponds to a simple turn of the shooter to the left or
113 // right, a linear movement from point "from" to point "to" is added to the
114 // parabola ('dy/dx' in 'b' constant and '-x1*dy/dx' in 'c' constant).
115
116 // Calculation of the parabola equation coefficients
117 const double a = 4 * arcHeight / dx / dx;
118 const double b = dy / dx - a * ( dx + 2 * x1 );
119 const double c = y1 + a * x1 * ( dx + x1 ) - x1 * dy / dx;
120
121 for ( int32_t i = 1; i <= steps; ++i ) {
122 pt.x += step;
123 pt.y = static_cast<int32_t>( std::lround( a * pt.x * pt.x + b * pt.x + c ) );
124 res.push_back( pt );
125 }
126
127 return res;
128 }
129
130 int32_t fheroes2::GetRectIndex( const std::vector<Rect> & rects, const Point & pt )
131 {
132 for ( size_t i = 0; i < rects.size(); ++i ) {
133 if ( rects[i] & pt )
134 return static_cast<int32_t>( i );
135 }
136
137 return -1;
138 }
139
140 fheroes2::Rect fheroes2::getBoundaryRect( const Rect & rt1, const Rect & rt2 )
141 {
142 if ( rt2.width == 0 && rt2.height == 0 ) {
143 return rt1;
144 }
145
146 if ( rt1.width == 0 && rt1.height == 0 ) {
147 return rt2;
148 }
149
150 const int32_t x = std::min( rt1.x, rt2.x );
151 const int32_t y = std::min( rt1.y, rt2.y );
152 const int32_t width = std::max( rt1.x + rt1.width, rt2.x + rt2.width ) - x;
153 const int32_t height = std::max( rt1.y + rt1.height, rt2.y + rt2.height ) - y;
154
155 return { x, y, width, height };
156 }