Skip to content

Commit 87119f3

Browse files
committed
GridCreator moved to Models assembly; code cleanup (reducing StyleCop warnings)
1 parent 5f82b9a commit 87119f3

4 files changed

Lines changed: 63 additions & 45 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ Dist/*.exe
33

44
Src/*.suo
55
Src/**/[Bb]in/
6-
Src/**/[Oo]bj/
6+
Src/**/[Oo]bj/
7+
8+
Src/**/StyleCop.Cache

Src/ScreenGrid.ViewModels/GridCreator.cs renamed to Src/ScreenGrid.Models/Grids/GridCreator.cs

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,43 @@
1-
namespace ScreenGrid.ViewModels
1+
namespace ScreenGrid.Models.Grids
22
{
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
66

77
using ScreenGrid.Models.Geometry;
8-
using ScreenGrid.Models.Grids;
98

10-
public class GridCreator
9+
public static class GridCreator
1110
{
12-
static GridCreator()
13-
{
14-
15-
}
16-
17-
private static IEnumerable<Line> CreateLines(Point[] points)
18-
{
19-
var res = new List<Line>();
20-
21-
for (var i = 0; i < points.Length; i += 2)
22-
{
23-
res.Add(new Line(new Point(points[i].X, points[i].Y), new Point(points[i + 1].X, points[i + 1].Y)));
24-
}
25-
26-
return res;
27-
}
28-
29-
public static Line[] Transform(IEnumerable<Line> src,
30-
Rotation rotation, bool isFlippedHorizontal, bool isFlippedVertical,
31-
double RenderWidth, double RenderHeight)
11+
public static Line[] Transform(
12+
IEnumerable<Line> src,
13+
Rotation rotation,
14+
bool isFlippedHorizontal,
15+
bool isFlippedVertical,
16+
double width,
17+
double height)
3218
{
3319
var lines = src.ToArray<Line>();
3420
var res = new Line[lines.Length];
3521

3622
for (var i = 0; i < lines.Length; i++)
3723
{
38-
var newp1 = TransformPoint(lines[i].p1, rotation, isFlippedHorizontal, isFlippedVertical, RenderWidth, RenderHeight);
39-
var newp2 = TransformPoint(lines[i].p2, rotation, isFlippedHorizontal, isFlippedVertical, RenderWidth, RenderHeight);
24+
var newp1 = TransformPoint(lines[i].p1, rotation, isFlippedHorizontal, isFlippedVertical, width, height);
25+
var newp2 = TransformPoint(lines[i].p2, rotation, isFlippedHorizontal, isFlippedVertical, width, height);
4026
res[i] = new Line(newp1, newp2);
4127
}
4228

4329
return res;
4430
}
4531

46-
public static Point TransformPoint(Point pts,
47-
Rotation rotation, bool isFlippedHorizontal, bool isFlippedVertical,
48-
double RenderWidth, double RenderHeight)
32+
public static Point TransformPoint(
33+
Point point,
34+
Rotation rotation,
35+
bool isFlippedHorizontal,
36+
bool isFlippedVertical,
37+
double width,
38+
double height)
4939
{
50-
var res = new Point(pts.X, pts.Y);
40+
var res = new Point(point.X, point.Y);
5141

5242
if (rotation != Rotation.R0)
5343
{
@@ -57,8 +47,8 @@ public static Point TransformPoint(Point pts,
5747
var cy = 0.5;
5848
res.X -= cx;
5949
res.Y -= cy;
60-
var xnew = res.X * Math.Cos(angle) - res.Y * Math.Sin(angle);
61-
var ynew = res.X * Math.Sin(angle) + res.Y * Math.Cos(angle);
50+
var xnew = (res.X * Math.Cos(angle)) - (res.Y * Math.Sin(angle));
51+
var ynew = (res.X * Math.Sin(angle)) + (res.Y * Math.Cos(angle));
6252
res.X = xnew + cx;
6353
res.Y = ynew + cy;
6454
}
@@ -75,20 +65,28 @@ public static Point TransformPoint(Point pts,
7565
}
7666

7767
// Denormalize (to screen coordinate system)
78-
res.X *= RenderWidth;
79-
res.Y *= RenderHeight;
68+
res.X *= width;
69+
res.Y *= height;
8070

8171
return res;
8272
}
8373

84-
public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, double height)
74+
/// <summary>
75+
/// Creates list of lines, which forms the selected grid type
76+
/// </summary>
77+
/// <param name="gridType">Selected type of grid</param>
78+
/// <param name="width">Width of output image</param>
79+
/// <param name="height">Height of output image</param>
80+
/// <returns>List of lines</returns>
81+
public static IEnumerable<Line> CreateGrid(GridType gridType, double width, double height)
8582
{
86-
switch (gridMode)
83+
switch (gridType)
8784
{
8885
case GridType.None:
8986
{
9087
return new List<Line>();
9188
}
89+
9290
case GridType.Thirds:
9391
{
9492
var points = new[]
@@ -101,17 +99,19 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
10199

102100
return CreateLines(points);
103101
}
102+
104103
case GridType.DiagonalOfThirds:
105104
{
106105
var points = new[]
107106
{
108107
new Point(0.0, 1.0), new Point(1.0, 0.0),
109-
new Point(0.0, 1.0 - RatioConstants.OneThird / 2.0), new Point(1.0 - RatioConstants.OneThird / 2.0, 0.0),
108+
new Point(0.0, 1.0 - (RatioConstants.OneThird / 2.0)), new Point(1.0 - (RatioConstants.OneThird / 2.0), 0.0),
110109
new Point(RatioConstants.OneThird / 2.0, 1.0), new Point(1.0, RatioConstants.OneThird / 2.0),
111110
};
112111

113112
return CreateLines(points);
114113
}
114+
115115
case GridType.GoldenRatio:
116116
{
117117
var points = new[]
@@ -124,6 +124,7 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
124124

125125
return CreateLines(points);
126126
}
127+
127128
case GridType.GoldenTriangle:
128129
{
129130
var srcPoints = new List<Point>(6);
@@ -154,6 +155,7 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
154155

155156
return CreateLines(srcPoints.ToArray());
156157
}
158+
157159
case GridType.GoldenDiagonal:
158160
{
159161
var points = new[]
@@ -165,6 +167,7 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
165167

166168
return CreateLines(points);
167169
}
170+
168171
case GridType.FibonacciRectangles:
169172
{
170173
var points = new[]
@@ -173,20 +176,21 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
173176
new Point(RatioConstants.Phi5D8, RatioConstants.Phi3D8), new Point(1.0, RatioConstants.Phi3D8),
174177

175178
// v
176-
new Point(RatioConstants.Phi5D8 + RatioConstants.Phi3D8*RatioConstants.Phi3D8, RatioConstants.Phi3D8),
177-
new Point(RatioConstants.Phi5D8 + RatioConstants.Phi3D8*RatioConstants.Phi3D8, 0.0),
179+
new Point(RatioConstants.Phi5D8 + (RatioConstants.Phi3D8 * RatioConstants.Phi3D8), RatioConstants.Phi3D8),
180+
new Point(RatioConstants.Phi5D8 + (RatioConstants.Phi3D8 * RatioConstants.Phi3D8), 0.0),
178181

179182
// h
180-
new Point(RatioConstants.Phi5D8 + RatioConstants.Phi3D8*RatioConstants.Phi3D8, RatioConstants.Phi3D8*RatioConstants.Phi5D8),
181-
new Point(RatioConstants.Phi5D8, RatioConstants.Phi3D8*RatioConstants.Phi5D8),
183+
new Point(RatioConstants.Phi5D8 + (RatioConstants.Phi3D8 * RatioConstants.Phi3D8), RatioConstants.Phi3D8 * RatioConstants.Phi5D8),
184+
new Point(RatioConstants.Phi5D8, RatioConstants.Phi3D8 * RatioConstants.Phi5D8),
182185

183186
// v
184-
new Point(RatioConstants.Phi5D8 + RatioConstants.Phi3D8*RatioConstants.Phi3D8*RatioConstants.Phi5D8, RatioConstants.Phi3D8),
185-
new Point(RatioConstants.Phi5D8 + RatioConstants.Phi3D8*RatioConstants.Phi3D8*RatioConstants.Phi5D8, RatioConstants.Phi3D8*RatioConstants.Phi5D8),
187+
new Point(RatioConstants.Phi5D8 + (RatioConstants.Phi3D8 * RatioConstants.Phi3D8 * RatioConstants.Phi5D8), RatioConstants.Phi3D8),
188+
new Point(RatioConstants.Phi5D8 + (RatioConstants.Phi3D8 * RatioConstants.Phi3D8 * RatioConstants.Phi5D8), RatioConstants.Phi3D8 * RatioConstants.Phi5D8),
186189
};
187190

188191
return CreateLines(points);
189192
}
193+
190194
// TODO: case GridType.GoldenSpiral:
191195
////{
192196
//// var res = new List<Line>();
@@ -211,9 +215,21 @@ public static IEnumerable<Line> CreateGrid(GridType gridMode, double width, doub
211215
////}
212216
default:
213217
{
214-
throw new ArgumentException(gridMode.ToString());
218+
throw new ArgumentException(gridType.ToString());
215219
}
216220
}
217221
}
222+
223+
private static IEnumerable<Line> CreateLines(Point[] points)
224+
{
225+
var res = new List<Line>();
226+
227+
for (var i = 0; i < points.Length; i += 2)
228+
{
229+
res.Add(new Line(new Point(points[i].X, points[i].Y), new Point(points[i + 1].X, points[i + 1].Y)));
230+
}
231+
232+
return res;
233+
}
218234
}
219235
}

Src/ScreenGrid.Models/ScreenGrid.Models.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="Geometry\Point.cs" />
4747
<Compile Include="Geometry\Rectangle.cs" />
4848
<Compile Include="Geometry\Utils.cs" />
49+
<Compile Include="Grids\GridCreator.cs" />
4950
<Compile Include="Grids\GridModeItem.cs" />
5051
<Compile Include="Grids\GridType.cs" />
5152
<Compile Include="Grids\Orientation.cs" />

Src/ScreenGrid.ViewModels/ScreenGrid.ViewModels.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<Reference Include="WindowsBase" />
4242
</ItemGroup>
4343
<ItemGroup>
44-
<Compile Include="GridCreator.cs" />
4544
<Compile Include="ScreenGridViewModel.cs" />
4645
<Compile Include="Properties\AssemblyInfo.cs" />
4746
<Compile Include="Utils\BaseNotifyPropertyChanged.cs" />

0 commit comments

Comments
 (0)