using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LR2 { class MnkOrtho { PictureBox pictureBox; RichTextBox richTextBox; List listPoints; Graphics g; float scale = 35; public MnkOrtho(PictureBox pictureBox, RichTextBox richTextBox, float scale = 35) { this.scale = scale; this.pictureBox = pictureBox; this.richTextBox = richTextBox; if (pictureBox != null) { g = pictureBox.CreateGraphics(); g.TranslateTransform(pictureBox.Width / 2, pictureBox.Height / 2); g.Clear(Color.White); g.DrawLine(Pens.Black, 0, -200, 0, 200); g.DrawLine(Pens.Black, -200, 0, 200, 0); } } public void solve(List listPoints) { this.listPoints = listPoints; List resultPoints = new List(); for (double x = -7; x < 9; x += 3) { resultPoints.Add(new PointF((float)x, (float)GetApproximation(x))); } drawOriginalCurve(); drawApproximationCurve(resultPoints); } public double GetQ(double x, int k) { double retValue = 0.0; if (k < 0) { retValue = 0.0; return retValue; } else if (k == 0) { retValue = 1.0; return retValue; } else if (k > 0) { if (k > 1) retValue = x * GetQ(x, k - 1) - GetAlpha(x, k - 1) * GetQ(x, k - 1) - GetBeta(x, k - 1) * GetQ(x, k - 2); else retValue = x * GetQ(x, k - 1) - GetAlpha(x, k - 1) * GetQ(x, k - 1); return retValue; } return retValue; } public double GetAlpha(double x, int k) { double chisl = 0.0, znam = 0.0, retValue = 0.0, temp = 0.0; for (int i = 0; i < listPoints.Count; ++i) { temp = GetQ(listPoints[i].X, k); chisl += listPoints[i].X * temp * temp; znam += temp * temp; } retValue = chisl / znam; return retValue; } public double GetBeta(double x, int k) { double chisl = 0.0, znam = 0.0, retValue = 0.0, temp1 = 0.0, temp2 = 0.0; for (int i = 0; i < listPoints.Count; ++i) { temp1 = GetQ(listPoints[i].X, k); temp2 = GetQ(listPoints[i].X, k - 1); chisl += listPoints[i].X * temp1 * temp2; znam += temp2 * temp2; } retValue = chisl / znam; return retValue; } public double GetFx(double x) { return Math.Sin(x) * Math.Sin(x) * Math.Sin(x) * Math.Sin(x) * Math.Sin(x);//Math.Abs(Math.Sin(2 * x)) * Math.Cos(x); } public double GetC(int k) { double retValue = 0.0, chisl = 0.0, znam = 0.0; for (int i = 0; i < listPoints.Count; ++i) { double curQ = GetQ(listPoints[i].X, k); chisl += curQ * listPoints[i].Y; znam += curQ * curQ; } retValue = chisl / znam; return retValue; } public double GetApproximation(double x) { double retValue = 0.0; for (int i = 0; i < listPoints.Count; ++i) retValue += GetC(i) * GetQ(x, i); return retValue; } private bool isEqualNull(double x) { return Math.Abs(x) <= 1e-8; } private void drawApproximationCurve(List points) { int n = points.Count; for (int i = 1; i < n; ++i) { g.DrawLine(Pens.Blue, points[i].X * scale, -points[i].Y * scale, points[i - 1].X * scale, -points[i - 1].Y * scale); } } private void drawOriginalCurve() { PointF curPoint = new PointF(0, 0), nextPoint = new PointF(0, 0); bool firstIter = true; for (double x = -10; x < 10; x += 0.01) { curPoint = nextPoint; nextPoint = new PointF((float)x, (float)GetFx(x)); if (firstIter) { firstIter = false; continue; } g.DrawLine(Pens.Red, nextPoint.X * scale, -nextPoint.Y * scale, curPoint.X * scale, -curPoint.Y * scale); } } } }