Sat, 24 Mar 2018 12:54:28 +0200
Happy new year 2018
1326 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
1319 | 19 | #include "geometry.h" |
20 | ||
21 | /* | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
22 | * LDraw uses 4 points of precision for sin and cos values. Primitives must be generated |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
23 | * accordingly. |
1319 | 24 | */ |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
25 | double ldrawsin(double angle) |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
26 | { |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
27 | return roundToDecimals(sin(angle), 4); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
28 | } |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
29 | |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
30 | double ldrawcos(double angle) |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
31 | { |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
32 | return roundToDecimals(cos(angle), 4); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
33 | } |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
34 | |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
35 | /* |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
36 | * Returns a point on a circumference. LDraw precision is used. |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
37 | */ |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
38 | QPointF pointOnLDrawCircumference(int segment, int divisions) |
1319 | 39 | { |
40 | double angle = (segment * 2 * pi) / divisions; | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
41 | return {ldrawcos(angle), ldrawsin(angle)}; |
1319 | 42 | } |
43 | ||
44 | /* | |
45 | * makeCircle | |
46 | * | |
47 | * Creates a possibly partial circle rim. | |
48 | * Divisions is how many segments the circle makes if up if it's full. | |
49 | * Segments is now many segments are added. | |
50 | * Radius is the radius of the circle. | |
51 | * | |
52 | * If divisions == segments, this yields a full circle rim. | |
53 | * The rendered circle is returned as a vector of lines. | |
54 | */ | |
55 | QVector<QLineF> makeCircle(int segments, int divisions, double radius) | |
56 | { | |
57 | QVector<QLineF> lines; | |
58 | ||
59 | for (int i = 0; i < segments; i += 1) | |
60 | { | |
1325
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
61 | QPointF p0 = radius * ::pointOnLDrawCircumference(i, divisions); |
f9abfc7ba676
Use 4 points of precision for circle point coordinates
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
62 | QPointF p1 = radius * ::pointOnLDrawCircumference(i + 1, divisions); |
1319 | 63 | lines.append({p0, p1}); |
64 | } | |
65 | ||
66 | return lines; | |
67 | } | |
68 | ||
69 | /* | |
70 | * Computes the shortest distance from a point to a rectangle. | |
71 | * | |
72 | * The code originates from the Unity3D wiki, and was translated from C# to Qt by me (Teemu Piippo): | |
73 | * | |
74 | * Original code: | |
75 | * http://wiki.unity3d.com/index.php/Distance_from_a_point_to_a_rectangle | |
76 | * | |
77 | * Copyright 2013 Philip Peterson. | |
78 | * | |
79 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
80 | * of this software and associated documentation files (the "Software"), to | |
81 | * deal in the Software without restriction, including without limitation the | |
82 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
83 | * sell copies of the Software, and to permit persons to whom the Software is | |
84 | * furnished to do so, subject to the following conditions: | |
85 | * | |
86 | * The above copyright notice and this permission notice shall be included in | |
87 | * all copies or substantial portions of the Software. | |
88 | * | |
89 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
90 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
91 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
92 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
93 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
94 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
95 | * IN THE SOFTWARE. | |
96 | */ | |
97 | qreal distanceFromPointToRectangle(const QPointF& point, const QRectF& rectangle) | |
98 | { | |
99 | // Calculate a distance between a point and a rectangle. | |
100 | // The area around/in the rectangle is defined in terms of | |
101 | // several regions: | |
102 | // | |
103 | // O--x | |
104 | // | | |
105 | // y | |
106 | // | |
107 | // | |
108 | // I | II | III | |
109 | // ======+==========+====== --yMin | |
110 | // VIII | IX (in) | IV | |
111 | // ======+==========+====== --yMax | |
112 | // VII | VI | V | |
113 | // | |
114 | // | |
115 | // Note that the +y direction is down because of Unity's GUI coordinates. | |
116 | // - I don't care which way is +y in this function because I want a distance --TP | |
117 | ||
118 | if (point.x() < rectangle.left()) | |
119 | { | |
120 | // Region I, VIII, or VII | |
121 | if (point.y() < rectangle.top()) // I | |
122 | return QLineF {point, rectangle.topLeft()}.length(); | |
123 | else if (point.y() > rectangle.bottom()) // VII | |
124 | return QLineF {point, rectangle.bottomLeft()}.length(); | |
125 | else // VIII | |
126 | return rectangle.left() - point.x(); | |
127 | } | |
128 | else if (point.x() > rectangle.right()) | |
129 | { | |
130 | // Region III, IV, or V | |
131 | if (point.y() < rectangle.top()) // III | |
132 | return QLineF {point, rectangle.topRight()}.length(); | |
133 | else if (point.y() > rectangle.bottom()) // V | |
134 | return QLineF {point, rectangle.bottomRight()}.length(); | |
135 | else // IV | |
136 | return point.x() - rectangle.right(); | |
137 | } | |
138 | else | |
139 | { | |
140 | // Region II, IX, or VI | |
141 | if (point.y() < rectangle.top()) // II | |
142 | return rectangle.top() - point.y(); | |
143 | else if (point.y() > rectangle.bottom()) // VI | |
144 | return point.y() - rectangle.bottom(); | |
145 | else // IX | |
146 | return 0; | |
147 | } | |
148 | } |