Skip to content

Commit

Permalink
Configuration of Plotter
Browse files Browse the repository at this point in the history
  • Loading branch information
smarthome committed Aug 10, 2023
1 parent 4a5caf8 commit 7b42aad
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 114 deletions.
27 changes: 27 additions & 0 deletions FamilyTree/Configuration/FamilySearchConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Drawing;

namespace FamilyTree.Configuration
{
Expand Down Expand Up @@ -29,5 +30,31 @@ public IEnumerable<float> GetCreateRadii()

public int GetNumberOfGenerations() => numberOfGenerations;

public IEnumerable<TextInfo> GetTextInfo(Person person, int currentGeneration)
{
if (currentGeneration < 6)
{
yield return new TextInfo(person.Name, Color.Black, 12);
yield return new TextInfo(person.FamilyName, Color.Black, 12);
yield return new TextInfo(createYearText(person), Color.FromArgb(255, 139, 139, 142), 12);
}
else if (currentGeneration < 7)
{
yield return new TextInfo(person.Name, Color.Black, 12);
yield return new TextInfo(person.FamilyName, Color.Black, 12);
}
else
{
yield return new TextInfo($"{person.Name} {person.FamilyName}", Color.Black, 12);
}
}

private static string createYearText(Person person)
{
if (person.IsAlive)
return $"{person.BirthYear} - Lebend";
else
return $"{person.BirthYear} - {person.DeathYear}";
}
}
}
4 changes: 3 additions & 1 deletion FamilyTree/Configuration/IConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Drawing;

namespace FamilyTree.Configuration
{
{
public interface IConfiguration
{
int GetNumberOfGenerations();
IEnumerable<float> GetCreateRadii();
IEnumerable<TextInfo> GetTextInfo(Person person, int currentGeneration);

}
}
19 changes: 19 additions & 0 deletions FamilyTree/Configuration/TextInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;

namespace FamilyTree.Configuration
{
public class TextInfo
{
public string Text { get; }

public TextInfo(string text, Color color, float fontSize)
{
Text = text;
Color = color;
FontSize = fontSize;
}

public Color Color { get; }
public float FontSize { get; }
}
}
196 changes: 83 additions & 113 deletions FamilyTree/TextPlotter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ public static void PlotText(SvgDocument document, PointF centre, Person model, I
{
var radii = configuration.GetCreateRadii().Intersect().Compute(i => (i.start + i.end) / 2.0f).ToArray();

// Main Person
plotMainPerson(document, centre, model, 12);
// Main Person (Generation 1)
plotMainPerson(document, centre, model, configuration);

// Generation 1
var angleSets_1 = MathHelper.GetCreateAngles(2).Intersect().ToArray();
var personSet_1 = MathHelper.GetPersonsOfLevel(model, 2).ToArray();
plotInnerPersons(document, centre, radii[0], angleSets_1, personSet_1, 12);

// Generation 2
var angleSets_2 = MathHelper.GetCreateAngles(3).Intersect().ToArray();
var personSet_2 = MathHelper.GetPersonsOfLevel(model, 3).ToArray();
plotInnerPersons(document, centre, radii[1], angleSets_2, personSet_2, 12);
// Generation 2 .. 3
for (int i = 2; i < 4; ++i)
{
var angleSets_N = MathHelper.GetCreateAngles(i).Intersect().ToArray();
var personSet_N = MathHelper.GetPersonsOfLevel(model, i).ToArray();
for (int j = 0; j < angleSets_N.Length; ++j)
plotInnerPerson(document, centre, radii[i-2], angleSets_N[j], personSet_N[j], i, configuration);
}

// Generation 3 .. n
// Generation 4 .. n
for (int i = 3; i < configuration.GetNumberOfGenerations(); ++i)
{
var angleSet_N = MathHelper.GetCreateAngles(i+1).Intersect().Compute(i => (i.start + i.end) / 2.0f).ToArray();
var personSet_N = MathHelper.GetPersonsOfLevel(model, i+1).ToArray();
plotOuterPersons(document, centre, radii[i-1], angleSet_N, personSet_N, i, getFontForLevel(i));
for (int j = 0; j < angleSet_N.Length; ++j)
plotOuterPerson(document, centre, radii[i - 1], angleSet_N[j], personSet_N[j], i, configuration);
}
}

Expand All @@ -45,127 +45,97 @@ private static float getFontForLevel(int currentGeneration)
return 3.0f;
}

private static void plotOuterPersons(SvgDocument document, PointF centre, float radius, float[] angleSets, Person[] personSet, int currentGeneration, float fontSize)
{
for (int i = 0; i < angleSets.Length; ++i)
plotOuterPerson(document, centre, radius, angleSets[i], personSet[i], currentGeneration, fontSize);

}

private static void plotOuterPerson(SvgDocument document, PointF centre, float radius, float angle, Person person, int currentGeneration, float fontSize)
private static void plotOuterPerson(SvgDocument document, PointF centre, float radius, float angle, Person person, int currentGeneration, IConfiguration configuration)
{
if (person == Person.NoPerson)
return;
// x / ( 2 * pi * r) = deltaAngle / 360
var deltaAngle = fontSize / (2.0f * MathF.PI * radius) * 360.0f;
if (angle < 0)
deltaAngle = -deltaAngle;

if (currentGeneration < 6)
{
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle - deltaAngle,
person.Name,
Color.Black,
fontSize));
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle,
person.FamilyName,
Color.Black,
fontSize));
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle + deltaAngle,
createYearText(person),
Color.FromArgb(255, 139, 139, 142),
fontSize));
}
else if (currentGeneration < 7)
var angles = Array.Empty<float>();
var textInfos = configuration.GetTextInfo(person, currentGeneration).ToArray();

switch (textInfos.Length)
{
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle - deltaAngle /2.0f,
person.Name,
Color.Black,
fontSize));
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle + deltaAngle / 2.0f,
person.FamilyName,
Color.Black,
fontSize));
case 1:
angles = new[] { angle };
break;
case 2:
var deltaAngle = (textInfos[0].FontSize + textInfos[1].FontSize) / (4.0f * MathF.PI * radius) * 360.0f;
if (angle < 0)
deltaAngle = -deltaAngle;
angles = new[] { angle - deltaAngle / 2.0f, angle + deltaAngle / 2.0f };
break;
case 3:
var deltaAngle_1 = (textInfos[0].FontSize + textInfos[1].FontSize) / (4.0f * MathF.PI * radius) * 360.0f;
var deltaAngle_2 = (textInfos[1].FontSize + textInfos[2].FontSize) / (4.0f * MathF.PI * radius) * 360.0f;
if (angle < 0)
{
deltaAngle_1 = -deltaAngle_1;
deltaAngle_2 = -deltaAngle_2;
}
angles = new[] { angle - deltaAngle_1, angle, angle + deltaAngle_2};
break;
default:
throw new NotImplementedException();
}
else

foreach (var textLine in angles.Select((a, i) => (angle: a, textInfo: textInfos[i])))
{
//var name = $"{person.Name.Remove(1,person.Name.Length - 1)}.{person.FamilyName}";
var name = $"{person.Name} {person.FamilyName}";
document.Children.Add(RotatedLineCreator.CreateRotatedText(
centre,
radius,
angle,
name,
Color.Black,
fontSize));
centre,
radius,
textLine.angle,
textLine.textInfo.Text,
textLine.textInfo.Color,
textLine.textInfo.FontSize));
}
}

private static void plotInnerPersons(SvgDocument document, PointF centre, float radius, (float start, float end)[] angleSets, Person[] personSet, float fontSize)
{
for(int i = 0; i <angleSets.Length; ++i)
plotInnerPerson(document, centre, radius, angleSets[i], personSet[i], fontSize);

}

private static void plotInnerPerson(SvgDocument document, PointF centre, float radius, (float innerAngle, float outerAngle) angle, Person person, float fontSize)
private static void plotInnerPerson(SvgDocument document, PointF centre, float radius, (float innerAngle, float outerAngle) angle, Person person, int currentGeneration, IConfiguration configuration)
{
if (person == Person.NoPerson)
return;
document.Children.Add(ArcCreator.CreateArcText( centre,
radius + fontSize,
angle.innerAngle,
angle.outerAngle,
person.Name,
Color.Black,
fontSize));
document.Children.Add(ArcCreator.CreateArcText( centre,
radius,
angle.innerAngle,
angle.outerAngle,
person.FamilyName,
Color.Black,
fontSize));
document.Children.Add(ArcCreator.CreateArcText( centre,
radius - fontSize,

var textInfos = configuration.GetTextInfo(person, currentGeneration).ToArray();
if (textInfos.Length != 3)
throw new NotImplementedException();

var radii = new[] { radius + textInfos[0].FontSize / 2.0f + textInfos[1].FontSize / 2.0f,
radius,
radius - textInfos[1].FontSize / 2.0f - textInfos[2].FontSize / 2.0f};

foreach (var textLine in radii.Select((r, i) => (radius: r, textInfo: textInfos[i])))
{
document.Children.Add(ArcCreator.CreateArcText(centre,
textLine.radius,
angle.innerAngle,
angle.outerAngle,
createYearText(person),
Color.FromArgb(255, 139, 139, 142),
fontSize));
angle.outerAngle,
textLine.textInfo.Text,
textLine.textInfo.Color,
textLine.textInfo.FontSize));
}
}

private static void plotMainPerson(SvgDocument document, PointF centre, Person person, float fontSize)
private static void plotMainPerson(SvgDocument document, PointF centre, Person person, IConfiguration configuration)
{
if (person == Person.NoPerson)
return;
document.Children.Add(TextCreator.CreateHorizontalText(new PointF(centre.X, centre.Y - fontSize),
person.Name,
Color.Black,
fontSize));
document.Children.Add(TextCreator.CreateHorizontalText( centre,
person.FamilyName,
Color.Black,
fontSize));
document.Children.Add(TextCreator.CreateHorizontalText( new PointF(centre.X, centre.Y + fontSize),
createYearText(person),
Color.FromArgb(255, 139, 139, 142),
fontSize));

var textInfos = configuration.GetTextInfo(person, 1).ToArray();
if (textInfos.Length != 3)
throw new NotImplementedException();

var points = new[] { PointF.Subtract(centre, new SizeF(0, textInfos[0].FontSize / 2.0f + textInfos[1].FontSize / 2.0f)),
centre,
PointF.Add(centre, new SizeF(0, textInfos[1].FontSize / 2.0f + textInfos[2].FontSize / 2.0f))};

foreach(var textLine in points.Select((p, i) => (point: p, textInfo: textInfos[i])))
{
document.Children.Add(TextCreator.CreateHorizontalText(textLine.point,
textLine.textInfo.Text,
textLine.textInfo.Color,
textLine.textInfo.FontSize));
}
}

private static string createYearText(Person person)
Expand Down

0 comments on commit 7b42aad

Please sign in to comment.