// Spludlow Software
// Copyright © Samuel P. Ludlow 2020 All Rights Reserved
// Distributed under the terms of the GNU General Public License version 3
// Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
// https://www.spludlow.co.uk/LICENCE.TXT
// The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
// v1.14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Spludlow.Drawing
{
///
/// Maths used in drawing
///
public class Maths
{
public static PointF ArcPoint(float x, float y, float xRadius, float yRadius, float angle)
{
angle += 90;
angle *= (float)(Math.PI / 180);
float x1 = x + xRadius * (float)Math.Cos(angle);
float y1 = y + yRadius * (float)Math.Sin(angle);
return new PointF(x1, y1);
}
public static PointF[] Polygon(float x, float y, float xRadius, float yRadius, int sides, float rotateAngle)
{
float whole = (float)(2 * Math.PI); // convert to arcPoint
float step = whole / sides;
rotateAngle += 90;
rotateAngle *= (float)(Math.PI / 180);
List points = new List();
for (int side = 0; side < sides; ++side)
{
float angle = side * step - rotateAngle;
float x1 = x + xRadius * (float)Math.Cos(angle);
float y1 = y + yRadius * (float)Math.Sin(angle);
points.Add(new System.Drawing.PointF(x1, y1));
}
return points.ToArray();
}
public static PointF[] ArcSector(float x, float y, float xOuterRadius, float yOuterRadius, float xInnerRadius, float yInnerRadius, float startAngle, float endAngle)
{
startAngle += 180;
endAngle += 180;
List points = new List();
float sweepAngle = endAngle - startAngle;
int totalSteps = (int)(sweepAngle); // equal to degrees ????
float stepAngle = sweepAngle / (float)totalSteps;
PointF point;
for (int stepIndex = 0; stepIndex < totalSteps; ++stepIndex)
{
float angleA = startAngle + (float)stepIndex * stepAngle;
float angleB = startAngle + (float)(stepIndex + 1) * stepAngle;
point = ArcPoint(x, y, xOuterRadius, yOuterRadius, angleA);
points.Add(point);
point = ArcPoint(x, y, xOuterRadius, yOuterRadius, angleB);
points.Add(point);
}
for (int stepIndex = totalSteps; stepIndex > 0; --stepIndex)
{
float angleA = startAngle + (float)stepIndex * stepAngle;
float angleB = startAngle + (float)(stepIndex - 1) * stepAngle;
point = ArcPoint(x, y, xInnerRadius, yInnerRadius, angleA);
points.Add(point);
point = ArcPoint(x, y, xInnerRadius, yInnerRadius, angleB);
points.Add(point);
}
point = ArcPoint(x, y, xOuterRadius, yOuterRadius, startAngle);
points.Add(point);
return points.ToArray();
}
public static PointF TransformPoint(PointF point, float scale, float angle, PointF origin)
{
angle = (angle / 180.0F) * (float)Math.PI;
float x = point.X - origin.X;
float y = point.Y - origin.Y;
x *= scale;
y *= scale;
float rotatedX = x * (float)Math.Cos(angle) - y * (float)Math.Sin(angle);
float rotatedY = x * (float)Math.Sin(angle) + y * (float)Math.Cos(angle);
return new PointF(rotatedX + origin.X, rotatedY + origin.Y);
}
}
}