Skip to content

Commit

Permalink
Corrected some errors on the areas defining the Duval Pentagons
Browse files Browse the repository at this point in the history
  • Loading branch information
code-recipes committed Oct 10, 2017
1 parent 1c00c35 commit 12abde5
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 24 deletions.
Binary file modified Distribution/xDGA Sample.xlsx
Binary file not shown.
Binary file modified Distribution/xDGA.ADDIN.xll
Binary file not shown.
Binary file modified Distribution/xDGA.ADDIN64.xll
Binary file not shown.
Binary file added Distribution/xDGA32.zip
Binary file not shown.
Binary file added Distribution/xDGA64.zip
Binary file not shown.
7 changes: 7 additions & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# xDGA

## Version 0.6
### October 2017

#### xDGA.CORE
* Corrected Duval Pentagons areas. Thanks to Nick and Ania from work who
pointed out the issue and suggested a solution.

## Version 0.5
### August 2017

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using xDGA.CORE.Interfaces;
Expand Down
19 changes: 11 additions & 8 deletions xDGA.CORE/Algorithms/DuvalPentagons/DuvalPentagonOneRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DuvalPentagonOneRule() : base("Duval Pentagon 1", Gas.Hydrogen, Gas.Ethan
Coordinates =
{
new CartesianCoordinate(0.0, 40.0),
new CartesianCoordinate(38.0, 12.0),
new CartesianCoordinate(38.0, 12.4),
new CartesianCoordinate(32.0, -6.0),
new CartesianCoordinate(4.0, 16.0),
new CartesianCoordinate(0.0, 1.5)
Expand All @@ -67,18 +67,19 @@ public DuvalPentagonOneRule() : base("Duval Pentagon 1", Gas.Hydrogen, Gas.Ethan
Coordinates =
{
new CartesianCoordinate(24.0, -30.0),
new CartesianCoordinate(1.0, -32.0),
new CartesianCoordinate(1.0, -32.4),
new CartesianCoordinate(-23.3, -32.4),
new CartesianCoordinate(-6.0, -4.0),
new CartesianCoordinate(-1.0, -2.0)
new CartesianCoordinate(-1.0, -2.0)
}
});

Pentagon.Areas.Add(new Area(FailureType.Code.T2)
{
Coordinates =
{
new CartesianCoordinate(1.0, -32.0),
new CartesianCoordinate(-22.5, -32.0),
new CartesianCoordinate(1.0, -32.4),
new CartesianCoordinate(-22.5, -32.4),
new CartesianCoordinate(-6.0, -4.0)
}
});
Expand All @@ -89,9 +90,10 @@ public DuvalPentagonOneRule() : base("Duval Pentagon 1", Gas.Hydrogen, Gas.Ethan
{
new CartesianCoordinate(0.0, 1.5),
new CartesianCoordinate(-35.0, 3.0),
new CartesianCoordinate(-22.5, -32.0),
new CartesianCoordinate(-22.5, -32.4),
new CartesianCoordinate(-6.0, -4.0),
new CartesianCoordinate(-1.0, -2.0)
new CartesianCoordinate(-1.0, -2.0),
new CartesianCoordinate(-23.3, -32.4)
}
});

Expand All @@ -105,7 +107,8 @@ public DuvalPentagonOneRule() : base("Duval Pentagon 1", Gas.Hydrogen, Gas.Ethan
new CartesianCoordinate(-1.0, 24.5),
new CartesianCoordinate(-1.0, 33.0),
new CartesianCoordinate(0.0, 33.0),
new CartesianCoordinate(0.0, 40.0)
new CartesianCoordinate(0.0, 40.0),
new CartesianCoordinate(-38.0, -12.4)
}
});
}
Expand Down
18 changes: 11 additions & 7 deletions xDGA.CORE/Algorithms/DuvalPentagons/DuvalPentagonTwoRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public DuvalPentagonTwoRule() : base("Duval Pentagon 2", Gas.Hydrogen, Gas.Ethan
Coordinates =
{
new CartesianCoordinate(0.0, 40.0),
new CartesianCoordinate(38.0, 12.0),
new CartesianCoordinate(38.0, 12.4),
new CartesianCoordinate(32.0, -6.0),
new CartesianCoordinate(4.0, 16.0),
new CartesianCoordinate(0.0, 1.5)
Expand Down Expand Up @@ -72,7 +72,8 @@ public DuvalPentagonTwoRule() : base("Duval Pentagon 2", Gas.Hydrogen, Gas.Ethan
new CartesianCoordinate(-1.0, 24.5),
new CartesianCoordinate(-1.0, 33.0),
new CartesianCoordinate(0.0, 33.0),
new CartesianCoordinate(0.0, 40.0)
new CartesianCoordinate(0.0, 40.0),
new CartesianCoordinate(-38.0, -12.4)
}
});

Expand All @@ -82,31 +83,34 @@ public DuvalPentagonTwoRule() : base("Duval Pentagon 2", Gas.Hydrogen, Gas.Ethan
{
new CartesianCoordinate(24.0, -30.0),
new CartesianCoordinate(-3.5, -3.0),
new CartesianCoordinate(2.5, -32.0)
new CartesianCoordinate(-1.0, -2.0),
new CartesianCoordinate(2.5, -32.4),
new CartesianCoordinate(23.3, -32.4)
}
});

Pentagon.Areas.Add(new Area(FailureType.Code.C)
{
Coordinates =
{
new CartesianCoordinate(2.5, -32.0),
new CartesianCoordinate(2.5, -32.4),
new CartesianCoordinate(-3.5, -3.0),
new CartesianCoordinate(-11.0, -8.0),
new CartesianCoordinate(-21.5, -32.0)
new CartesianCoordinate(-21.5, -32.4)
}
});

Pentagon.Areas.Add(new Area(FailureType.Code.O)
{
Coordinates =
{
new CartesianCoordinate(-21.5, -32.0),
new CartesianCoordinate(-21.5, -32.4),
new CartesianCoordinate(-11.0, -8.0),
new CartesianCoordinate(-3.5, -3.0),
new CartesianCoordinate(-1.0, -2.0),
new CartesianCoordinate(0.0, 1.5),
new CartesianCoordinate(-35.0, 3.0)
new CartesianCoordinate(-35.0, 3.0),
new CartesianCoordinate(-23.3, -32.4)
}
});
}
Expand Down
71 changes: 63 additions & 8 deletions xDGA.CORE/Models/Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// SOFTWARE.

using System;
using System.Linq;
using System.Collections.Generic;

namespace xDGA.CORE.Models
Expand Down Expand Up @@ -110,25 +111,79 @@ public CartesianCoordinate GetCoordinate(double x, double y)
return Coordinates.Find(c => { return c.X == x && c.Y == y; });
}

/// <summary>
/// Ensure coordinates are sorted in a counter-clockwise position
/// </summary>
private void SortCoordinates()
{
var angles = new Dictionary<double,CartesianCoordinate>();

if (Coordinates.Count > 0)
{
foreach (var coordinate in Coordinates)
{
angles.Add(coordinate.ToVector().Angle,coordinate);
}

angles.Values.ToList();

var list = angles.Keys.ToList();
list.Sort();

Coordinates.Clear();

foreach (var angle in list)
{
Coordinates.Add(angles[angle]);
}
}
}

/// <summary>
/// Determines whether a point is inside this polygonal area.
/// If the point is at an edge or a vertex it is considered to be
/// inside the area. It uses the algorithm documented by Paul Bourke.
/// <see href="http://web.archive.org/web/20080812141848/http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/"/>
/// </summary>
/// <param name="coordinate">The <see cref="CartesianCoordinate"/> that will be checked.</param>
/// <returns></returns>
public bool CheckIfCoordinateIsInArea(CartesianCoordinate coordinate)
{
var isInside = false;
var x = coordinate.X;
var y = coordinate.Y;
int counter = 0;
int i = 1;
int n = Coordinates.Count;
double xinters;
CartesianCoordinate p1, p2;

var polySides = Coordinates.Count;
var j = polySides - 1;
p1 = Coordinates[0];

for (int i = 0; i < polySides; i++)
foreach (var point in Coordinates)
{
if ((((Coordinates[i].Y < y && Coordinates[j].Y >= y) || (Coordinates[j].Y < y && Coordinates[i].Y >= y)) && (Coordinates[i].X <= x || Coordinates[j].X <= x)))
p2 = Coordinates[i % n];

if (coordinate.Y > Math.Min(p1.Y,p2.Y))
{
isInside = isInside ^ ((Coordinates[i].X + (y - Coordinates[i].Y) / (Coordinates[j].Y - Coordinates[i].Y) * (Coordinates[j].X - Coordinates[i].X)) < x);
if(coordinate.Y <= Math.Max(p1.Y,p2.Y))
{
if(coordinate.X <= Math.Max(p1.X,p2.X))
{
if(p1.Y != p2.Y)
{
xinters = (coordinate.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
if (p1.X == p2.X || coordinate.X <= xinters) counter++;
}
}
}
}

j = i;
p1 = p2;

i++;
}

if (counter % 2 != 0) isInside = true;

return isInside;
}
}
Expand Down
1 change: 1 addition & 0 deletions xDGA.CORE/xDGA.CORE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\xDGA.CORE.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down

0 comments on commit 12abde5

Please sign in to comment.