namespace AcTools
{
public class HH
{
public static TransientManager tm = TransientManager.CurrentTransientManager;
public static Random rand = new Random();
public static Vector3D G = new Vector3D(0,-10,0);// - Vector3D. * 10;
public static Document doc => Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
public static Editor ed => doc.Editor;
[CommandMethod ("show")]
public static void demo()
{
颜色 c;
List<Circle> cirs = new List<Circle>();
for (int i = 1; i < 257; i++)
{
Circle circle = new Circle()
{
Center = Point3d.Origin,
Radius = i + 1,
Color = Z.Rainbow彩虹色(i, 1, 256)
};
cirs.Add(circle);
}
Z.db.AddEntityToModeSpace(cirs.ToArray());
火花 h = new 火花();
var psr = Z.ed.GetPoint("");
while (psr.Status == PromptStatus.OK)
{
h.添加火点(new Point3d(psr.Value.X ,psr.Value .Y ,0));
h.添加火点(new Point3d(psr.Value.X +600,psr.Value .Y-20 ,0));
h.添加火点(new Point3d(psr.Value.X - 600, psr.Value.Y+20, 0));
h.添加火点(new Point3d(psr.Value.X + 900, psr.Value.Y - 20, 0));
h.添加火点(new Point3d(psr.Value.X - 900, psr.Value.Y + 20, 0));
psr = Z.ed.GetPoint("");
}
}
}
public delegate Autodesk.AutoCAD.Colors.Color 颜色(double k, double min, double max);
public class 火花:IDisposable
{
public void Dispose()
{
timer.Tick -= 火星飞行事件;
火星s.Clear();
timer.Stop();
timer.Dispose();
}
Timer timer;
List<火星>火星s=new List<火星> ();
//int k = 0;
public 火花()//一个火花包括很多个火星
{
timer = new Timer();
火星s = new List<火星>();
timer.Interval = 20;//相当于多久更新一下屏幕
timer.Tick += 火星飞行事件;
timer.Start();
}
public void 添加火点(Point3d pt)
{
int n = new Random().Next(26, 40);//一个火球爆发出几束烟花
var sudu = new Random().Next(16,27);//速度
int ys = HH.rand.Next(0, 5);//颜色
颜色 cc;
switch (ys)
{
case 0: cc = Rainbow紫到蓝; break;//绿
case 1: cc = Rainbow彩虹色; break;//绿
case 2: cc = Rainbow红到黄; break;//黄
case 3: cc = Rainbow黄到绿; break;//紫
case 4: cc = Rainbow绿到蓝; break;//橙
default: cc = Rainbow蓝到紫; break;//蓝
}
double 衰减速度 = HH.rand.NextDouble() * 0.05 + 0.95;
for (int i = 0; i < n; i++)
{
var vt = new Vector3d(HH.rand.NextDouble() - 0.5, HH.rand.NextDouble() - 0.5, HH.rand.NextDouble() - 0.5);
火星s.Add(new 火星(pt, vt, sudu, cc, 衰减速度));
}
}
private void 火星飞行事件(object sender, EventArgs e)
{
//List<火星> toRemove = new List<火星>();
List<火星> temp = new List<火星>();
foreach (var h in 火星s)//刚开始一个火球爆炸产生几个小球
{
h.运动();//每个小球开始自己的运动轨迹
}
List<Circle> cirs = new List<Circle>();
for (int i = 火星s.Count - 1; i >= 0; i--)//index为0的元素也得删除
{
if (火星s[i].灭)
{
cirs.AddRange(火星s[i].cirs);
火星s.RemoveAt(i);
//toRemove.Add(火星s[i]);
}
}
foreach (var c in cirs)
{
HH.tm.EraseTransient(c, new IntegerCollection());//这里比较卡,瞬态删除太慢,代码有待优化
HH.ed.UpdateScreen();
// System.Windows.Forms.Application.DoEvents();
}
System.Windows.Forms.Application.DoEvents();
}
public static Color Rainbow紫到蓝(double a, double min, double max)
{
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double step = a - min;
double num = max - min;
double r = 0.0; double g = 0.0; double b = 0.0;
double h = step / num;
double i = (int)(h * 5);
double f = h * 5.0 - i;
double q = 1 - f;
switch (i % 5)
{
case 3: r = 1; g = q; b = 0; break;//红色到黄色
case 2: r = f; g = 1; b = 0; break;//黄色到绿色
case 0: r = 0; g = f; b = 1; break;//绿到蓝
case 1: r = 0; g = 1; b = q; break;
case 4: r = 1; g = 0; b = f; break;
}
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static Color Rainbow红到黄(double a, double min, double max)
{//红到黄 (255 ,0 ,0) ---- (255 ,255 ,0)
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double normalizedA = (a - min) / (max - min);
double r = 1.0; double g = normalizedA; double b = 0.0;
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static Color Rainbow黄到绿(double a, double min, double max)
{////黄到绿(255 ,255 ,0) - (0, 255,0 )
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double normalizedA = (a - min) / (max - min);
double r = 1 - normalizedA; double g = 1.0; double b = 0.0;
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static Color Rainbow绿到蓝(double a, double min, double max)
{//////绿到蓝0, 255,0 ---- 0 ,0 ,255
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double normalizedA = (a - min) / (max - min);
double r = 0; double g = 1 - normalizedA; double b = normalizedA;
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static Color Rainbow蓝到紫(double a, double min, double max)
{///蓝到紫 (0 ,0 ,255) - (255, 0, 255)
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double normalizedA = (a - min) / (max - min);
double r = normalizedA; double g = 0; double b = 1;
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
public static Color Rainbow彩虹色(double a, double min, double max)
{///红到紫 (255 ,0 ,0) - (255, 0, 255)//128 1 320
max ++;
if (min > max) { double v = min; min = max; max = v; }
if (a > max) a = max; if (a < min) a = min;
double step = a - min;//127
double num = max - min;//319
double r = 0.0; double g = 0.0; double b = 0.0;
double h = step / num;//0.398
double i = (int)(h * 4); //i = (int)Math.Round(h * 5);//1.99 1 会出现同心圆颜色
double f = h * 4.0 - i;//0.99 ,本应该是0
double q = 4 - f;//4.01,本该是5
switch (i % 4)
{
//if i == 128 debugger.stop;
case 0: r = 1; g = f; b = 0; break;//红色到黄色
case 1: r = q; g = 1; b = 0; break;//黄色到绿色
case 2: r = 0; g = q; b = f; break;//绿到蓝
case 3: r = f; g = 0; b = 1; break; ;//蓝到紫
case 4: r = 1; g = 0; b = q; break;//紫到红 目前有问题,画320个圆,128个颜色有问题
}
return Color.FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
}
public class 火星
{
Random rand = new Random();
public bool 灭;
public List<Circle>cirs = new List<Circle>();
Circle cir;
int k;
double 速度;
int 总数量;
颜色 颜色渐变;
double 衰减速度;
Vector3D 方向;
public 火星(Point3d cent,Vector3D 方向,double 初速度,颜色 yanse,double 衰减速度)
{
this.方向 = 方向;
this.速度 = 初速度;
this.颜色渐变 = yanse;
this.衰减速度 = 衰减速度;
总数量 = rand.Next(24,28);//一个火星延伸的长度
cir= new Circle(new Point3d( cent.X,cent.Y +300,0),Vector3d .ZAxis,0.1);//烟花出现在鼠标点的上方距离
cir.LineWeight = LineWeight.LineWeight015;
cir.Color = yanse(0,0,总数量);
}
public void 运动()
{
k++;
var vt = 方向.GetNormal() * 速度;
var g = new Vector3D(0, -10, 0);
vt = vt + HH.G;
cir.Center = cir.Center + vt;
Circle cir1 = cir.Clone() as Circle;
cir1.Color = 颜色渐变(k,0,总数量);
cirs.Add(cir1);
HH.tm.AddTransient(cir1, TransientDrawingMode.Main, 0, new IntegerCollection());
//List<Entity> ents = cirs.Cast<Entity>().ToList();//list类型强转
//Flash flash = new Flash(ents);//创建瞬态
速度 *= 衰减速度;
if (k > 总数量 || 速度 < 0.1)
{
灭 = true;
}
}
}
}