-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
243 lines (201 loc) · 7.53 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2020 Pietro Vitelli
*
* This file is part of OriFlagTess program.
*
* OriFlagTess is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* OriFlagTess is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OriFlagTess. If not, see<http://www.gnu.org/licenses/>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Flagstone_Tessellation___Molecule_construction
{
public static class Utils
{
public static double Cos(double value)
{
return Math.Round(Math.Cos(value), 5);
}
public static double Sin(double value)
{
return Math.Round(Math.Sin(value), 5);
}
public static double Tan(double value)
{
return Math.Round(Math.Tan(value), 5);
}
public static double Atan2(double value1, double value2)
{
return RadToDeg(Math.Atan2(value1, value2));
}
public static Line line(double x1, double y1, double x2, double y2, Brush brush, int lineStyle = 0)
{
Line line = new Line();
line.X1 = x1;
line.X2 = x2;
line.Y1 = y1;
line.Y2 = y2;
if(lineStyle != 0)
line.StrokeDashArray = toDashArrayPattern(lineStyle);
line.StrokeThickness = 1;
line.Stroke = brush;
Canvas.SetZIndex(line, 100);
return line;
}
public static DoubleCollection toDashArrayPattern(int lineStyle)
{
DoubleCollection pattern = new DoubleCollection();
switch (lineStyle)
{
case 1:
pattern.Add(8); pattern.Add(2); pattern.Add(1); pattern.Add(2);
break;
case 2:
pattern.Add(8); pattern.Add(3);
break;
case 3:
pattern.Add(4); pattern.Add(2);
break;
default:
break;
}
return pattern;
}
public static Line lineDash(double x1, double y1, double x2, double y2, Brush brush)
{
Line line = new Line();
line.X1 = x1;
line.X2 = x2;
line.Y1 = y1;
line.Y2 = y2;
DoubleCollection dashes = new DoubleCollection();
dashes.Add(5);
dashes.Add(5);
line.StrokeDashArray = dashes;
line.StrokeThickness = 1;
line.Stroke = brush;
Canvas.SetZIndex(line, 100);
return line;
}
public static Rectangle rectangle(double width, double height, double left, double top, Brush brush)
{
Rectangle r = new Rectangle();
r.Fill = brush;
r.Width = width;
r.Height = height;
Canvas.SetLeft(r, left);
Canvas.SetTop(r, top);
return r;
}
public static Polygon polygon(PointCollection points, Brush brush)
{
Polygon p = new Polygon();
p.Fill = brush;
p.Points = points;
return p;
}
public static Ellipse ellipse(double width, double height, double left, double top, Brush brush)
{
Ellipse ellipse = new Ellipse();
ellipse.Width = width;
ellipse.Height = height;
ellipse.Stroke = brush;
ellipse.Fill = brush;
Canvas.SetLeft(ellipse, left);
Canvas.SetTop(ellipse, top);
return ellipse;
}
public static double DegToRad(double angle)
{
return Math.PI * angle / 180.0;
}
public static double RadToDeg(double angle)
{
return angle * (180.0 / Math.PI);
}
public static Point RotTras(Point p, double angle, Point tras)
{
Point newP = new Point();
//Rotation
newP.X = p.X * Utils.Cos(angle) - p.Y * Utils.Sin(angle);
newP.Y = p.X * Utils.Sin(angle) + p.Y * Utils.Cos(angle);
//Traslation
newP.X = newP.X + tras.X;
newP.Y = newP.Y + tras.Y;
return newP;
}
public static Point MultipleRotTras(Point p, List<double> angles, List<Point> tras)
{
Point newP = p;
for (int i = 0; i < angles.Count; i++)
{
newP = RotTras(newP, angles[i], tras[i]);
}
return newP;
}
public static List<double> calculate4Sides(List<double> sides, List<double> angles)
{
Point C = new Point();
C.X = sides[0] - sides[1] * Math.Cos(Math.PI - angles[1]);
C.Y = sides[1] * Math.Sin(Math.PI - angles[1]);
double AC = Math.Sqrt(Math.Pow(C.X, 2) + Math.Pow(C.Y, 2));
double angleDAC = Math.PI - angles[0] - Math.Atan2(C.Y, C.X);
double angleDCA = Math.PI - angles[2] - (angles[1] - Math.Atan2(C.Y, C.X));
double CD = AC * Math.Sin(angleDAC) / Math.Sin(Math.PI - angles[3]);
double AD = AC * Math.Sin(angleDCA) / Math.Sin(Math.PI - angles[3]);
return new List<double>{ Math.Round(sides[0], 3), Math.Round(sides[1], 3), Math.Round(CD, 3), Math.Round(AD, 3) };
}
public static Point Rot(Point p, double angle, Point rotCenter)
{
Point newP = new Point();
//Rotation
newP.X = (p.X-rotCenter.X) * Utils.Cos(angle) + (p.Y-rotCenter.Y) * Utils.Sin(angle) + rotCenter.X;
newP.Y = -(p.X - rotCenter.X) * Utils.Sin(angle) + (p.Y-rotCenter.Y) * Utils.Cos(angle) + rotCenter.Y;
return newP;
}
public static Point Tras(Point p, Point trasCenter)
{
Point newP = new Point();
//Rotation
newP.X = p.X + trasCenter.X;
newP.Y = p.Y + trasCenter.Y;
return newP;
}
public static List<double> calculatePolygonSides(List<double> sides, List<double> angles)
{
Point V = new Point(sides[0],0);
double rotAngle = 0;
for (int i = 1; i < angles.Count-2; i++)
{
Point init = V;
rotAngle -= angles[i];
V = Tras(V, new Point(sides[i],0));
V = Rot(V, rotAngle, init);
}
rotAngle -= angles[angles.Count - 2];
sides[angles.Count - 2] = Math.Round(( - V.Y - V.X * Utils.Tan(angles[0]) ) / (Utils.Cos(rotAngle) * Utils.Tan(angles[0]) - Utils.Sin(rotAngle) ), 3);
if(Utils.Cos(angles[0]) == 0)
sides[angles.Count - 1] = Math.Round((-sides[angles.Count - 2] * Utils.Sin(rotAngle) + V.Y) / (Utils.Sin(angles[0])),3);
else
sides[angles.Count - 1] = Math.Round((sides[angles.Count - 2] * Utils.Cos(rotAngle) + V.X) / (-Utils.Cos(angles[0])),3);
return sides;
}
}
}