/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Graphics/CapsuleShape.cs
131 строка
3 KB
EugenyCh7
++ Shapes
21 июл 2025, 22:44
21 июл 2025, 22:44
eab4816
Код
Авторство
О чём код?
using Puppet2D.System; using System; namespace Puppet2D.Graphics { public class CapsuleShape : Shape { private const uint defaultCornerPoints = 10; private const float pi = 3.141592654f; private uint cornerPoints; private Vector2 center1; private Vector2 center2; private float radius; public CapsuleShape(Vector2 center1, Vector2 center2, float radius, uint cornerPoints = defaultCornerPoints) : base() { this.center1 = center1; this.center2 = center2; this.radius = radius; this.cornerPoints = cornerPoints; Update(); } public CapsuleShape(CapsuleShape copy) : base(copy) { center1 = copy.center1; center2 = copy.center2; radius = copy.radius; cornerPoints = copy.cornerPoints; Update(); } /// <summary> /// The number of vertices to smooth each side. /// </summary> public uint CornerPoints { get { return cornerPoints; } set { cornerPoints = value; Update(); } } /// <summary> /// The first center of the capsule. /// </summary> public Vector2 Center1 { get { return center1; } set { center1 = value; Update(); } } /// <summary> /// The second center of the capsule. /// </summary> public Vector2 Center2 { get { return center2; } set { center2 = value; Update(); } } /// <summary> /// The radius of the capsule. /// </summary> public float Radius { get { return radius; } set { radius = value; Update(); } } public override Vector2 GetPoint(uint index) { Vector2 dc = center2 - center1; float angle = MathF.Atan2(dc.Y, dc.X); // Наклон капсулы float inner = MathF.Sqrt(dc.X * dc.X + dc.Y * dc.Y); // Расстояние между центрами float halfInner = inner * 0.5f; Vector2 v = new Vector2(Radius, 0); Vector2 point; if (index < cornerPoints) { // Правая часть float a = -pi * index / (cornerPoints - 1) + pi * 0.5f; point = v.Rotate(a); point.X += halfInner; } else { // Левая часть float a = -pi * (index - cornerPoints) / (cornerPoints - 1) - pi * 0.5f; point = v.Rotate(a); point.X -= halfInner; } return point.Rotate(-angle) + (center1 + center2) * 0.5f; } public override uint GetPointCount() { return cornerPoints * 2; } } }