Нарисовать необычную фигуру - C#
Формулировка задачи:
Необходимо нарисовать вот это. Для этого нужно знать все линии каждого сегмента т.е. массив линий образующих это сегмент, затем рисовать закрашивая данный сегмент, а затем контур этого сегмента. Но как получить линии каждого из сегмента не приходит в голову, не могу придумать алгоритм.
Решение задачи: «Нарисовать необычную фигуру»
textual
Листинг программы
class MainForm: Form
{
public MainForm()
{
Size = new Size(400, 400);
int totalSectors = 7;
int totalNumbers = 10;
float smallRadius = 10;
float bigRadius = 135;
float centerX = 200;
float centerY = 200;
Paint += (s, a) =>
{
a.Graphics.TranslateTransform(centerX, centerY);
a.Graphics.RotateTransform(-90);
a.Graphics.SmoothingMode = SmoothingMode.HighQuality;
a.Graphics.DrawLine(Pens.Blue, -0.1f, -0.1f, 0.1f, 0.1f);
for (int sector = 0; sector < totalSectors; sector++)
for (int fromCenter = 0; fromCenter < totalNumbers; fromCenter++)
{
var gp = GetArea(sector, totalSectors, fromCenter, totalNumbers, smallRadius, bigRadius);
//a.Graphics.DrawPath(Pens.Red, gp);
a.Graphics.FillPath(Guid.NewGuid().GetHashCode() % 2 == 0 ? Brushes.Blue : Brushes.Green, gp);
}
};
}
private GraphicsPath GetArea(
int sectorNumber, int totalSectors,
int fromCenterNumber, int totalNumbers,
float smallRadius, float bigRadius)
{
double deltaAngle = 2 * Math.PI / totalSectors;
double directionAngle = deltaAngle * sectorNumber;
double deltaRadius = (bigRadius - smallRadius) / totalNumbers;
double minorRadius = deltaRadius * fromCenterNumber + smallRadius;
double majorRadius = minorRadius + deltaRadius;
var minorRectangle = new RectangleF(-(float)minorRadius, -(float)minorRadius,
(float) (2 * minorRadius), (float) (2 * minorRadius));
var majorRectangle = minorRectangle;
majorRectangle.Inflate((float)deltaRadius, (float)deltaRadius);
var g = new GraphicsPath();
g.AddArc(minorRectangle, ToDegrees(directionAngle + deltaAngle), ToDegrees(-deltaAngle));
g.AddLine((float)(Math.Cos(directionAngle) * minorRadius), (float)(Math.Sin(directionAngle) * minorRadius),
(float)(Math.Cos(directionAngle) * majorRadius), (float)(Math.Sin(directionAngle) * majorRadius));
g.AddArc(majorRectangle, ToDegrees(directionAngle), ToDegrees(deltaAngle));
g.CloseFigure();
return g;
}
private float ToDegrees(double angle)
{
return (float)(angle * 360 / 2 / Math.PI);
}
}