public Bitmap Draw(ResistorStyle style) { List <Bitmap> resistors = Resistors.Select(x => x.Draw(style)).ToList(); Bitmap bitmap = new Bitmap(resistors.Max(x => x.Width) + style.WireLength * 2, resistors.Sum(x => x.Height + style.WireLength) - style.WireLength, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmap)) { g.FillRectangle(new SolidBrush(style.BackColor), 0, 0, bitmap.Width, bitmap.Height); int sum = 0; Pen pen = new Pen(new SolidBrush(style.WireColor), style.WireThickness); resistors.ForEach(res => { g.DrawLine(pen, 0, sum + res.Height / 2, bitmap.Width - style.WireLength - style.WireThickness, sum + res.Height / 2); g.DrawImage(res, (bitmap.Width - res.Width) / 2, sum); sum += res.Height + style.WireLength; }); g.DrawLine(pen, 0, resistors[0].Height / 2, 0, sum - style.WireLength - resistors.Last().Height / 2); g.DrawLine(pen, bitmap.Width - style.WireLength - style.WireThickness, resistors[0].Height / 2, bitmap.Width - style.WireLength - style.WireThickness, sum - style.WireLength - resistors.Last().Height / 2); g.DrawLine(pen, bitmap.Width - style.WireThickness, bitmap.Height / 2, bitmap.Width - style.WireLength - style.WireThickness, sum - style.WireLength - bitmap.Height / 2); } return(bitmap); }
private void IDC_NEXTR_BUTTON_Click(object sender, EventArgs e) { var currentId = Int32.Parse(m_nextr.Text); if (currentId <= parameters.NumResistors) { Resistors.Add(ReadElementData(ElementType.Resistor, parameters.NumResistors, Resistors.Count)); } else if (currentId <= parameters.NumCapacitors + parameters.NumResistors) { Capacitors.Add(ReadElementData(ElementType.Capacitor, parameters.NumCapacitors, Capacitors.Count)); } else if (currentId <= parameters.NumCapacitors + parameters.NumResistors + parameters.NumInductors) { Inductors.Add(ReadElementData(ElementType.Inductor, parameters.NumInductors, Inductors.Count)); } if (currentId == parameters.NumCapacitors + parameters.NumResistors + parameters.NumInductors) { Close(); } if (currentId == parameters.NumResistors) { elementTypeTxt.Text = ElementType.Capacitor.ToString(); valueLbl.Text = "Ёмкость (мкФ)"; } else if (currentId == parameters.NumCapacitors + parameters.NumResistors) { elementTypeTxt.Text = ElementType.Inductor.ToString(); valueLbl.Text = "Индкутивность (Гн)"; } }
public static double CalculateRatio(Resistors Resistors, double WantedValue, double MinError, bool Inverted) { double ratio = Resistors.R1 / Resistors.R2; // Ger error double errorRatio = EsseivaN.Tools.Tools.GetErrorPercent(WantedValue, ratio); // Add if serial in range if (Math.Abs(errorRatio) <= MinError) { if (Inverted) { Resistors = new Resistors() { R1 = Resistors.R2, R2 = Resistors.R1, }; ratio = 1 / ratio; } Result res = new Result() { BaseResistors = Resistors, Ratio = ratio, Parallel = false, Error = errorRatio }; Results.Add(res); } // Return lowest error return(Math.Abs(errorRatio)); }
public async Task <IActionResult> Edit(int id, [Bind("ResistorsId,Ohms,Information")] Resistors resistors) { if (id != resistors.ResistorsId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(resistors); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ResistorsExists(resistors.ResistorsId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(resistors)); }
public async Task <IActionResult> Create([Bind("ResistorsId,Ohms,Information")] Resistors resistors) { if (ModelState.IsValid) { _context.Add(resistors); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(resistors)); }
static void Main() { //New resistor object Resistors resistor = new Resistors(); resistor.SetResistanceAndMax(); //Parse and splits resistance and max resistor.SetVoltage(); //Calcs voltage resistor.CalcPwrDis(); //Calculates Power Dissipation resistor.PrintTable(); //Displays table Console.ReadLine(); } //End Main()
/// <summary> /// Calculate equivalent resistor in serial and parallel configuration. If valid, add to results /// </summary> /// <returns>Lowest error</returns> public static double CalculateResistors(Resistors Res, double WantedValue, double MinError) { // parallel resistor double pr = Res.GetParallelResistor(); // Serial resistor double sr = Res.R1 + Res.R2; // Ger error double pErrorRatio = Tools.GetErrorPercent(WantedValue, pr); double sErrorRatio = Tools.GetErrorPercent(WantedValue, sr); // Add if serial in range if (Math.Abs(sErrorRatio) <= MinError) { Res.SerialValid = true; Res.SerialResult = new Result() { BaseResistors = Res, Resistor = sr, Parallel = false, Error = sErrorRatio }; } // Add if parallel in range if (Math.Abs(pErrorRatio) <= MinError) { Res.ParallelValid = true; Res.ParallelResult = new Result() { BaseResistors = Res, Resistor = pr, Parallel = true, Error = pErrorRatio }; } // Return lowest error return(Math.Min(Math.Abs(pErrorRatio), Math.Abs(sErrorRatio))); }
public Bitmap Draw(ResistorStyle style) { List <Bitmap> resistors = Resistors.Select(x => x.Draw(style)).ToList(); Bitmap bitmap = new Bitmap(resistors.Sum(x => x.Width), resistors.Max(x => x.Height), PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmap)) { g.FillRectangle(new SolidBrush(style.BackColor), 0, 0, bitmap.Width, bitmap.Height); int sum = 0; resistors.ForEach(res => { g.DrawImage(res, sum, (bitmap.Height - res.Height) / 2); sum += res.Width; }); } return(bitmap); }
public object Clone() => new ParallelResistor { Resistors = Resistors.Select(x => (IResistor)x.Clone()).ToList() };
public static void FillTable(BackgroundWorker b, List <short> Serie, double MinError, double WantedValue) { Results.Clear(); MinError = Math.Abs(MinError); double currentPercent = 0; double PreviousPercent = 0; bool Inverted = false; if (!(WantedValue > 0 && WantedValue < 1)) { Inverted = true; WantedValue = 1 / WantedValue; } // For each R1 in the serie foreach (var r1 in Serie) { // Check if cancel asked if (b.CancellationPending) { return; } // Update progress int progress = ((Results.Count * 100) / maxResults); if (progress > 100) { progress = 100; } else if (progress < 0) { progress = 0; } b.ReportProgress(progress); // Take the min value double r1t = r1; while (r1t >= (10 * minRes)) { r1t /= 10; } while (r1t < minRes) { r1t *= 10; } // Check only for one occurence of R1, you just multiply left and right by 10 to get the other one... if (r1t <= maxRes) { // Check overflow if (Results.Count >= maxResults) { WriteLog("Buffer size reached, incomplete results", Logger.Log_level.Warn); MessageBox.Show("Maximum number of result reached\nNot all results are found, you may want to decrease the minimum error or increase the buffer size !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // With R1 fixed, select R2 in the serie foreach (var r2 in Serie) { double r2t = r2; // Take the min value while (r2t >= (10 * minRes)) { r2t /= 10; } while (r2t < minRes) { r2t *= 10; } PreviousPercent = -1; // Check for all powers of R2 while (r2t <= maxRes) { Resistors Res = new Resistors() { R1 = r1t, R2 = r2t, Parallel = false, }; // Get the error percent and save result if in error range currentPercent = CalculateRatio(Res, WantedValue, MinError, Inverted); // If error is increasing, it will continue, so jump to next R2 if (currentPercent > PreviousPercent && PreviousPercent != -1) { break; } PreviousPercent = currentPercent; r2t *= 10; } } } } }
/// <summary> /// Do the hard work, fill the result list /// </summary> public static void GetResults(BackgroundWorker b, List <short> Serie, double WantedValue, double MinError, double minRes, double maxRes, int maxResults) { // Clear previous results Results.Clear(); // Get absolute error MinError = Math.Abs(MinError); // Error percents double PreviousPercent = 0; // Progress value int progress; // Temp values r1 and r2 double r1t; double r2t; // No log in this area to maximize speed // For each R1 in the serie foreach (var r1 in Serie) { // Check if cancel asked if (b.CancellationPending) { return; } // Update progress progress = ((Results.Count * 100) / WindowParallelReverse.maxResults); if (progress > 100) { progress = 100; } else if (progress < 0) { progress = 0; } b.ReportProgress(progress); // Take the value nearest and greater than minRes r1t = r1; while (r1t >= (10 * minRes)) { r1t /= 10; } while (r1t < minRes) { r1t *= 10; } // Check for all powers of R1 until PowS or error increasing while (r1t <= maxRes) { // Check overflow (with duplicates) if (Results.Count >= maxResults) { // Clear duplicates Results = Results.Distinct().ToList(); // Check overflow without dplicates if (Results.Count >= maxResults) { WriteLog("Buffer size reached, incomplete results", Logger.Log_level.Warn); MessageBox.Show("Maximum number of result reached\nResults are incomplete and not all low error are found, you may want to decrease the minimum error or increase the buffer size !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } // If resistor is the wanted resistor if (r1t == WantedValue) { // Add result Resistors resistors = new Resistors() { R1 = r1t, R2 = 0, Parallel = false, }; Result res = new Result() { BaseResistors = resistors, Resistor = r1t, Parallel = false, Error = 0 }; Results.Add(res); } // With R1 fixed, select R2 in the serie foreach (var r2 in Serie) { r2t = r2; // Take the min value while (r2t >= (10 * minRes)) { r2t /= 10; } while (r2t < minRes) { r2t *= 10; } PreviousPercent = -1; // Check for all powers of R2 while (r2t <= maxRes) { Resistors Res = new Resistors() { R1 = Math.Min(r1t, r2t), R2 = Math.Max(r1t, r2t), Parallel = false, }; // Get the error percent and save result if in error range double currentPercent = Res.CalculateResistors(WantedValue, MinError); if (Res.SerialValid) { Results.Add(Res.SerialResult); } if (Res.ParallelValid) { Results.Add(Res.ParallelResult); } // If error is increasing, it will continue, so jump to next R2 if (currentPercent > PreviousPercent && PreviousPercent != -1 && PreviousPercent > MinError) { break; } PreviousPercent = currentPercent; // Next pow r2t *= 10; } } // Next pow r1t *= 10; } } }
public static double GetParallelResistor(Resistors resistors) { return((resistors.R1 * resistors.R2) / (resistors.R1 + resistors.R2)); }