public static void CreateBranch(BranchArgs a, ref LightningData data, Random r) { // Find Displacement Vector2 d = a.End - a.Start; float dist = d.Length(); d /= dist; Vector2 n = new Vector2(-d.Y, d.X); // Get Range Of Lightning Displacements float range = a.LineMaxLength - a.LineMinLength; // Add Startpoint data.Lines.Add(a.Start); // Add Jags while (dist > a.LineMaxLength) { float disp = (float)r.NextDouble() * range + a.LineMinLength; a.Start += disp * d; dist -= disp; Vector2 nl = a.Start + ((float)r.NextDouble() * 2 - 1) * a.JagDisplacement * n; data.Lines.Add(nl); if (nl.X < a.MinBounds.X || nl.Y < a.MinBounds.Y || nl.X > a.MaxBounds.X || nl.Y > a.MaxBounds.Y) { return; } if (r.NextDouble() > 0.95) { Vector2 bd = nl - data.Lines[data.Lines.Count - 2]; bd.Normalize(); Vector2 bn = new Vector2(-bd.Y, bd.X); // Make Left Branch float dispb = ((float)r.NextDouble() * 2 - 1) * a.BranchSlope * dist; a.End = nl + bd * dist + bn * dispb; CreateBranch(a, ref data, r); // Make Right Branch dispb = ((float)r.NextDouble() * 2 - 1) * a.BranchSlope * dist; a.End = nl + bd * dist + bn * dispb; CreateBranch(a, ref data, r); return; } else { data.Lines.Add(nl); } } // Add Endpoint data.Lines.Add(a.End); }
public static void CreateBranch(BranchArgs a, ref LightningData data, Random r) { // Find Displacement Vector2 d = a.End - a.Start; float dist = d.Length(); d /= dist; Vector2 n = new Vector2(-d.Y, d.X); // Get Range Of Lightning Displacements float range = a.LineMaxLength - a.LineMinLength; // Add Startpoint data.Lines.Add(a.Start); // Add Jags while(dist > a.LineMaxLength) { float disp = (float)r.NextDouble() * range + a.LineMinLength; a.Start += disp * d; dist -= disp; Vector2 nl = a.Start + ((float)r.NextDouble() * 2 - 1) * a.JagDisplacement * n; data.Lines.Add(nl); if(nl.X < a.MinBounds.X || nl.Y < a.MinBounds.Y || nl.X > a.MaxBounds.X || nl.Y > a.MaxBounds.Y) return; if(r.NextDouble() > 0.95) { Vector2 bd = nl - data.Lines[data.Lines.Count - 2]; bd.Normalize(); Vector2 bn = new Vector2(-bd.Y, bd.X); // Make Left Branch float dispb = ((float)r.NextDouble() * 2 - 1) * a.BranchSlope * dist; a.End = nl + bd * dist + bn * dispb; CreateBranch(a, ref data, r); // Make Right Branch dispb = ((float)r.NextDouble() * 2 - 1) * a.BranchSlope * dist; a.End = nl + bd * dist + bn * dispb; CreateBranch(a, ref data, r); return; } else { data.Lines.Add(nl); } } // Add Endpoint data.Lines.Add(a.End); }