Нарисовать необычную фигуру - C#

Узнай цену своей работы

Формулировка задачи:

Необходимо нарисовать вот это. Для этого нужно знать все линии каждого сегмента т.е. массив линий образующих это сегмент, затем рисовать закрашивая данный сегмент, а затем контур этого сегмента. Но как получить линии каждого из сегмента не приходит в голову, не могу придумать алгоритм.

Решение задачи: «Нарисовать необычную фигуру»

textual
Листинг программы
  1. class MainForm: Form
  2. {
  3.     public MainForm()
  4.     {
  5.         Size = new Size(400, 400);
  6.        
  7.         int totalSectors = 7;
  8.         int totalNumbers = 10;
  9.        
  10.         float smallRadius = 10;
  11.         float bigRadius = 135;
  12.        
  13.         float centerX = 200;
  14.         float centerY = 200;
  15.  
  16.         Paint += (s, a) =>
  17.         {
  18.             a.Graphics.TranslateTransform(centerX, centerY);
  19.             a.Graphics.RotateTransform(-90);
  20.             a.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  21.            
  22.             a.Graphics.DrawLine(Pens.Blue, -0.1f, -0.1f, 0.1f, 0.1f);
  23.            
  24.             for (int sector = 0; sector < totalSectors; sector++)
  25.             for (int fromCenter = 0; fromCenter < totalNumbers; fromCenter++)
  26.             {
  27.                 var gp = GetArea(sector, totalSectors, fromCenter, totalNumbers, smallRadius, bigRadius);
  28.                 //a.Graphics.DrawPath(Pens.Red, gp);
  29.                 a.Graphics.FillPath(Guid.NewGuid().GetHashCode() % 2 == 0 ? Brushes.Blue : Brushes.Green, gp);
  30.             }
  31.         };
  32.     }
  33.    
  34.     private GraphicsPath GetArea(
  35.         int sectorNumber, int totalSectors,
  36.         int fromCenterNumber, int totalNumbers,
  37.         float smallRadius, float bigRadius)
  38.     {
  39.         double deltaAngle = 2 * Math.PI / totalSectors;
  40.         double directionAngle = deltaAngle * sectorNumber;
  41.        
  42.         double deltaRadius = (bigRadius - smallRadius) / totalNumbers;
  43.         double minorRadius = deltaRadius * fromCenterNumber + smallRadius;
  44.         double majorRadius = minorRadius + deltaRadius;
  45.        
  46.         var minorRectangle = new RectangleF(-(float)minorRadius, -(float)minorRadius,
  47.             (float) (2 * minorRadius), (float) (2 * minorRadius));
  48.         var majorRectangle = minorRectangle;
  49.         majorRectangle.Inflate((float)deltaRadius, (float)deltaRadius);
  50.        
  51.         var g = new GraphicsPath();
  52.        
  53.         g.AddArc(minorRectangle, ToDegrees(directionAngle + deltaAngle), ToDegrees(-deltaAngle));
  54.         g.AddLine((float)(Math.Cos(directionAngle) * minorRadius), (float)(Math.Sin(directionAngle) * minorRadius),
  55.             (float)(Math.Cos(directionAngle) * majorRadius), (float)(Math.Sin(directionAngle) * majorRadius));
  56.         g.AddArc(majorRectangle, ToDegrees(directionAngle), ToDegrees(deltaAngle));
  57.         g.CloseFigure();
  58.        
  59.         return g;
  60.     }
  61.    
  62.     private float ToDegrees(double angle)
  63.     {
  64.         return (float)(angle * 360 / 2 / Math.PI);
  65.     }
  66. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы